简体   繁体   中英

Prevent R from executing code in R profile when installing packages

I have some code in my R profile that I've found interferes with package installation. For example, I like to automatically load devtools when I'm working on packages, so I have this

if (file.exists("DESCRIPTION")) {
    try({suppressMessages(library(devtools))})
}

However, I've found that this interferes with package installation, I get errors like ERROR: lazy loading failed for package 'rlang' . If I comment out the loading of devtools in the R profile, the package installs without error.

Is there any way to check if.Rprofile is being executed during package installation so that I could put that condition in the if and stop devtools from loading at inappropriate times?

I'd generally recommend against putting such code into your ~/.Rprofile ! — The ~/.Rprofile should contain only general configuration that is always valid. For further customisation, use a project-local ~/.Rprofile instead.

However, I would make one exception to the above guideline, because one useful distinction you can make is to only execute certain code in interactive sessions:

if (interactive()) {
    if (file.exists("DESCRIPTION")) {
        try({suppressMessages(library(devtools))})
    }
}

.Rprofile files are sourced on R startup. They won't be called later by other functions so you can't use the checking logic during install.packages() .

For this reason loading packages using RProfile is not always advisable as it can make your code non-reproducible. However, as a helper package, devtools is probably a good exception (ignoring your particular problem here).

If you use base R only you can tell R not to load Rprofile during startup by using the --vanilla or --no-init-file arguments.

If you are using RStudio one workaround would be to use project level Rprofile files. Rstudio starts a new R session for each project, and loads the user Rprofile from the project root. If it then can't find that it will attempt to load the user RProfile.

  • Create a new project and within the the project options tick the option to disable the Rprofile loading.
  • Whenever you want to install a package switch to this project.

One caveat is to make sure you are running this as the only project session open, having multiple RStudio sessions open when installing new packages can lead to problems without putting the Rprofile in the mix.

Rstudio article on startup files https://support.rstudio.com/hc/en-us/articles/360047157094-Managing-R-with-Rprofile-Renviron-Rprofile-site-Renviron-site-rsession-conf-and-repos-conf

Base R documentation on R initialization https://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html

A diagram of the R startup flow and some commentary on use of renviron and rprofile https://rstats.wtf/r-startup.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM