Does RStudio run any additional Rprofile-like files during R startup (problems setting default libPaths)?

rstudio

#1

I want to change my default library paths. 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. I also know different workarounds to solve my immediate problem. 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.

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. I also added a .First function to see if the problem was happening between the Rprofile stage and the .First stage of startup.

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()
.First<- function(){
print("First")
print(.libPaths())
}

The code was indeed running. The library paths were still set correctly after running the .First() function. However, when I ran .libPaths() in the console, it again had the incorrect library paths. So I opened Rgui (I've done this with both x64 and i386 with the same results, since Rstudio opens x64 I'll only show the results there). The same startup code runs fine. Additionally, the library paths are correctly set when running .libPaths() from the console. Last, I checked the only other thing I know that runs after .First() during startup, which is the the .First.sys() function. 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 (so after default packages were loaded, so I don't think the problem can be caused by something happening during package initialization). In both Rstudio and Rgui they are still set correctly, but only in Rstudio have they been reset prior being able to run code in the console.

Below are screenshots to help show what I mean. So the questions are, why do I get different results in Rstudio? Is some other R code being run? Is there a way to log the code R is executing during startup or otherwise find other files being opened?


#2

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.

Most of this logic is written in R itself, so you can inspect it to see what RStudio is doing. Pick up the trail here: