I am trying to share some R code with a few colleagues that are not familiar with R at all. To avoid them trying to understand R and running a script I created a batch file to just run the file without them doing anything, just double-clicking on the ".bat" file.
I've tried it and it works perfectly fine. But then I added a few lines in my Rscript to make sure the needed libraries were installed and, if not, to install them. That is when my batch file stopped working.
I've checked with another R script that just does install.packages(package_name) and the result I got is: the batch file does nothing.
So how can I run a R script that does install libraries through a batch file??
My atempt:
My extremely simple batch file:
"C:\Program Files\R\R-4.2.1\bin\R.exe" CMD BATCH "C:\Users\user\OneDrive\Documents\SomeFolder\test.R"
EXIT
And my "test.R" script:
list.of.packages <- c("lubridate","tidyverse","Z10")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
I also tried install.packages(new.packages,repos="https://cloud.r-project.org") but it doesn't work either.
Note: I know my code works because if I run it inside R it does the job.
Not sure why this doesn't work,
but your error message (in the comments) of
indicates that it isn't seeing
repos. (I suspect that you had another error when you hadinstall.packages(..., repos=)and, because you were not looking at the.Routfile you didn't see that error. But I don't know for sure.)If the
install.packages(..., repos=)isn't working, you can always set it usingearlier in your batch file, and it should work.