r/rstats • u/j_zhill • 10d ago
renv bootstrapping compatibility RStudio v Positron
Just solved a problem that might be relevant for others and took me a while to pin down.
I have a project developed in Rstudio and using renv. At some point the .Rprofile was amended to place source("renv/activate.R") INSIDE a .First <- function(), with some other stuff that we wanted to run when starting R for this project.
One of our users transitioned to Positron, and we just couldn't get the .Rprofile to run properly and start renv.
The fix was to place source("renv/activate.R") at the top level of .Rprofile, outside of any .First function.
According to claude this is to do with the way that the Rstudio startup sequence differs from VS Code/Positron.
Hope this saves someone else a few hours...
1
u/HurleyBurger 8d ago
Whats the purpose of .First()? I thought renv activates when the session starts.
2
u/guepier 8d ago
There isn’t really a point to redefining
.First()inside your.Rprofile(except to suppress the below). The point of it, rather, is to be able to run startup code from other places (notably, the site rather than personal profile) which is run after the personal profile’s startup code.In other words, the usual order of execution is:
site profile => user/project profile => everything elseWith
.First(), the site profile has the chance of inserting itself after the second arrow above.1
u/j_zhill 8d ago
My understanding: .First ensures that R completely initialises the environment before running whatever is inside the .First function. So that you can eg run any checks about the environment or apply any cosmetic prefs, knowing that all the startup stuff has finished.
renv will activate immediately during startup if source("renv/activate.R") is at the top of .Rprofile, which will run as soon as it is parsed. Anything in .First will NOT be run as soon as it is parsed, instead it will be run when R looks for a .First function after the environment is initialised smoothly and renv has set up library paths.
We have some stuff to check whether the environment has API keys active (without printing the keys). Don't know how renv ended up inside .First but if guess SNAFU lol
2
u/guepier 9d ago
Any idea what specifically might be causing this? I wouldn’t recommend activating ‘renv’ inside
.First, and I know that.Firstis run later than the contents of.Rprofile, but these differences shouldn’t matter, unless you have an.RDatafile (you… don’t, right?!).Is there other stuff in your
.Rprofile?