How to Change .libPaths() permanently in R?

前端 未结 4 1440
無奈伤痛
無奈伤痛 2020-12-01 07:52

Whenever I change the library path order using the .libPaths() function, it reverts back to the default if I restart R. How can I change this permanently? I am

4条回答
  •  臣服心动
    2020-12-01 08:08

    On Mac, I find that in a 'fresh' installation there is no user library and packages are all installed in the version-specific system library. Eventually, a user library is created (possibly accidentally or via an RStudio prompt) and is set to the default location for future package installations. So, if you're not paying close attention you end up with some packages installed in the system library and some in the user library.

    The user library is listed first and the system library is listed second:

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

    I like to keep everything in one place (the system library), so to accomplish this (permanently) I do the following:

    In a Terminal window, create a .Rprofile file in your user directory:

    nano ~/.Rprofile
    

    In that file, add the following line of R code:

    .libPaths( c(.libPaths()[2], .libPaths()[1]) )
    

    Alternatively, add a similar line of R code but with hardcoded paths:

    .libPaths( c("/Library/Frameworks/R.framework/Versions/3.6/Resources/library", "/Users//Library/R/3.6/library") )
    

    Replace with your actual username. Then Ctrl-O (save) and Ctrl-X (exit) that file.

    Either line switches the order of the paths listed above, so that path [2] (system) is first (Default) and path [1] (user) is second.

    Importantly, by putting this line of code in your user's .Rprofile it will be run every time R starts and you (hopefully) won't have to worry about this again.

提交回复
热议问题