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.