简体   繁体   中英

Getting Rmarkdown command line arguments printed to the html report

I'm trying to generate an Rmarkdown html report, where Rmarkdown is called through the command line. I'd like the header to have information passed through the command line arguments.

Here's my Rmarkdown code:

```{r setup.n.args, include=FALSE}
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(DT))
suppressPackageStartupMessages(library(Cairo))
knitr::opts_chunk$set(dev="CairoPNG")
knitr::opts_chunk$set(echo=FALSE,out.width='1000px',dpi=200,fig.keep="all")
options(width = 1000)
options(knitr.table.format = "html")

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

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


# Table
```{r table,warning=FALSE,message=FALSE,echo=FALSE}
datatable(args.df,rownames = FALSE,class='cell-border stripe')
```

And here's my command line call to it:

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

Unfortunately no header is created.

Any idea?

Change output part of YAML front matter from

output: html_document
  html_document:
      keep_md: true

to

output: 
  html_document:
      keep_md: true

And --author is incorrectly parsed.

In command line, changing from

--author='test test'

to

"--author=test test"

will fix that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM