0

I believe I have found an incorrect statement in the documentation of the defaultPackages option. In R 4.2.0, it reads (on pg. 438 of fullrefman.pdf)

     ‘defaultPackages’: the packages that are attached by default when
          R starts up.  Initially set from value of the environment
          variable ‘R_DEFAULT_PACKAGES’, or if that is unset to
          ‘c("datasets", "utils", "grDevices", "graphics", "stats",
          "methods")’.  (Set ‘R_DEFAULT_PACKAGES’ to ‘NULL’ or a
          comma-separated list of package names.)  It will not work to
          set this in a .Rprofile file, as its value is consulted
          before that file is read.

My question concerns the last sentence - actual behavior in R 3.4+ seems to contradict this. Here is a Dockerfile to demonstrate

# Dockerfile
FROM rocker/tidyverse

RUN echo "Example 1: Regular defaultPackages" | tee -a out.log
RUN R --quiet -e "search()" | tee -a out.log

RUN echo "Example 2: defaultPackages set to empty" | tee -a out.log
RUN echo "options(defaultPackages = c())" | tee ~/.Rprofile
RUN R --quiet -e "search()" | tee -a out.log

RUN echo "Example 3: defaultPackages appended with tidyverse" | tee -a out.log
RUN echo "options(defaultPackages = c(getOption('defaultPackages'), 'tidyverse'))" | tee ~/.Rprofile
RUN R --quiet -e "search()" | tee -a out.log

CMD cat out.log

# out.log
Example 1: Regular defaultPackages
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> 
> 
Example 2: defaultPackages set to empty
> search()
[1] ".GlobalEnv"      "package:methods" "Autoloads"       "package:base"   
> 
> 
Example 3: defaultPackages appended with tidyverse
> search()
 [1] ".GlobalEnv"        "package:forcats"   "package:stringr"  
 [4] "package:dplyr"     "package:purrr"     "package:readr"    
 [7] "package:tidyr"     "package:tibble"    "package:ggplot2"  
[10] "package:tidyverse" "package:stats"     "package:graphics" 
[13] "package:grDevices" "package:utils"     "package:datasets" 
[16] "package:methods"   "Autoloads"         "package:base"     

Am I misunderstanding something?

dusky
  • 101
  • 4

2 Answers2

0

R 4.2.0, Ubuntu 22.04

edd@rob:~$ R --vanilla --quiet -e 'search()'
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> 
> 
edd@rob:~$ 

I have stats. Maybe something at your end is different?

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Hmm, so when I use `search()` in R interactively I get only `[1] ".GlobalEnv" "package:methods" "Autoloads" "org:r-lib" "package:base"`. But when I use your exact command, I get the same result as you. So it's something about the `--vanilla` flag or something about interactive R. – dusky Jun 09 '22 at 20:59
  • It may have to do with your default files in `RHOME/etc`. Also `org:r-lib` seems unusual. What OS do you use, and where did you get your version from? – Dirk Eddelbuettel Jun 09 '22 at 21:05
  • Right, so the `--vanilla` flag ignores `.Rprofile`, so it makes sense that it shows `stats`. I'm confused that `?options` claims that I can't change `defaultPackages` in `.Rprofile`, because `defaultPackages` is consulted prior to `.Rprofile` loading, but I clearly can change it. Can you reproduce being able to change `defaultPackages` in `.Rprofile`? – dusky Jun 09 '22 at 21:06
  • I'm on Ubuntu 20.04 and I downloaded R from the standard Rutter PPA. – dusky Jun 09 '22 at 21:07
  • I am also using your `r2u` PPA with `bspm`, if that makes any difference. – dusky Jun 09 '22 at 21:08
  • I use the same Ubuntu binaries (rebuilt from the Debian package I maintain) so it should really be the same. I do not tweak the files in `/etc/R/*` (linked to/from `/usr/lib/R/etc` much -- and what I do later in my personal `~/.Rprofile` is skipped by `--vanilla`. Also, I just tried as `root`, same output. "Something" must be different at your end, but I cannot tell what. Sorry! – Dirk Eddelbuettel Jun 09 '22 at 21:14
  • Could you try setting your root's `.Rprofile` to `options(defaultPackages = c())` and then run `R --quiet -e 'search()'`? I'm pretty sure `stats` won't show up then. – dusky Jun 09 '22 at 21:17
  • It seems that `org:r-lib` shows up when R is used interactively, but not with `R -e`. – dusky Jun 09 '22 at 21:28
  • No I do not get `org:whatever` in interactive mode. Same as above for me. – Dirk Eddelbuettel Jun 09 '22 at 22:12
  • The difference between using the env.var and using `Rprofile` may be that I can avoid the (relatively expansive) package `methods` (which R used to not load by default ...) in the former case of using `R_DEFAULT_PACKAGES=NULL`. – Dirk Eddelbuettel Jun 09 '22 at 22:17
  • Well I'm stumped. Thank you for your time! – dusky Jun 10 '22 at 01:44
  • 1
    If your doubts remain after a back and forth with Dirk, who is an acknowledged R guru, You can post this on the R-dev mailing list where question of the accuracy of the documentation are often addressed. – IRTFM Jun 10 '22 at 02:11
0

I found an interpretation of the documentation that makes sense. Going to post this here for others that may be confused.

Consider the last sentence again

It will not work to set this in a .Rprofile file, as its value is consulted before that file is read.

There are two valid interpretations: 1) the word "this" refers to defaultPackages, 2) the word "this" refers to R_DEFAULT_PACKAGES.

In the first interpretation, the statement is inaccurate, as supplied by the examples in my OP.

In the second interpretation, the statement is correct (though I must say, I don't think this is the natural interpretation of this sentence). Dockerfile to demonstrate.

# Dockerfile
FROM rocker/tidyverse:3.4

RUN echo "Example 4: R_DEFAULT_PACKAGES set to empty in .Rprofile" | tee -a out.log
RUN echo "Sys.setenv('R_DEFAULT_PACKAGES' = '')" | tee ~/.Rprofile
RUN R --quiet -e "search()" | tee -a out.log

RUN echo "Example 5: R_DEFAULT_PACKAGES set to tidyverse in .Rprofile" | tee -a out.log
RUN echo "Sys.setenv('R_DEFAULT_PACKAGES' = 'tidyverse')" | tee ~/.Rprofile
RUN R --quiet -e "search()" | tee -a out.log

CMD cat out.log

# out.log
Example 4: R_DEFAULT_PACKAGES set to empty in .Rprofile
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> 
> 
Example 5: R_DEFAULT_PACKAGES set to tidyverse in .Rprofile
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"   
dusky
  • 101
  • 4