Does RStudio run any additional Rprofile-like files during R startup?
I want to change my default library paths and I prefer to keep all my libraries in the site directory, but somehow the defaults for my last few installations of R on this computer are set the user directory. I know this an R concept, not an Rstudio one, and I am very familiar with the ?Startup help guide. However, this appears to a symptom of something larger, so I want to understand what is causing the issue rather than just obtaining a fix.
Testing Rprofile and Startup Files
I edited the Rprofile.site file to set new library paths, but when I open Rstudio and run .libPaths() it gives me the original paths. I wondered if the Rprofile.site file was not running, so I put in the following code to print some output to the console:
print("Reading Library Paths") .libPaths() print("Setting Library Paths") .libPaths(c("C:/Program Files/R/R-3.5.1/library","C:/Users/andre/Documents/R/win-library/3.5")) print("Reading New Library Paths") .libPaths()
The code was indeed running and the library paths were still set correctly after running the .First() function. I also added a .First function to see if the problem was happening between the Rprofile stage and the .First stage of startup. I opened the system Rprofile file in the base package, and editted .First.sys() to read out the library paths at the end of the function.
Comparison Between RStudio and Rgui
I opened Rgui and the same startup code runs fine; additionally, the library paths are correctly set when running .libPaths() from the console. In both Rstudio and Rgui they are still set correctly at the .First.sys() stage, but only in Rstudio have they been reset prior being able to run code in the console. The following table summarizes the observed behavior:
| Environment | Startup Result | Status in Console |
|---|---|---|
| Rgui | Code runs fine | Correctly set |
| RStudio | Code runs during startup | Reset prior to console access |
The Cause of Library Path Modification
So the questions are, why do I get different results in Rstudio? RStudio attempts to ensure that the first (default) library is writeable, so that package installations succeed. If it detects that the default library is not writeable, it will modify libPaths so that is. That's probably what's happening here.
Technical Details
Most of this logic is written in R itself, so you can inspect it to see what RStudio is doing. You can find the logic within the RStudio source code, specifically looking for the function:
- .rs.addFunction("ensureWriteableUserLibrary", function() )
Is there a way to log the code R is executing during startup or otherwise find other files being opened? You can pick up the trail at the RStudio GitHub repository to examine how SessionPackages.R handles these modifications.