5

Here is some R markdown code in a "test_param.Rmd" file:

---
output: pdf_document
params:
  number: "1"
title: `r params$number`
---

```{r setup, include=TRUE}

knitr::opts_chunk$set(echo = TRUE)

```

```{r one, include = TRUE}

i = 2
#data = data.frame(x = c(1,2,3))

#for(i in 1:dim(data)[1]){

 # plot(i*1000)

#}

```

`r params$number`

that is called using a .r file here:

library(rmarkdown)

rmarkdown::render("C://Users//me//Desktop//test_param.Rmd", 
                  params = list(number= "1"))

I get this error when I run the .r

Error in yaml::yaml.load(enc2utf8(string), ...) : 
  Scanner error: while scanning for the next token at line 4, column 8found character that cannot start any token at line 4, column 8

Any idea why?

I am using `` not '' to do the title: r params$number

user3022875
  • 8,598
  • 26
  • 103
  • 167

2 Answers2

4

Question 1 is nicely answered here, you can use params to inject a new title into the YAML config:

---
output: html_document
params: 
    new_title: "My Title!"
title: "`r params$new_title`"
---

Then the output filenames for rendered files can be set by the output_file parameter of rmarkdown::render.

Community
  • 1
  • 1
blmoore
  • 1,487
  • 16
  • 31
  • are those are backtick correct? when I do `r params$new_title` I get the error Error in yaml::yaml.load(enc2utf8(string), ...) : Scanner error: while scanning for the next token at line 4, column 8found character that cannot start any token at line 4, column 8 . Can you see updated code? – user3022875 Oct 20 '16 at 17:39
  • You're right, this now needs to be: `"\`r params$new_title\`"` (quotes around backticks). I'll update the answer – blmoore Oct 20 '16 at 17:49
  • I tried "`r paste0(params$MyParam, " test paste")`" and it does not work. Is there a way to paste text on to the title? – user3022875 Oct 20 '16 at 19:26
  • 1
    How about building the title on the R side rather than the Rmd side for simplicity? (Also it might be worth reverting your question edits so this all makes sense for people googling this same problem later.) – blmoore Oct 20 '16 at 19:35
2

For anyone who comes across this, instead of trying to call paste you can use something like"``r params$MyParam` test paste`". such as:

--- output: html_document params: new_title: "My Title!" title: "``r params$new_title` test paste`" ---

This worked using RStudio 1.0.143 with R 3.4.0