简体   繁体   中英

How to reset or completely override .libPaths in R?

I want .libPaths() to return "/home/balter/R" "/home/balter/conda/envs/dada2/lib/R/library" .

This is what I'm getting:

> .libPaths()
[1] "/home/balter/R"                "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[4] "/usr/lib/R/library"           
> .libPaths(new='/home/balter/conda/envs/dada2/lib/R/library')
> .libPaths()
[1] "/home/balter/conda/envs/dada2/lib/R/library" "/usr/local/lib/R/site-library"              
[3] "/usr/lib/R/site-library"                     "/usr/lib/R/library"                         
> .libPaths(new=c("/home/conda/envs/dada2/lib/R/library", "/home/balter/R"))
> .libPaths()
[1] "/home/balter/R"                "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[4] "/usr/lib/R/library"           
> .libPaths(c("/home/conda/envs/dada2/lib/R/library", "/home/balter/R"))
> .libPaths()
[1] "/home/balter/R"                "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[4] "/usr/lib/R/library"           
> .libPaths(c("/home/conda/envs/dada2/lib/R/library", "/home/balter/R"))
> .libPaths()
[1] "/home/balter/R"                "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[4] "/usr/lib/R/library"           
> .libPaths(new=c("/home/conda/envs/dada2/lib/R/library", "/home/balter/R"))
> .libPaths()
[1] "/home/balter/R"                "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[4] "/usr/lib/R/library"            
> .libPaths(new="home/balter/R")
> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"       "/usr/lib/R/library"           
> .libPaths("home/balter/R")
> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"       "/usr/lib/R/library" 

I can't seem to clear out and reset the paths.

NOTE:

This is in RStudio Server free edition running in WSL Ubuntu 18.04 on localhost.

NOTE II:

@Dason pointed out a typo. Correcting it I have:

> .libPaths()
[1] "/home/balter/R/x86_64-pc-linux-gnu-library/3.4" "/usr/local/lib/R/site-library"                 
[3] "/usr/lib/R/site-library"                        "/usr/lib/R/library"                            
> .libPaths(c("/home/balter/conda/envs/dada2/lib/R/library", "~/R"))
> .libPaths()
[1] "/home/balter/conda/envs/dada2/lib/R/library" "/home/balter/R"                             
[3] "/usr/local/lib/R/site-library"               "/usr/lib/R/site-library"                    
[5] "/usr/lib/R/library"                         
> 

Is there a way to completely get rid of the defaults so that only the first to are included?

This is pretty old but there's no answer marked so here's my input:

I've just solved this exact problem by overriding a few of rstudio server's default R_* environment variables by defining a .Renviron in the root of the project directory.

# .Renviron example - set exclusive R library
R_LIBS=/path/to/your/envs/R/library/ # Set env's R library location
R_LIBS_USER=""                       # Unset user's local R library
R_LIBS_SITE=""                       # Unset default system R libraries
    # !EDIT! - Make sure the version of R you set to R_HOME is the one that RStudio-Server 
    #    is running. (I haven't been able to get the server to run Anaconda's version)
    #  When I changed R_HOME to the version in my conda env and the server was running 
    #    the system R, it broke all of the locally compiled packages.
R_HOME=/path/to/your/envs/R          # Reset R's home directory to the one in the env's home

In case you're not aware, the .Renviron is a script that's run when an R session starts up. As its name suggests, it's used specifically to set environment variables.

You can also run R code at startup in a .Rprofile script. The below example script will add a new path to your .libPaths but won't result in the removal of the libraries found by searching in the R_LIBS* directories.

# .Rprofile - set .libPaths
assign(".lib.loc", "/path/to/your/envs/R/library/", envir = environment(.libPaths))

You can cascade these config files from your project root, user home directory and R's home directory and they will be run preferentially in that order (but only the first encountered of each script type will be run).

I think the .Renviron is run after the .Rprofile (or the environment variables are processed later) because the changes I was making using the above .Rprofile script was always overwritten by the environment variables.

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