[R] Why would something work in R but not Rscript?

Henrik Bengtsson hb at biostat.ucsf.edu
Tue Dec 16 00:51:21 CET 2014


On Mon, Dec 15, 2014 at 9:44 AM, Jeff Hansen <dscheffy at gmail.com> wrote:
> Thanks Henrik! Adding the line
>
> library("methods")
>
> to the list of required libraries did indeed solve the problem. My next
> comment will show my naivety when it comes to R dependency management, but I
> noticed that including `library("rJava")` in place of methods also solves
> the problem. This confuses me because I would think that RWeka depends on
> rJava so I don't understand why transitive dependencies wouldn't
> automatically be loaded -- something for me to research more when I have the
> chance.

The naming of field "Depends" in DESCRIPTION is a bit unfortunate and
misleading, since the package depends all all packages under "Depends"
and "Imports" on order for it to be loaded.  The reason for the name
is historical - it predates namespaces and was coined at a time when
"Imports" didn't exists.

If you look at http://cran.r-project.org/web/packages/RWeka/index.html,
 or packageDescription("RWeka"), you'll find that RWeka *imports*
rJava, but it does not *attach* it.  A package listed under "Depends"
will be *loaded* and *attached* whereas a package under "Imports" will
only be *loaded*.  An attached package makes its API available via
search() and therefore also to the user at the R prompt, whereas a
package that is only loaded, will not be available this way.

Next, since RWeka only lists rJava under Imports, rJava is only
*loaded*.  In turn, whatever package rJava depends on ("Depends" or
"Imports" etc) will only be *loaded*.  In other words, it does not
matter that rJava has "methods" under "Depends" - the latter will
still only be *loaded*.


EXAMPLE:

$ Rscript -e "library('RWeka'); sessionInfo()"
R Under development (unstable) (2014-12-10 r67152)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  base

other attached packages:
[1] RWeka_0.4-23

loaded via a namespace (and not attached):
[1] grid_3.2.0         methods_3.2.0      rJava_0.9-6        RWekajars_3.7.11-1
[5] tools_3.2.0

You can explicitly load (the namespace of a) package (emulating what
happens with Imports) by:

$: Rscript -e "loadNamespace('rJava'); sessionInfo()"
<environment: namespace:rJava>
R Under development (unstable) (2014-12-10 r67152)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  base

loaded via a namespace (and not attached):
[1] methods_3.2.0 rJava_0.9-6   tools_3.2.0

and compare it to when you attach a package:

Rscript -e "library('rJava'); sessionInfo()"

[HB-X201]{hb}: Rscript -e "library('rJava'); sessionInfo()"
Loading required package: methods
R Under development (unstable) (2014-12-10 r67152)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] methods   stats     graphics  grDevices utils     datasets  base

other attached packages:
[1] rJava_0.9-6

loaded via a namespace (and not attached):
[1] tools_3.2.0

Look at where 'methods' ends up.

/Henrik

>
> Thanks for the help -- sadly it took me a month to see it as these responses
> were automatically diverted to a folder using filter logic I set up a few
> years ago and forgot about...
>
> On Wed, Nov 19, 2014 at 2:57 PM, Henrik Bengtsson <hb at biostat.ucsf.edu>
> wrote:
>>
>> When using Rscript, the 'methods' package is not loaded/attached by
>> default, which it is when you use R.  See ?Rscript for details.  For
>> any scripts intended for batch usage, the safest is to only assume
>> that 'base' is attached, but nothing else.
>>
>> /Henrik
>>
>> On Wed, Nov 19, 2014 at 10:03 AM, Ben Tupper <ben.bighair at gmail.com>
>> wrote:
>> > Hi,
>> >
>> > On Nov 19, 2014, at 11:48 AM, Jeff Hansen <dscheffy at gmail.com> wrote:
>> >
>> >> I have a script that uses RWeka (and consequently rJava).  When I run
>> >> it in Rstudio everything works fine. When I run it with `R CMD BATCH`,
>> >> everything also works fine. However, when I run it with Rscript, I get
>> >> the following error:
>> >>
>> >> Error in FUN(X[[1L]], ...) :
>> >>  object is not a Java object reference (jobjRef/jarrayRef).
>> >> Calls: evaluate_Weka_classifier -> t -> sapply -> lapply -> FUN
>> >> Execution halted
>> >>
>> >> The following is a very simple toy script that you can run to produce
>> >> the results:
>> >>
>> >> library("RWeka")
>> >> result <- c(TRUE,FALSE,TRUE,FALSE,TRUE)
>> >> observation <- c(TRUE,FALSE,TRUE,FALSE,TRUE)
>> >> df <- data.frame(result,observation)
>> >> j48 <- J48(result ~ .,data=df)
>> >> evaluate_Weka_classifier(j48)
>> >>
>> >> Save that to a file called help.R and run
>> >>
>> >> R CMD BATCH help.R
>> >>
>> >> Check the output file help.Rout and you should see no errors. Now try
>> >> running it from:
>> >>
>> >> Rscript help.R
>> >>
>> >
>> > You might try using the the --vanilla option for each.  At least then
>> > you can rule out that something is being restored in the session of one but
>> > not the other.
>> >
>> > R --vanilla CMD BATCH help.R
>> > Rscript --vanilla help.R
>> >
>> > Cheers,
>> > Ben
>> >
>> >
>> >> And you should see the error I've pasted above.
>> >>
>> >> I have consulted (and will continue to consult) the literature, but
>> >> the manuals tend to answer how usage differs between the two commands
>> >> rather than going into implementation details. I imagine there's a
>> >> difference in how environments get loaded and I just need to adjust
>> >> something on the Rscript side.
>> >>
>> >> I'm working on a Mac (OSX) running R 3.1.0, but I get the same results
>> >> when I run everything from a Centos 6.4 virtual machine (headless)
>> >> with R 3.1.1 installed.
>> >>
>> >> Thanks for any help!
>> >>
>> >> ______________________________________________
>> >> R-help at r-project.org mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>> >> PLEASE do read the posting guide
>> >> http://www.R-project.org/posting-guide.html
>> >> and provide commented, minimal, self-contained, reproducible code.
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list