库是不可写的

我在 Ubuntu 机器上安装 R 3.0.2(2013.09-25)软件包时遇到了这个问题:

install.packages("randomForest")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages :
'lib = "/usr/local/lib/R/site-library"' is not writable

怎么解决呢?

129336 次浏览

It means exactly what it says. You don't have write permission in that folder. Either you need to change the permissions for that folder, or change the R library location.

If you are using R with RStudio, rather than starting RStudio with tray icon, start Rstudio or R with command line using sudo rstudio or sudo R.

It will solve your problem for sure, it works for me. It requires sudo privilege to write something in installation directory.

For R version 3.2.2 (2015-08-14) this problem should be dealt with since R suggests within the installation process a different path to store your R libraries. The installation looks like this: (Here 'random' is used as an example package)

install.packages('random')


Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("random") :
'lib = "/usr/local/lib/R/site-library"' is not writable


Would you like to use a personal library instead?  (y/n) y


Would you like to create a personal library
~/R/pc-linux-gnu-library/3.2
to install packages into?  (y/n) y

So during the installation answering both questions with 'y' should install the package correctly.

Update 18/01/19

In case you don't want to store your R packages in an additional file:

As Antoine-Sac and Robert TheSim point out you can add yourself to the staff group in order to be able to write to 'site-library'. (Click on the names to see their important additions)

Before this update I mentioned in this comment the option of changing the permission of the folder 'site-library' using 'chmod o+w' in order to be able to write to it. Assuming security issues but unable to tell at the time I warned about it but have primarily been waiting for somone to clear this up. Antoine-Sac and Robert TheSim have done so in the meantime. Thanks!

You can change permission to 'site-library' and all included directories.

sudo chmod 777 -R /usr/local/lib/R/site-library

add yourself to the group called 'staff'

sudo usermod -a -G staff your_user_name

replace your_user_name with your login username, then logoff and relogin.

DO NOT use chmod 777 which is a breach of security and btw. a complete non-sense!!!

If you are on Windows, you can run R (or RStudio) as an administrator.

Close your R, then go to the R or RStudio icon, right-click and "open as administrator". It works perfectly, all error messages while installing packages are gone forever.

For someone who tried to use install.packages() with multiple packages like this:

install.packages("vcd","vcdExtra","plyr")

and got similar warning:

Warning in install.packages :
'lib = "vcdExtra"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) cancel
Error in install.packages : unable to install packages

You should put the package names in a vector:

install.packages(c("vcd","vcdExtra","plyr"))

as the second parameter in install.packages() is lib.

The problem is that the default install location is a place where you do not have write permissions.

The solution is to use an install location where you do have write permissions.

Specifically, I'd suggest using the following commands to create a personal library folder in a place that doesn't require special permissions and that will automatically be detected the next time you startup R:

dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE)  # create personal library
.libPaths(Sys.getenv("R_LIBS_USER"))  # add to the path


install.packages("randomForest")  # install like always
library(randomForest)  # use library like always

The call to dir.create follows the suggestion in this faq to create a folder named according to Sys.getenv("R_LIBS_USER"). This is a good choice since it will be found on the next startup of R so you will be able to use install.packages and library without specifying special locations. The .libPaths function call lets us avoid restarting R by immediately adding the new folder to the library path. The first two lines are only needed if you do not yet have a personal library created, but there is no harm in running them repeatedly. After that, installing packages and using libraries can be done as usual.

If you are using OS windows 10 then maybe Ransomware protection is on. You need to disable that.

I was facing the same problem. I had the access to write. but suddenly it stopped. I couldn't install the packages. Disabling Ransomware protection worked for me.

The "XX" is not writable error can also mean that the library path you're providing does not exist.

Use sudo to Rscript code. I have same error fixed using sudo Rscript filename.R

Error

$ Rscript babynames.R
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("babynames") :
'lib = "/usr/local/lib/R/site-library"' is not writable
Error in install.packages("babynames") : unable to install packages
Execution halted

Fix

$ sudo Rscript babynames.R
[sudo] password for abhay:
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘cli’, ‘glue’, ‘utf8’, ‘fansi’, ‘lifecycle’, ‘magrittr’, ‘pillar’, ‘pkgconfig’, ‘rlang’, ‘vctrs’, ‘tibble’

Maybe can try sudo chmod +777 #nameoflib It works for me