[Rd] $new cannot be accessed when running from Rscript and methods package is not loaded

Winston Chang winstonchang1 at gmail.com
Tue Feb 11 18:06:53 CET 2014

To state the issue that Kirill raised in a different way... A package
with S4 or reference classes and Depends:methods can throw an error
when you do something as simple as this:
  Rscript -e "mypackage::foo()"

But this will work:
  Rscript -e "library(mypackage); foo()"

This is because when mypackage has Depends:methods, calling
mypackage::foo() merely loads mypackage, and doesn't attach it -- and
when this happens, the methods package is also loaded and not
attached. In this situation, you can get errors of the type that Dirk

In contrast, library(mypackage) causes methods to be attached, and so
you won't get those errors. This seems like a problem with the
behavior of the :: operator. Why should it be that when a package is
loaded, the packages listed in Depends are not attached? Presumably,
when someone lists a package in Depends instead of Imports, it's for a
good reason, as is the case here with the methods package.

In the following thread, it's stated that the defensive approach is to
use Depends:methods -- but that's still not enough for running the
code listed above.


On Mon, Feb 10, 2014 at 8:31 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
> 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
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

More information about the R-devel mailing list