0

我正在尝试从命令行运行一个Rmarkdown脚本,传递一些参数,这些参数将被集成到标题信息中,然后也打印其中一些。

这是我的Rmd脚本代码:

args <- commandArgs(TRUE)
parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
argsL <- as.list(as.character(argsDF$V2))
names(argsL) <- argsDF$V1

---
title: "`r argsL$title`"
author: "`r argsL$author`"
date: "`r format(Sys.time(), '%d %B, %Y, %H:%M')`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r cars}
print(argsL$title)
print(argsL$author)
```

我的命令行调用是:

R -e "rmarkdown::render('test.Rmd',output_file='test.html')" --args --title=my.title --author=me

我收到了这个错误:

processing file: test.Rmd
  |................                                                 |  25%
   inline R code fragments

Quitting from lines 2-12 (test.Rmd)
Error in eval(expr, envir, enclos) : object 'argsL' not found
Calls: <Anonymous> ... in_dir -> inline_exec -> withVisible -> eval -> eval

Execution halted

知道怎么做吗?

4

0 回答 0