Can we choose library folder automatically based on R version?

Hello there,

I was trying to have RStudio picked the library location automatically based on R version. I put these lines in either .Renviron or .Rprofile but it didn't work

if(grepl("3.6", strsplit(version[['version.string']], ' ')[[1]][3])){
    R_LIBS_USER="~/R/win-library/R3"
} else {
    R_LIBS_USER="~/R/win-library/R4"
}

Anyone tried to do this before and succeeded?

Thank you!

You are trying to set an environmental variable so you can try with this

if(grepl("3.6", strsplit(version[['version.string']], ' ')[[1]][3])){
    Sys.setenv('R_LIBS_USER'="~/R/win-library/R3")
} else {
    Sys.setenv('R_LIBS_USER'="~/R/win-library/R4")
}

But you could also modify the Rprofile.site file on each R version to avoid checking for this.

1 Like

Thanks. How can I setup .Rprofile file separately for each R version?

I was talking about Rprofile.site not .Rprofile but actually a better fit for this would be to set R_LIBS_USER in your Renviron.site file, this file doesn't exist by default but you can create it at $R_HOME/etc/Renviron.site for each R version.

Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-40~1.0"
1 Like

Yes this is the way. Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.