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

后端 未结 15 2648
青春惊慌失措
青春惊慌失措 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 07:03

    If you want to change your library path permanently (without calling .libPath() every time when entering in R, this works for me:

    1. create .Rprofile under your home directory. (~/.Rprofile)

    2. type .libPaths(c( .libPaths(), "your new path" )) in .Rprofile file, save.

    3. open R (any directory) and check, just type .libPaths(), you can find your libaray path is updated!

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

    I managed to solve the problem by placing the code in the .Rprofile file in the default working directory.

    First, I found the location of the default working directory

    > getwd()
    [1] "C:/Users/me/Documents"
    

    Then I used a text editor to write a simple .Rprofile file with the following line in it

    .libPaths("C:/software/Rpackages")
    

    Finally, when I start R and run .libPaths() I get the desired output:

    > .libPaths()
    [1] "C:/software/Rpackages"               "C:/Program Files/R/R-2.15.2/library"
    [3] "C:/Program Files/RStudio/R/library"
    
    0 讨论(0)
  • 2020-11-22 07:05

    If your default package library has been changed after installing a new version of R or by any other means, you can append both the libraries to use all the packages with the help of the commands below. Get the existing library path :

    .libPaths()

    Now,set the existing and the old path :

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

    Hope it helps.

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

    The proper solution is to set environment variable R_LIBS_USER to the value of the file path to your desired library folder as opposed to getting RStudio to recognize a Rprofile.site file.

    To set environment variable R_LIBS_USER in Windows, go to the Control Panel (System Properties -> Advanced system properties -> Environment Variables -> User Variables) to a desired value (the path to your library folder), e.g.

    Variable name: R_LIBS_USER 
    Variable value: C:/software/Rpackages  
    

    If for some reason you do not have access to the control panel, you can try running rundll32 sysdm.cpl,EditEnvironmentVariables from the command line on Windows and add the environment variable from there.

    Setting R_LIBS_USER will ensure that the library shows up first in .libPaths() regardless of starting RStudio directly or by right-clicking an file and "Open With" to start RStudio.

    The Rprofile solution can work if RStudio is always started by clicking the RStudio shortcut. In this case, setting the default working directory to the directory that houses your Rprofile will be sufficient. The Rprofile solution does not work when clicking on a file to start RStudio because that changes the working directory away from the default working directory.

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

    just change the default folder for your R libraries in a directory with no Administrator rights, e.g.

    .libPaths("C:/R/libs")
    
    0 讨论(0)
  • 2020-11-22 07:11

    Since most of the answers here are related to Windows & Mac OS, (and considering that I also struggled with this) I decided to post the process that helped me solve this problem on my Arch Linux setup.

    Step 1:

    • Do a global search of your system (e.g. ANGRYSearch) for the term Renviron (which is the configuration file where the settings for the user libraries are set).
    • It should return only two results at the following directory paths:
      1. /etc/R/
      2. /usr/lib/R/etc/
        NOTE: The Renviron config files stored at 1 & 2 (above) are hot-linked to each other (which means changes made to one file will automatically be applied [ in the same form / structure ] to the other file when the file being edited is saved - [ you also need sudo rights for saving the file post-edit ] ).

    Step 2:

    • Navigate into the 1st directory path ( /etc/R/ ) and open the Renviron file with your favourite text editor.
    • Once inside the Renviron file search for the R_LIBS_USER tag and update the text in the curly braces section to your desired directory path.

      EXAMPLE:
      ... Change From ( original entry ):
      R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/4.0'}
      ... Change To ( your desired entry ):
      R_LIBS_USER=${R_LIBS_USER-'~/Apps/R/rUserLibs'}

    Step 3:

    • Save the Renviron file you've just edited ... DONE !!
    0 讨论(0)
提交回复
热议问题