Reputation: 337
I am trying to create a parameterized report in R Markdown based on the following tutorial: http://rmarkdown.rstudio.com/developer_parameterized_reports.html#passing-parameters
I'm trying to pass a file path as a parameter from the r console using render. Like this:
render('rmarkdownfile.rmd',params= list( client= "clientdata.csv"))
and my markdown file looks like this:
title: "Liquidity Report"
output: pdf_document
params: client:"clientdata.csv"
---
```{r plot, echo=FALSE, warning=FALSE}
cftest <- read.csv(params$client)
But I get an error that says:
Eror in read.table(file=file, header=header, sep=sep, quote=quote, : 'file' must be a character string or connection Calls:
It seems like Markdown is not recognizing the parameters even though I'm following the steps of the tutorial. Has anyone been able to successfully use parameters in R Markdown?
Also, I'm following the recommendations of the tutorial and am using the R Studio preview as well as the latest builds of r markdown and knitr.
Thank you for the help!
Rafael
Upvotes: 10
Views: 24197
Reputation: 530
What I like to do is not just specify a file name but also a directory on my parameterized reports.
---
title: Liquidity Report
date: '`r strftime(Sys.time(), format = "%B %d, %Y")`'
output:
pdf_document:
number_sections: yes
theme: cerulean
toc: yes
toc_depth: 2
params:
directory:
value: x
file:
value: x
---
```{r, include = FALSE}
knitr::opts_chunk$set(
echo = FALSE
, warning = FALSE
, message = FALSE
)
## Pull in the data
dataset <- read.csv(file.path(params$directory, params$file))
```
And then in your render function you can:
rmarkdown::render(
input = 'LiquidityReport.Rmd'
, params = list(
directory = '~/path/to/data'
, file = 'clientdata.csv'
)
)
The knitr
docs can add more information: > ?knitr::knit_params
Upvotes: 16
Reputation: 11782
In my case it worked, just had to change the indentation in the header and some names which are available in my folder...
Here my jnk.Rmd
---
title: "Liquidity Report"
output: pdf_document
params:
client: "NAMESPACE"
---
```{r plot, echo=FALSE, warning=FALSE}
cftest <- read.csv(params$client)
```
And this is what I called in the console : render('jnk.Rmd',params= list( client= "NAMESPACE"))
Upvotes: 9