use*_*015 11 r r-markdown
我正在从命令行运行降价报告:
R -e "rmarkdown::render('ReportUSV1.Rmd')"
这份报告是在R工作室完成的,顶部看起来像
---
title: "My Title"
author: "My Name"
date: "July 14, 2015"
output:
html_document:
css: ./css/customStyles.css
---
```{r, echo=FALSE, message=FALSE}
load(path\to\my\data)
```
Run Code Online (Sandbox Code Playgroud)
我想要的是能够将标题和文件路径一起传递到shell命令,以便它生成原始报告,结果是不同的filename.html.
谢谢!
mat*_*fee 16
有几种方法可以做到.
您可以在YAML中使用backtick-R块并在执行render之前指定变量:
---
title: "`r thetitle`"
author: "`r theauthor`"
date: "July 14, 2015"
---
foo bar.
Run Code Online (Sandbox Code Playgroud)
然后:
R -e "thetitle='My title'; theauthor='me'; rmarkdown::render('test.rmd')"
Run Code Online (Sandbox Code Playgroud)
或者您可以commandArgs()直接在RMD中使用并在以下情况下将其输入--args:
---
title: "`r commandArgs(trailingOnly=T)[1]`"
author: "`r commandArgs(trailingOnly=T)[2]`"
date: "July 14, 2015"
---
foo bar.
Run Code Online (Sandbox Code Playgroud)
然后:
R -e "rmarkdown::render('test.rmd')" --args "thetitle" "me"
Run Code Online (Sandbox Code Playgroud)
这里如果你命名为args R -e ... --args --name='the title',你commandArgs(trailingOnly=T)[1]的字符串是"--name = foo" - 它不是很聪明.
在任何一种情况下,我想你会想要某种错误检查/默认检查.我通常会编写一个编译脚本,例如
# compile.r
args <- commandArgs(trailingOnly=T)
# do some sort of processing/error checking
# e.g. you could investigate the optparse/getopt packages which
# allow for much more sophisticated arguments e.g. named ones.
thetitle <- ...
theauthor <- ...
rmarkdown::render('test.rmd')
Run Code Online (Sandbox Code Playgroud)
然后R compile.r --args ...以我编写脚本要处理的格式运行提供参数.
| 归档时间: |
|
| 查看次数: |
3156 次 |
| 最近记录: |