简体   繁体   中英

How to fix 'Unable to install packages' error while trying to run R script through command line?

I am trying to run an R script through the command line. My R script contains statement for inclusion of library and some methods of the same library as given here. The command line statement I am using to run the code is as below.

Test.R

library("readxl")
sales <- read_xlsx("<file path>", sheet = 1)

Commands:

cd "C:\Users\Debasish.sena\Desktop\out"
"C:\Program Files\R\R-3.2.2\bin\x64\Rscript.exe" -e "install.packages('readxl', repos = 'https://cran.us.r-project.org')"
"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH "C:\Users\Debasish.sena\Documents\Test.R" "C:\Users\Debasish.sena\Desktop\out\Test.Rout"

While executing the command I am getting the error as below: Warning in install.packages("readxl", repos = "https://cran.us.r-project.org") : 'lib = "C:/Program Files/R/R-3.2.2/library"' is not writable Error in install.packages("readxl", repos = "https://cran.us.r-project.org") : unable to install packages Execution halted

I am expecting the R script to be executed and the results stored in 'sales' variable

I think you are not logged in as Administrator, or you are trying to install on a remote system. Can you print the result of .libPaths() ?

I had encountered a similar issue, I solved it as follows:

  1. I created an Environment variable called R_LIBS for my account (NOT System variable) as R_LIBS=C:\\Users\\W.Aftab\\Documents\\R\\win-library\\4.0
  2. Then I restarted the VS code as I was using its terminal to call Rscript.exe. you can restart the command prompt if you are using it to call Rscript.

That's it,

I tested it on 64-bit windows 10 enterprise.

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