Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Parameters to R Markdown

Tags:

r

r-markdown

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

like image 984
Rafael Velásquez Avatar asked Sep 09 '15 12:09

Rafael Velásquez


People also ask

How do I use parameters in R Markdown?

Parameters in an R Markdown document are very simple to use. In the yaml header (the section at the top of the markdown document), you just have to add a couple new lines for your parameters to hardcode them in. Once they're coded in, they will be available in the params object for use in the rest of the analysis.

How do I add code to R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

Can you insert a table into R Markdown?

To use it, open a Rmd or R document and select “Addins –> Insert Table”.


2 Answers

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

like image 75
Paul James Avatar answered Nov 12 '22 17:11

Paul James


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

like image 26
drmariod Avatar answered Nov 12 '22 16:11

drmariod