Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use command line arguments with knitr source file?

Tags:

r

knitr

I have an R script that accepts command line arguments that I want to convert to an Rtex or Rnw file to be knitted with a command like:

knit('myScript.Rtex "-args arg1=a arg2=b"')

but I cannot figure out how to get my command line arguments into myScript.Rtex [for evaluation therein by args <- commandArgs()]. I am trying to avoid writing temporary files that could contain the same information and be read by myScript.Rtex, because that seems unnecessarily untidy. Please can somebody provide a suggestion/solution?

So, long story short, how best to get information into a chunk in a .Rnw file that is equivalent (or at least similar) to processing command line arguments in a .R file?

Edit: Incorporating code from comments below

I want to use them in the computation. For example, I had an R script run like

R CMD BATCH --slave --no-save --no-restore "--args input=blah" myScript.R

Now I'd like to pass the same argument to myScript.Rnw, which is a version of myScript.R, but interspersed LaTeX and R code chunks. So the broader issue is simply that for knitr report processing to be useful for me I need a way to change the data that the report is run on. I have a workaround - but I'd sure like to know if I can pass command line arguments to a chunk in my .Rnw file. Right now I cannot do that.

This is my workaround:

#!/bin/bash
if [ $# -ne 2 ];
  then ` echo "Usage: $0 refdir cmpdir" exit 1`
fi
export CSPP_VIIRS_EDR_REFERENCE_DIR=$1
export CSPP_VIIRS_EDR_COMPARISON_DIR=$2
script=$(dirname $0)'/viirs_edr_UseR_compare.Rnw'
R --slave --no-save --no-restore -e "require(knitr); knit('$script')" &> viirs_edr_UseR_compare.log
pdflatex viirs_edr_UseR_compare.tex
exit 0

I use Sys.getenv() in *viirs_edr_UseR_compare.Rnw* to extract parameters I want to pass, e.g., CSPP_VIIRS_EDR_REFERENCE_DIR.

like image 473
jim davies Avatar asked Jul 08 '13 18:07

jim davies


2 Answers

You could write a R script instead of a bash one:

my_script_launcher.R

library(knitr)
args <- commandArgs(TRUE)
if (length(args) < 2) stop("Bad args, usage refdir cmpdir")

refdir <- args[1]
cmpdir <- args[2]

knit2pdf('my_script.Rnw')

my_script.Rnw

\documentclass{article}
\begin{document}
refdir=\Sexpr{refdir}
cmpdir=\Sexpr{cmpdir}
\end{document}

Then you launch it like this:

Rscript my_script_launcher.R foo bar
like image 168
Karl Forner Avatar answered Sep 18 '22 19:09

Karl Forner


I had the same problem and ended up using the following commands (using make):

# set parameters
P1=some parameter
P2=some other parameter

# top target
all : report.html

# convert Markdown to HTML
%.html : %.md
    Rscript -e 'require(markdown);markdownToHTML("$<","$@")'

# knit Rmd passing parameters
%.md : %.Rmd
    Rscript -e 'param1<-$(P1);param2<-$(P2);require(knitr);knit("$<")'

In report.Rmd I can use str(c(param1,param2)) in a code chunk and running make will produce an HTML file containing chr [1:2] "some parameter" "some other parameter".

If you are not using make, just specify how you want to knit and where you are getting the parameters from and I can help you to adjust my solution to your needs.

like image 33
mschilli Avatar answered Sep 19 '22 19:09

mschilli