1
votes

I have a report in R which generates charts and has some text. The text contains a reference to the previous week which is is itself based on a dataframe

I cant get it to run. Below is the code. Does anyone see the problem

I get the following error message

Error in yaml::yaml.load(enc2utf8(string), ...) : Scanner error: mapping values are not allowed in this context at line 5, column 9 Calls: ... yaml_load_utf8 -> mark_utf8 -> -> .Call Execution halted

---
title: "Foo"
author: "John Smith"
date: "18 October 2016"
output: word_document
  params:
  weeknr: !r max(data$WEEKNRs)
---

```{r}

# creates the dataframe referenced in the header
source('transform.R')

```

All,

Please find below [attached](www.stackexchange.com) report for week ``r params$weeknr``
1
Is data in your global environment by chance? You need to create data in your markdown document itself. Rmarkdown is built to be a self contained script and so doesn't pull object written to the global environment by other scripts. In other words you can't use an object in your param section unless calling the Rmarkdown with a render function. - Morgan Ball
Try setting weeknr to an arbitary value and then call render(rmarkdowndoc.rmd,params=(weeknr=max(data$WEEKNRs)) - Morgan Ball
Hi @Morgan, the variable itself is basically in the last week in the dataframe data. By sourcing transform.R it should create the dataframe and store it within the file? Would Rmarkdown be able to pick this up? In your second comment, thus this information get put into an R Chunk? - John Smith
Yes you can pull it into your Rmarkdown with source . You could then just assign Max(data$WEEKNRs) to an object and use it in your Rmarkdown. But you can't use it in the params section. - Morgan Ball
If transform.R returns data, then you could do this : weeknr: !r max(source(transform.R)$WEEKNRs) or it might need to be this if transform.R returns a listweeknr: !r max(source(transform.R)[[1]]$WEEKNRs) - Morgan Ball

1 Answers

1
votes

The error you get is because you indented params: on the line after word_document.

It is possible to have code in the header, but you need to format it differently, see YAML current date in rmarkdown

In your case you can use something like (used the title tag in order to be able to easily see the result):

---
title:  "`r source('transform.R'); max(data$WEEKNRs)`"
---