1

I have this simple script with most of the code extracted from this answer by Martin Morgan:

#!/usr/bin/Rscript
# sript name: testclass.R
# library(methods)
.A1 <- setClass("A1", representation(a="integer"))
.B1 <- setClass("B1", contains="A1", representation(b="integer"))

A1 <- function(a = integer(), ...) {
    .A1(a=a, ...)
}

B1  <- function(a=integer(), b=integer(), ...) {
    .B1(A1(a), b=b, ...)
}

b <- .B1()

inherits(b, "A1")

If I run the script with Rscript it produces this error:

user@localhost ~/test
  % chmod +x testclass.R                 

user@localhost ~/test
  % ./testclass.R                        
Error: could not find function "setClass"
Execution halted

If I uncomment the line library(methods) and run again, it works:

user@localhost ~/test
  % ./testclass.R                        
[1] TRUE

Or if I run like this, I don't need to import methods manually neither:

R --vanilla <testclass.R

So my question is, what is going on, and how can I avoid importing methods manually and run with Rscript successfully?

R versions:

 Rscript --version  
R scripting front-end version 3.3.1 (2016-06-21)
 R --version                                              
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

Update

As a workaround while waiting for a more informative answer: I can use /usr/bin/r from littler package instead of /usr/bin/Rscript and the error does not happen.

Community
  • 1
  • 1
biocyberman
  • 5,675
  • 8
  • 38
  • 50
  • http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html says that "the default for Rscript omits `methods` as it takes about 60% of the startup time." – Weihuang Wong Dec 12 '16 at 10:01
  • What's the downside of inserting `library(method)` in your scripts? You could also put it in your `.Rprofile` file and `method` will be loaded in any R session. However, I discourage it, since your scripts won't be portable (they will fail on systems that don't have `library(method)` in their `.Rprofile`). – nicola Dec 12 '16 at 10:08
  • You could change your shebang to `#!/usr/bin/Rscript --default-packages=methods`, but as @nicola mentioned, it seems more straightforward to just include `library(methods)`. – Weihuang Wong Dec 12 '16 at 10:10
  • @WeihuangWong The downside is: `methods` is not an obvious dependency when people want to use `Rscript`. I prefer a solution from the package development, when classes are defined in the package. – biocyberman Dec 12 '16 at 10:16
  • What is a "solution from the package development"? That `Rscript` doesn't import `method` is well documented, and anyone should test their code to see if it runs. The received error can be immediately detected and corrected, so don't see how this can be an issue. – nicola Dec 12 '16 at 10:26
  • @nicola I've been writing R script with Rscript for several years, during which I used many packages, and it is my first time to see this problem. My main purpose is to understand how other packages do not require manually import of `methods`, and propose a fix for the package I am using. This is the issue on github: https://github.com/ccagc/QDNAseq/issues/46 – biocyberman Dec 14 '16 at 14:04

1 Answers1

1

The methods package is always shipped with R, but as it is not always needed Rscript has opted to not load it by default. ?Rscript will tell you that this is to save startup time. Rstudio and R loads this library by default. If functions from the methods package are to be used in a program intended to be run using Rscript then the simple solution is to add the line:

library(methods)

in the script. This does no harm when run through Rstudio or R because the methods package was already loaded there. I routinely add library(methods) to all scripts that I know are going to be run with Rscript.

Rikard N
  • 427
  • 4
  • 16