Search code examples
rsparse-matrixrscript

R Error in validObject(.Object) when running as script, but not in console


The following code works fine in the R console (R 3.3.0):

m = system.file("external/pores_1.mtx", package = "Matrix")
x = Matrix::readMM(m)

I can put it in a script and it runs fine in R console as:

source("test.R")

However, when I execute it as Rscript --vanilla test.R or Rscript test.R, I get an error:

Error in validObject(.Object) : 
  invalid class “dgTMatrix” object: Not a valid 'Mnumeric' class object
Calls: <Anonymous> -> new -> initialize -> initialize -> validObject
Execution halted

I don't know if this is related to that specific function. I am guessing this has something to do with how Rscript works, but I used it with various other libraries and functions previously and have never seen anything like this. Any idea what is happening?


Solution

  • I can confirm ... I'm getting the exact same error when running a script containing a call to glmnet(). I was able to trace it back to the Matrix package, on which glmnet depends.

    I back-rev'd my R version from v3.3.3 -> v3.3.2 and the error disappeared. I then checked the sessionInfo() between the two and discovered that the version of the Matrix package differed ... it is 1.2-8 (in v3.3.3) and 1.2-7.1 (in v3.3.2). To confirm, I simply replaced the "OK" version of Matrix (7.1) with the "broken" version and the error returned.

    I can also confirm that explicitly loading the methods package via library(methods) resolves the error (somehow?), which explains the differing behavior between the console call and the Rscript call from the command line.

    So, it appears we have 2 work-arounds:

    1. library(methods)
    2. back rev your version of Matrix to 1.2-7.1.

    Neither is super satisfactory ... I'd just like to know what's going on with Matrix 1.2-8. Maybe it'll be bug-fixed in the next version.

    If you're interested, here is my sessionInfo():

    R version 3.3.3 (2017-03-06)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Ubuntu 16.04.2 LTS
    
    locale:
     [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
     [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
     [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
     [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  base
    
    other attached packages:
    [1] glmnet_2.0-5  foreach_1.4.3 Matrix_1.2-8 
    
    loaded via a namespace (and not attached):
    [1] codetools_0.2-15 grid_3.3.3       iterators_1.0.8  methods_3.3.3   
    
    [5] lattice_0.20-35