27

Just saw this:

$ Rscript -e "sessionInfo()['basePkgs']"
$basePkgs
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "base"

$ R --vanilla --slave -e "sessionInfo()['basePkgs']"
$basePkgs
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"
[7] "base"

The methods package seems to be always available when running R, but not with Rscript. I suspect that this is to gain performance, but are there any practical implications besides the obvious? I'm asking because of a funny bug that is triggered by the presence or absence of the methods package.

krlmlr
  • 25,056
  • 14
  • 120
  • 217
  • You can always explicitly load a package by adding `library(methods)` to your .Rprofile – Carl Witthoft Oct 30 '13 at 11:28
  • 1
    related thread on r-devel: http://r.789695.n4.nabble.com/advise-on-Depends-tp4678930p4679079.html – GSee Oct 30 '13 at 11:45
  • 5
    `?Rscript` tells you why it omits the methods package, "The default for `Rscript` omits `methods` as it takes about 60% of the startup time." – Joshua Ulrich Oct 30 '13 at 12:34
  • @JoshuaUlrich: Thanks, this explains the "why". I have only consulted `man Rscript`. – krlmlr Oct 30 '13 at 12:39
  • 5
    If you are a package writer, it means that your package needs to `Depends: methods` in the DESCRIPTION file (see the comment by Chambers in the thread cited by @GSee), and `import("methods")` in the NAMESPACE file -- probably 'best practices' anyway. – Martin Morgan Oct 30 '13 at 14:02
  • [Related](http://stackoverflow.com/q/19468506/271616) – Joshua Ulrich Nov 13 '13 at 14:16

1 Answers1

24

According to ?Rscript the methods package isn't loaded because, "The default for Rscript omits methods as it takes about 60% of the startup time."

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 16
    This tiny speedup is a horrible reason to do something so dastardly. It causes various R code to run differently in unpredictable ways under `R` and `Rscript`. On my machine, `echo 7 | R` takes 0.15 seconds, and `Rscript -e 7` takes 0.07 seconds. Hardly something worth the spooky-action-at-a-distance it causes. – Ken Williams Nov 01 '17 at 20:00
  • 7
    ...and sorry for commenting on an old question & answer, but 3 scientists in my group just shared horror stories of losing days at a time discovering this behavior, so I wanted to come put a comment on the first applicable SO question that Google found for me. =) – Ken Williams Nov 01 '17 at 20:02