R / Sweave参数 [英] R / Sweave arguments
问题描述
我使用R和Sweave以生成报告。
I'm using R and Sweave to generate a report.
研究CMD Sweave MyReport.Rnw
R CMD Sweave MyReport.Rnw
我希望能够发送参数给R code,因为该报告,当然,动态。所以,我希望能够做这样的事
I want to be able to send arguments to the R code because the report is , of course, "Dynamic".So, I would like to be able to do something like this:
研究CMD SWeave MyReport.Rnw PatientId = 5
R CMD SWeave MyReport.Rnw PatientId=5
...并有R $ C $℃的变读 PatientId
值...
...and have the R code read the PatientId
value in a variable...
我如何做到这一点?使用环境变量有人提到但是这似乎是一个不完美的解决方案。
How do I do this? Somebody mentioned using environment variables but that's seems like a non-elegant solution.
推荐答案
要获得从研发从命令行传递的参数,你可以使用函数 commandArgs()
,只可惜研究CMD Sweave
不支持额外的命令行选项。但是,你仍然可以调用Sweave通过研究-e
,例如
To get arguments passed from R command line, you can use the function commandArgs()
, but unfortunately R CMD Sweave
does not support extra command line options. However, you can still call Sweave by R -e
, e.g.
R -e "Sweave('MyReport.Rnw')" --args PatientId=1
在 MyReport.Rnw
,你做一些文字处理上 commandArgs(TRUE)
,它给你的性格字符串 PatientId = 1
在这种情况下。
In MyReport.Rnw
, you do some text processing on commandArgs(TRUE)
, which gives you a character string PatientId=1
in this case.
但我相信一个更好的做法是使用功能 Sweave()
在R脚本的直接的;例如在这种情况下,您可以编写过程中如
But I believe a better practice is to use the function Sweave()
in an R script directly; e.g. in this case you can write the process in a script like
PatientId <- 1
Sweave("MyReport.Rnw")
和 MyReport.Rnw
使用全局变量 PatientId
直接。如果你想生成一个系列报道,你甚至可以用一个循环 PatientId
。
and in MyReport.Rnw
you use the global variable PatientId
directly. If you want to generate a series of reports, you can even use a loop for PatientId
.
这篇关于R / Sweave参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!