Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distributing R scripts that use external packages

Tags:

scripting

r

I am working within a small group of mostly non-technical users and would like to distribute an R script within the group. This uses 'RScript' in the shebang line, takes some command line arguments and produces a graphics file and some text as output.

I am wondering if there are ways to ensure that any external packages that the script uses (even something like optparse for parsing arguments) is available to the user. I expect that everyone in the group will be able to invoke the program from the command line by following instructions but asking people to start R and download and install dependencies might be too much. I've used py2exe or py2app for similar situations in Python, but I am wondering if there are equivalent ways or at least best practices for distributing R scripts.

like image 271
primelens Avatar asked Nov 20 '25 08:11

primelens


2 Answers

Create a package, declare the dependencies as Depends: in DESCRIPTION. That works, and is tested widely across OSs.

You can then also place the package in a local repository, making updates easier and possibly even automatic. I once wrote a company-internal package which in its own startup code checked if a newer version was available on the local repository.

like image 76
Dirk Eddelbuettel Avatar answered Nov 21 '25 21:11

Dirk Eddelbuettel


Dirk's given you the best practice solution (packages with dependencies) but the next-best-practice solution is probably to use require(foo) and test if the package is there, and then get it if not. Something like:

if(require(foo)){
 # foo loaded okay
 cat("we got foo\n")
}else{
 cat("uh oh, no foo. let's get it\n")
 install.packages("foo")
}

you might want to specify a CRAN repo in install.packages so the user isn't interrogated, and also to use some options on require to stop warnings.

like image 39
Spacedman Avatar answered Nov 21 '25 23:11

Spacedman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!