Rscript sees a different R version than the one installed - r

I am trying to call an R script from inside Python however in the 1st line of the R script I am calling the "lubridate" library which gives me the following error
Error in readRDS(pfile) : cannot read workspace version 3 written by R 4.0.2; need R 3.5.0 or newer Calls: library -> find.package -> lapply -> FUN -> readRDS Execution halted
After calling .libPaths() in Rstudio I find that the library directories are :
[1] "/zhome/c9/f/144817/R/x86_64-pc-linux-gnu-library/4.0" [2] "/appl/R/4.0.2-mkl2020/lib64/R/library"
However, I put the print(.libPaths()) command at the first line of my R script and then run Rscript from inside my Python code, and the path that gives me is the following:
[1] "/appl/R/3.2.2/lib64/R/library"
It seems that the .libPaths() sees different library directories when called from Rstudio and when called from inside Python withe the Rscript command.
Any advice here, how I could make it look to the right library location?

Related

Install package through batch file

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,
install.packages(new.packages,repos="https://cloud.r-project.org")
but your error message (in the comments) of
Installing package into 'C:/Users/user/AppData/Local/R/win-library/4.2' (as 'lib' is unspecified)
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror Calls: install.packages -> contrib.url
Aborted execution
indicates that it isn't seeing repos. (I suspect that you had another error when you had install.packages(..., repos=) and, because you were not looking at the .Rout file 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 using
options(repos = "https://cloud.r-project.org")
earlier in your batch file, and it should work.

R CMD build versus devtools::build() (Non-standard file/directory found at top level)

I am writing an R package and build and test it with:
Rscript -e "devtools::document()" && R CMD build . && Rscript -e "devtools::test();devtools::check()"
I get a note:
checking top-level files
Non-standard file/directory found at top level:
‘PosteriorBootstrap_0.0.1.tar.gz’
I get the note whether devtools::check() comes first or second.
This
thread suggests:
The note is telling you that you usually shouldn't have a file called
build in the top level of your package.
The tar.gz file is created by R CMD build and I get the same error even if I delete it before running it.
This
thread
suggests adding the tar.gz file to .Rbuildinore, which removes the note.
Another way to remove it is to run everything from devtools:
Rscript -e "devtools::document(); devtools::build(); devtools::load_all('.'); devtools::test(); devtools::check()"
And then I don't get that note.
What's the difference between R CMD build and devtools::build() and why does the former throw that note?
You are combining a number of steps that perform similar and/or competing functions. I would suggest reading this for a best-practice build and check workflow.
When you run R CMD build it builds the package to the current directory, which is the top level package directory. Therefore, when you run your checks, it sees the .tar.gz file in the package root, which is a non-standard file to have in a package, thus the warning. devtools::build() is smart and builds the package to the package parent directory (regardless of where you are calling it from). Trying to call R CMD commands mixed with devtools functions can create issues, because devtools also calls R CMD commands, so you may be duplicating actions at various points in time or causing commands to be called in the incorrect order.
Per the link above, a best-practice workflow would be:
Rscript -e "devtools::document();devtools::check();devtools::build()"
called from the package root, and you avoid dealing with R CMD altogether. If you want to use R CMD it would look something like this:
Rscript -e "devtools::document()" && cd .. && R CMD build PosteriorBootstrap && R CMD check PosteriorBootstrap*.tar.gz
starting in the package root and then changing to the parent directory.

Error in library ("dtw") : there is no package called "dtw"

I am trying to run an R script from the Windows Command Prompt but is running into the following error:
Error in library("dtw") : there is no package called 'dtw'
Execution halted
The code that I typed is just RScript ...R
The R script actually runs in RStudio, and the address returned from .libPaths() function contains the "dtw" package.
Anyone has any idea on how to fix this?

Running r function with rscript fails after loading package "methods" [duplicate]

I've recently compiled R on Ubuntu Server 16.04. I'm attempting to run an R Script using the Rscript function, but I'm running into errors.
When I run the script using Rscript, the output shows the functions as() and new() from the methods package included in the R distribution cannot be found. When I run R interactively, I'm able to use these functions without any problem.
Any help would be tremendously appreciated!
It is a design bug in Rscript: it does not load methods on startup.
Two fixes:
Add library(methods) to your script, or
Do sudo apt-get install r-cran-littler and use r instead of Rscript.
r has been loading methods since day one as this behaviour of Rscript never really made sense.

How do I run Rcpp Hello World?

OK, so I have created an R package foo with function Rcpp.package.skeleton. I have also compiled the Hello World C++ file with
R CMD SHLIB foo/src/rcpp_hello_world.cpp
However, when I call rcpp_hello_world I get an error:
> source("foo/R/rcpp_hello_world.R")
> rcpp_hello_world()
Error in .Call("rcpp_hello_world", PACKAGE = "foo") :
"rcpp_hello_world" not available for .Call() for package "foo"
Any clues?
"Package skeleton" implies that you are supposed to follow the creation of a (simple, skeleton) package with (optionally) building the package (into a tar.gz) as well as installing it.
Once installed you can load it and then you can in fact execute the new function.
Alternatively, you can work on the fly via Rcpp Attributes and/or the inline package.
To run the "hello world" example, do the following:
Start R and install the Rcpp package by:
install.packages('Rcpp')
Generate Rcpp template, in R:
Rcpp.package.skeleton("mypackage")
Next, create an archive for the package:
R CMD build mypackage
Exit R. You should see a folder "mypackage" generated. Type the following to check the package:
R CMD check mypackage
Now, you will see an archive mypackage_1.0.tar.gz. Install it:
R CMD INSTALL mypackage_1.0.tar.gz
Let's run the package in R. Start R and do the following:
library('mypackage')
rcpp_hello_world() # Try the C++ function generated in the template
[[1]]
[1] "foo" "bar"
[[2]]
[1] 0 1

Resources