Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use command line arguments with knitr source file?
    primarykey
    data
    text
    <p>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:</p> <pre><code>knit('myScript.Rtex "-args arg1=a arg2=b"') </code></pre> <p>but I cannot figure out how to get my command line arguments into myScript.Rtex [for evaluation therein by <code>args &lt;- commandArgs()</code>]. 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?</p> <p>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?</p> <h2>Edit: Incorporating code from comments below</h2> <p>I want to use them in the computation. For example, I had an R script run like</p> <pre><code>R CMD BATCH --slave --no-save --no-restore "--args input=blah" myScript.R </code></pre> <p>Now I'd like to pass the same argument to <code>myScript.Rnw</code>, which is a version of <code>myScript.R</code>, 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.</p> <p>This is my workaround:</p> <pre><code>#!/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')" &amp;&gt; viirs_edr_UseR_compare.log pdflatex viirs_edr_UseR_compare.tex exit 0 </code></pre> <p>I use <code>Sys.getenv()</code> in *viirs_edr_UseR_compare.Rnw* to extract parameters I want to pass, e.g., <strong>CSPP_VIIRS_EDR_REFERENCE_DIR</strong>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload