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:
我有一个R脚本接受命令行参数,我想将其转换为Rtex或Rnw文件,使用如下命令编写:
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?
但我无法弄清楚如何将我的命令行参数放入myScript.Rtex [由args < - commandArgs()]进行评估。我试图避免编写可能包含相同信息的临时文件,并由myScript.Rtex读取,因为这似乎不必要的不整洁。有人可以提供建议/解决方案吗?
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?
那么,长话短说,如何最好地将信息放入.Rnw文件中与.R文件中处理命令行参数等效(或至少类似)的块中?
Edit: Incorporating code from comments below
I want to use them in the computation. For example, I had an R script run like
我想在计算中使用它们。例如,我有一个R脚本运行
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.
现在我想将相同的参数传递给myScript.Rnw,它是myScript.R的一个版本,但是散布了LaTeX和R代码块。因此,更广泛的问题只是针对编织器报告处理对我有用,我需要一种方法来更改运行报告的数据。我有一个解决方法 - 但我肯定想知道我是否可以将命令行参数传递给我的.Rnw文件中的一个块。现在我做不到。
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.
我在* viirs_edr_UseR_compare.Rnw *中使用Sys.getenv()来提取我想要传递的参数,例如CSPP_VIIRS_EDR_REFERENCE_DIR。
2 个解决方案
#1
4
You could write a R script instead of a bash one:
您可以编写R脚本而不是bash脚本:
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
#2
1
I had the same problem and ended up using the following commands (using make
):
我有同样的问题,最终使用以下命令(使用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"
.
在report.Rmd我可以在代码块中使用str(c(param1,param2))并且运行make将生成包含chr [1:2]“some parameter”“some other parameter”的HTML文件。
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.
如果您不使用make,只需指定您想要编织的方式以及从哪里获取参数,我可以帮助您根据需要调整我的解决方案。
#1
4
You could write a R script instead of a bash one:
您可以编写R脚本而不是bash脚本:
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
#2
1
I had the same problem and ended up using the following commands (using make
):
我有同样的问题,最终使用以下命令(使用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"
.
在report.Rmd我可以在代码块中使用str(c(param1,param2))并且运行make将生成包含chr [1:2]“some parameter”“some other parameter”的HTML文件。
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.
如果您不使用make,只需指定您想要编织的方式以及从哪里获取参数,我可以帮助您根据需要调整我的解决方案。