2

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?

burger
  • 5,683
  • 9
  • 40
  • 63
  • 1
    I can't replicate this. Are you sure the R you are running is tied to the same version of Rscript you are running? Maybe try adding `library(methods)` to the script since Rscript doesn't load that by default. – MrFlick Apr 07 '17 at 17:03
  • Definitely the same version of R and Rscript (same `bin` and same `--version`). – burger Apr 07 '17 at 17:20
  • Adding `library(methods)` worked. What happened? – burger Apr 07 '17 at 17:21

2 Answers2

6

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 
Stu Field
  • 255
  • 2
  • 10
  • I was also using Matrix 1.2-8, so it makes more sense now. I still have no idea why executing using Rscript makes a difference. – burger Apr 07 '17 at 19:36
  • 1
    It makes a difference because `methods` is not attached when you "kick" a script via the `Rscript` command, but it IS loaded on a standard R startup. The environments are close, but not identical (which previously I had assumed!). Compare: `Rscript --vanilla -e "sessionInfo()"` to `R --vanilla (start R session)` `sessionInfo()` The methods package is attached in the latter but not the former. – Stu Field Apr 07 '17 at 19:53
  • thanks for clarifying! I didn't know that. I am surprised `methods` wasn't loaded by any other packages. I initially encountered this problem even after loading a few packages. – burger Apr 07 '17 at 19:56
  • Was news to me too. Lesson learned. It still doesn't answer what changed in Matrix_1.2-8 however. That's still a mystery. At least there is a work around until a bug fix is released. – Stu Field Apr 07 '17 at 19:58
2

Oh dear. I'm pretty (not 100% !) sure that this problem should not apply in newer version of R and Matrix. Still I would claim this is not a Matrix bug in a proper sense, rather either an 'Rscript' vs 'R' bug -- as @Stu Field mentions already; Rscript does not by default attach the methods package to the search() path, but regular R does.

OTOH, R CMD check Matrix nowadays should try to run Matrix without the methods package being in the search path, and hence the problem should not be present from Matrix 1.2-9 and newer, i.e., by default from R 3.4.0 and newer or then update the 'Matrix' package if you have an older version of R.

Again: Can you please confirm that the problem is gone with R 3.4.0 (which has Matrix 1.2-9 "along with it") ?

Here is a more useful example script, I called Rscript-tst.R. Run as

Rscript --vanilla Rscript-tst.R > Rscript-tst.Rout 2>&1

or (as myself with many R versions installed) something like

`R-3.4.0 RHOME`/bin/Rscript --vanilla Rscript-tst.R > Rscript-tst.Rout_R340 2>&1

Rscript-tst.R :

options(echo = TRUE)# << even with "Rscript" or --slave ...
(m <- system.file("external/pores_1.mtx", package = "Matrix"))
packageDescription("Matrix")
## This *load*s the Matrix package but does not attach it to search()
str(Matrix::readMM)
sessionInfo()
x <- Matrix::readMM(m)
## used to fail because 'methods' was not "there" (in Rscript only)
Martin Mächler
  • 4,619
  • 27
  • 27