Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including Command Line Scripts with an R Package

I am interested in providing a command line interface to an R package called Slidify that I am authoring. It uses Rscript and I think that would make it cross-platform. The scripts are stored in the subdirectory inst/slidify. In order to use the script from any directory, I added its path to my .bash_profile as I am on a Mac.

My question is

  1. How should I handle installation of the script in an automated cross-platform way?
  2. How can I make sure that the file permissions are retained in this process?
  3. What should the shebang line for the script be? I am currently using

    #!/usr/bin/Rscript --vanilla --slave

I would appreciate pointers on how to handle this and any examples of R packages that already do it. Just to make sure, I am clear on how this would work, a user would be able to generate a slide deck from slides.Rmd by just running slidify generate slides.Rmd from the command line.

UPDATE:

Here is how I install it on a Mac from the command line. I use the excellent sub library by 37 signals to create the scripts.

echo "$(path/to/clidir/slidify init -)" >> ~/.bash_profile exec bash

Two follow up questions

  1. Can I package these commands into an R function install_slidify_cli?
  2. How can I mirror these commands for Windows users?
like image 638
Ramnath Avatar asked Nov 17 '12 20:11

Ramnath


People also ask

How do I run an Rscript from the command line?

Running R from the Command Line To open up the command prompt, just press the windows key and search for cmd. When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line.

How do I add a script to RStudio?

To start writing a new R script in RStudio, click File – New File – R Script. Shortcut! To create a new script in R, you can also use the command–shift–N shortcut on Mac.

How do I get command line arguments in R?

Use commandArgs(trailingOnly = TRUE) to obtain a vector of the command-line arguments that a program was run with.

What does Rscript -- vanilla mean?

Let's break it down line by line: #!/usr/bin/env Rscript --vanilla. Sometimes called a 'shebang', this line tells the Linux and MacOS command line interpreters (which both default to one called 'bash'), what you want to use to run the rest of the code in the file.


1 Answers

Lovin' slidify so would be glad to help.

But in short, you can't.

R packages simply cannot install outside of $R_HOME or the chosen library folder. Ship the script in the package, and tell users to copy it. If there was a better way, out littler package with predecessor / alternative to Rscript would long have used it, and roxygen / roxygen2 would also have shipped something.

like image 159
Dirk Eddelbuettel Avatar answered Oct 11 '22 00:10

Dirk Eddelbuettel