Change R default library path using .libPaths in Rprofile.site fails to work

后端 未结 15 2645
青春惊慌失措
青春惊慌失措 2020-11-22 06:27

I am running R on Windows, not as an administrator. When I install a package, the following command doesn\'t work:

> install.packages(\"zoo\")
Installing          


        
相关标签:
15条回答
  • 2020-11-22 06:52

    I found what I think is a solution here (thank you Carl Schwarz at SFU) for adding a personal library that is permanently (you don't have to define it each session) recognized whether using R or Rstudio, and Rstudio treats it as the default on my Mac machine. I hadn't seen it laid out this explicitly on SO, so I summarized the steps they provided, for Windows and then for Mac.

    For a Windows 7 OS:

    1. Create a directory on the drive where you want to have your personal library, e.g. C:\User\Rlibs (or another that you have permissions to)

    2. Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings

    3. Click "New..." in the middle of the "Environmental Variables" window

    4. In the "New User Variable" window, type R_LIBS for the "Variable name", and the path to the personal library directory you created, e.g. C:\User\Rlibs

    5. Click OK and you should see the Variable/Value pair in the User variables window

    6. Click OK again

    Now when you start R (or Rstudio) and type the command .libPaths() you should see the personal library you created as well as the R system library.

    For Mac:

    1. In your "Home" or "username" directory create a folder called Rlibs

    2. Launch the Terminal application

    3. Type: echo "R_LIBS=~/Rlibs"> .Renvrion Make sure case matches.

    4. Type ls -a to see the full list of files in the directory, which should now include .Renvrion

    5. Verify that the .Renviron file has been set properly: more .Renviron

    Launch R/Rstudio and type .libPaths() and you should see the new path to your personal library.

    0 讨论(0)
  • 2020-11-22 06:53

    https://superuser.com/questions/749283/change-rstudio-library-path-at-home-directory

    Edit ~/.Renviron

    R_LIBS_USER=/some/path
    
    0 讨论(0)
  • 2020-11-22 06:55

    I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?

    .libPaths( c( .libPaths(), "~/userLibrary") )
    

    Or (and this will make the userLibrary the first place to put new packages):

    .libPaths( c( "~/userLibrary" , .libPaths() ) )
    

    Then I get (at least back when I wrote this originally):

    > .libPaths()
    [1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
    [2] "/Users/user_name/userLibrary"  
    

    The .libPaths function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv and Sys.setenv that report and alter the R environment variables have been split apart but .libPaths can either report or alter its target.

    The information about the R startup process can be read at ?Startup help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio

    In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library and .Library.site, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.

    Since Sys.getenv() returns the current system environment for the R process, you can see the library and other paths with:

    Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]
    

    The two that matter for storing and accessing packages are (now different on a Linux box):

    R_LIBS_SITE                          /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
    R_LIBS_USER                          /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/
    
    0 讨论(0)
  • I was looking into this because R was having issues installing into the default location and was instead just putting the packages into the temp folder. It turned out to be the latest update for Mcaffee Endpoint Security which apparently has issues with R. You can disable the threat protection while you install the packages and it will work properly.

    0 讨论(0)
  • 2020-11-22 06:58

    I've had real trouble understanding this. gorkypl gave the correct solution above when I last re-installed my OS & Rstudio but this time round, setting my environment variable didn't resolve.

    Uninstallled both R and Rstudio, creating directories C:\R and C:\Rstudio then reinstalled both.

    Define R_LIBS_USER user variable to your prefered directory (as per gorkypl's answer) and restart your machine for User variable to be loaded. Open Rstudio, errors should be gone.

    You can also use Sys.setenv() to modify R_LIBS_USER to the path of your alternative library which is easier and does not need to restart your computer.

    To see what R_LIBS_USER is set to: ?Sys.getenv()

    Reading help(Startup) is useful.

    0 讨论(0)
  • 2020-11-22 07:02

    I read the readme. In that they mentioned use .libPaths() in command line to check which paths are there. I had 2 library paths earlier. When I used the command .libpath("C:/Program Files/R/R-3.2.4revised/library") where I wanted, it changed the library path. When I typed in .libPaths() at the command line again it showed me the correct path. Hope this helps

    0 讨论(0)
提交回复
热议问题