9

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.

8
  • Thanks for your suggestion. I need to provide a solution called from bash with arguments passed through from the bash command line. So I think I will use the environment of that bash instance and pick up what I need with Sys.getenv(). It is a bit ugly, but I need to push on. Commented Jul 8, 2013 at 18:52
  • 2
    @jimdavies it is not clear what you want to do. looking at knit commandn it expected something like , ..knit(input, output,...) Commented Jul 8, 2013 at 18:56
  • this is the shell script I ship with knitr: github.com/yihui/knitr/blob/master/inst/bin/knit but it is not clear to me what you want to do with the --args -- do you want to pass them to knit(), or use them in your computation? Commented Jul 8, 2013 at 19:20
  • Use them in the computation. So, for example, if I had an R script that I used to run from the command line as Commented Jul 8, 2013 at 22:53
  • 2
    @jimdavies if you want to clarify anything, please just edit your original post; the comments here are pretty restrictive in terms of writing and reading code Commented Jul 9, 2013 at 7:00

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.