Kirill Müller
2014-Feb-11 01:53 UTC
[Rd] $new cannot be accessed when running from Rscript and methods package is not loaded
Hi
Accesses the $new method for a class defined in a package fails if the
methods package is not loaded. I have created a test package with the
following single code file:
newTest <- function() {
cl <- get("someClass")
cl$new
}
someClass <- setRefClass("someClass")
(This is similar to code actually used in the testthat package.)
If methods is not loaded, executing the newTest function fails in the
following scenarios:
- Package "depends" on methods (scenario "depend")
- Package "imports" methods and imports either the setRefClass
function
(scenario "import-setRefClass") or the whole package (scenario
"import-methods")
It succeeds if the newTest function calls require(methods) (scenario
"require").
The script at
https://raw2.github.com/krlmlr/methodsTest/master/test-all.sh creates an
empty user library in subdirectory r-lib of the current directory,
installs devtools, and tests the four scenarios by repeatedly installing
the corresponding version of the package and trying to execute newTest()
from Rscript. I have attached the output. The package itself is on
GitHub: https://github.com/krlmlr/methodsTest , there is a branch for
each scenario.
Why does it seem to be necessary to load the methods package here?
Best regards
Kirill
Peter Meilstrup
2014-Feb-11 02:22 UTC
[Rd] $new cannot be accessed when running from Rscript and methods package is not loaded
Because "depends" is treated incorrectly (if I may place a value judgement on it). I had an earlier thread on this, not sure if any changes have taken place since then: http://r.789695.n4.nabble.com/Dependencies-of-Imports-not-attached-td4666529.html On Mon, Feb 10, 2014 at 5:53 PM, Kirill M?ller <kirill.mueller at ivt.baug.ethz.ch> wrote:> Hi > > > Accesses the $new method for a class defined in a package fails if the > methods package is not loaded. I have created a test package with the > following single code file: > > newTest <- function() { > cl <- get("someClass") > cl$new > } > > someClass <- setRefClass("someClass") > > (This is similar to code actually used in the testthat package.) > > If methods is not loaded, executing the newTest function fails in the > following scenarios: > > - Package "depends" on methods (scenario "depend") > - Package "imports" methods and imports either the setRefClass function > (scenario "import-setRefClass") or the whole package (scenario > "import-methods") > > It succeeds if the newTest function calls require(methods) (scenario > "require"). > > The script at https://raw2.github.com/krlmlr/methodsTest/master/test-all.sh > creates an empty user library in subdirectory r-lib of the current > directory, installs devtools, and tests the four scenarios by repeatedly > installing the corresponding version of the package and trying to execute > newTest() from Rscript. I have attached the output. The package itself is on > GitHub: https://github.com/krlmlr/methodsTest , there is a branch for each > scenario. > > Why does it seem to be necessary to load the methods package here? > > > Best regards > > Kirill > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Dirk Eddelbuettel
2014-Feb-11 02:31 UTC
[Rd] $new cannot be accessed when running from Rscript and methods package is not loaded
On 11 February 2014 at 02:53, Kirill M?ller wrote:
| Why does it seem to be necessary to load the methods package here?
"Just use littler (TM pending)".
It (auto-)load methods automagically, thanks to Jeff Horner. See below.
edd at max:~$ chmod 0755 /tmp/kirill.r
edd at max:~$ /tmp/kirill.r
[1] "refObjectGenerator"
attr(,"package")
[1] "methods"
edd at max:~$ cat /tmp/kirill.r
#!/usr/bin/r
newTest <- function() {
cl <- get("someClass")
cl$new
}
someClass <- setRefClass("someClass")
print(class(someClass))
edd at max:~$
For Rscript, you still need to load it explicitly like any other packages you
want to use.
Dirk
PS New littler release pending in a few days or weeks. The Github repo is
current and working.
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com