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

Duncan Murdoch murdoch.duncan at gmail.com
Tue Feb 11 19:26:31 CET 2014


On 11/02/2014 12:06 PM, Winston Chang wrote:
> 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
> noted.
>
> 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.

I think the idea is that the search list belongs to the user, and 
packages shouldn't be messing with it.  If the user doesn't have 
mypackage and methods in his/her search list, maybe there's a reason for 
that, and mypackage::foo shouldn't mess with it.

I think the real problem is that loading the mypackage namespace isn't 
enough to get the functions within it to work.

Duncan Murdoch


>
> 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.
> http://r.789695.n4.nabble.com/advise-on-Depends-td4678930.html
>
> -Winston
>
>
> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list