10

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

Rafael Velásquez
  • 337
  • 1
  • 3
  • 14

2 Answers2

15

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

Paul James
  • 520
  • 4
  • 17
  • Thanks! Looks like a great way to do it. I'll try it out. – Rafael Velásquez Sep 11 '15 at 18:04
  • If you want multiple files, do you just add `value x` `value y` `value z`? And how would you select the right file? – M. Beausoleil Feb 06 '20 at 22:19
  • @M.Beausoleil Do you intend on using all the files within a single report or iteratively run the report for multiple files? – Paul James Feb 06 '20 at 22:38
  • I want to use all files within the same report: `Rscript -e "rmarkdown::render('./testreport.Rmd', params=list(directory = '/PATHTO/geneticstuff/', lqual = 'finches_stats.lqual', ldepth.mean = 'finches_stats.ldepth.mean', lmiss = 'finches_stats.lmiss', fq = 'finches_stats.frq', idepth = 'finches_stats.idepth', imiss = 'finches_stats.imiss'))"` – M. Beausoleil Feb 06 '20 at 22:44
  • 1
    OK! I think I got it: `params: directory: value: x lqual: value: x ldepth.mean: value: x lmiss: value: x fq: value: x idepth: value: x imiss: value: x ` These are put on different lines. So that you use the each "file" as a new `params$file` to be put into the R script! – M. Beausoleil Feb 06 '20 at 22:47
  • 1
    Also I'd suggest to change the `paste` function to `file.path` – M. Beausoleil Feb 06 '20 at 22:49
  • 1
    The `value: x` line is the default value. I used `x` in this case in order to create a placeholder and make sure the render call fails without a valid directory and file path. – Paul James Feb 06 '20 at 23:17
  • Thanks a lot for the explanation! It worked for me! I'm happy!! :D – M. Beausoleil Feb 06 '20 at 23:35
9

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

drmariod
  • 11,106
  • 16
  • 64
  • 110
  • This is working fine for me too. The quotes are not necessary around the file name in the YAML header by the way. @Rafael, are you sure the clientdata.csv file is in the same directory as your .Rmd file ? – Tutuchan Sep 09 '15 at 13:54
  • Actually the script complains about the content of the variable (must be character) and not about a non existing file... – drmariod Sep 09 '15 at 13:58
  • Thank you very much! That worked perfectly. I'm pretty new at this and did not have any idea that indentation could have such a large effect. – Rafael Velásquez Sep 09 '15 at 13:59