Consider this script (with h5 installed): $ cat test.R library(h5) name <- tempfile() f <- h5file(name) file.remove(name) $ Rscript test.R Error in initialize(value, ...) : cannot use object of class "character" in new(): class "H5File" does not extend that class Calls: h5file -> new -> initialize -> initialize Execution halted $ /usr/lib64/R/bin/R --slave --no-restore --file=test.R [1] TRUE $ R_DEFAULT_PACKAGES= Rscript test.R [1] TRUE $ R> source('test.R') >$ R_DEFAULT_PACKAGES='datasets,utils,grDevices,graphics,stats' R> source('test.R')Error in initialize(value, ...) : cannot use object of class "character" in new(): class "H5File" does not extend that class>After looking into C source code, I found that Rscript by default fills environment variable R_DEFAULT_PACKAGES with "datasets,utils,grDevices,graphics,stats", and it somehow fails some package like h5. The problem here is, not setting R_DEFAULT_PACKAGES is equivalent to setting it to a magic value, it's really confusing. I suggest remove this feature. [[alternative HTML version deleted]]
Dirk Eddelbuettel
2017-Dec-26 12:46 UTC
[Rd] Rscript fails with some packages (for example, h5)
On 26 December 2017 at 15:24, Sun Yijiang wrote: | After looking into C source code, I found that Rscript by default fills | environment variable R_DEFAULT_PACKAGES with | "datasets,utils,grDevices,graphics,stats", and it somehow fails some | package like h5. | | The problem here is, not setting R_DEFAULT_PACKAGES is equivalent to | setting it to a magic value, it's really confusing. I suggest remove this | feature. The more confusing part is that "methods" is missing 'by design' (as loading methods is marginally more expensive that other packages). Ie for your script edd at bud:/tmp$ cat h5ex.R library(methods) library(h5) name <- tempfile() f <- h5file(name) file.remove(name) edd at bud:/tmp$ Rscript h5ex.R [1] TRUE edd at bud:/tmp$ it all works if you just add `library(methods)` as seen in the first line. For what it is worth, littler's r does not need that as it loads methods just like R itself does avoiding the confusion: edd at bud:/tmp$ cat h5ex2.R library(h5) name <- tempfile() f <- h5file(name) file.remove(name) edd at bud:/tmp$ r h5ex2.R edd at bud:/tmp$ Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
Hi Dirk, Thanks for the solution. Now I know the work-arounds, but still don't quite get it. Why does R_DEFAULT_PACKAGES has anything to do with library(methods)? If library(h5) works, it should just work, not depend on an environment variable. Rscript is not consistent with R, that's my confusion. Steve 2017-12-26 20:46 GMT+08:00 Dirk Eddelbuettel <edd at debian.org>:> > On 26 December 2017 at 15:24, Sun Yijiang wrote: > | After looking into C source code, I found that Rscript by default fills > | environment variable R_DEFAULT_PACKAGES with > | "datasets,utils,grDevices,graphics,stats", and it somehow fails some > | package like h5. > | > | The problem here is, not setting R_DEFAULT_PACKAGES is equivalent to > | setting it to a magic value, it's really confusing. I suggest remove > this > | feature. > > The more confusing part is that "methods" is missing 'by design' (as > loading > methods is marginally more expensive that other packages). Ie for your > script > > edd at bud:/tmp$ cat h5ex.R > library(methods) > library(h5) > name <- tempfile() > f <- h5file(name) > file.remove(name) > edd at bud:/tmp$ Rscript h5ex.R > [1] TRUE > edd at bud:/tmp$ > > it all works if you just add `library(methods)` as seen in the first line. > > For what it is worth, littler's r does not need that as it loads methods > just > like R itself does avoiding the confusion: > > edd at bud:/tmp$ cat h5ex2.R > library(h5) > name <- tempfile() > f <- h5file(name) > file.remove(name) > edd at bud:/tmp$ r h5ex2.R > edd at bud:/tmp$ > > Dirk > > -- > http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org >[[alternative HTML version deleted]]