0

I'm trying to run an Rmarkdown script from the command line passing a few arguments that will be integrated into the header info and then print some of them too.

Here's my Rmd script code:

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)
```

And my command line call is:

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

And I'm getting this error:

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

Any idea how to do this?

dan
  • 6,048
  • 10
  • 57
  • 125
  • I have not tested but it looks like the first part of your code is not contained in an r code chuck. Markdown should treat this as text, which is why argsL is not found. – Ian Wesley Jun 22 '17 at 22:19
  • Right. Thanks a lot – dan Jun 22 '17 at 22:24

0 Answers0