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``
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)`"
---
Related
I am trying to use a parameterized path for a reference_docx in a Rmarkdown document with rmarkdown::word_document output format, in a similar way as e.g. is done here for the bibliography file (section Bibliograghy and Citation YAML options).
However, it seems like this feature does not work for the reference_docx option, as expressions passed to the arguments of the output format function (rmarkdown::word_document, or bookdown::word_document2 for that matter) are interpreted literally instead of evaluated. See e.g. this minimal reprex:
Working example:
---
params:
ref: www/Template_doc.docx
output:
word_document:
reference_docx: www/Template_doc.docx
---
Some markdown stuff
Equivalent non-working example:
---
params:
ref: www/Template_doc.docx
output:
word_document:
reference_docx: "`r params$ref`"
---
Some markdown stuff
This example gives the following error when trying to knit:
pandoc.exe: `r params$ref`: openBinaryFile: does not exist (No such file or directory)
That is, it tries to use `r params$ref` exactly as the file name, instead of evaluating params$ref
I have also tried with (as explained in this answer):
!r params$ref: it apparently ignores the !r altogether and considers params$ref to be the intended file name
!expr params$ref: it gives the following error:
Error: object 'params' not found
Error in yaml::yaml.load(..., eval.expr = TRUE) :
Could not evaluate expression: params$ref
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted
Any ideas on how to solve this? Many thanks!!
Apparently, the problem is with trying to access params from within the yaml header itself. As stated here,
Finally, params (the list of parameters provided within the knitting environment) is not available for yaml expressions.
Other than that, the field reference_docx can evaluate expressions the same way as other yaml header fields, see e.g..
Consequently, my own (working) example, adapted to this, would be:
---
params:
ref: www/Template_doc.docx
output:
word_document:
reference_docx: !expr file.path("www", "Template_doc.docx")
---
Some markdown stuff
I am getting an error when I try to save a file in R Markdown using Knit
'Error in yaml::yaml.load(string, ...) :
Scanner error: while scanning for the next token at line 7, column 1found character that cannot start any token at line 7, column 1
Calls: ... parse_yaml_front_matter -> yaml_load_utf8 -> -> .Call
Execution halted'
Can you please how to fix this? I cant save anything at the moment
The YAML header had to look like this:
---
title: "Microarray analysis 2"
author: "Ania"
date: "3/9/2017"
output: html_document
---
You can implement your R code after this header. Note that your R code has to be implemented in this way:
```{r}
source("http://bioconductor.org/biocLite.R")
```
The way you tried will not work, because (1) the source command was not in an R-environment, (2) the URL http://bioconductor.org/biocLite.R [1] was not found.
```
I am using rmarkdown and knitr
I have two questions:
Q1: I am not sure why the figure caption does not appear when I build my rmarkdown document.
Here is an example. I get the figure there but with not the figure caption!
---
title: "Untitled"
author: "XXXX"
date: "9 September 2016"
output: pdf_document
---
## R Markdown
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
![figure1 caption](my photo.png)
Q2: I am developing an R-package using RStudio. I am able to create an html_vignette using the code below. However, I am not sure why I am not able to create a PDF vignette when I change the output to output: rmarkdown::pdf_vignette? The latter gives an error:
Error: 'pdf_vignette' is not an exported object from 'namespace:rmarkdown'
Execution halted
---
title: "TITLE XXX"
author: "AMT"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{package-intro}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
I am new to this area, so if somebody can explain that would be great.
Using knitr in RStudio, I have an rmarkdown file:
---
title: "PDF knit error"
output: pdf_document
date: "`r format(Sys.time(), '%d %B, %Y')`"
---
#Content
In RStudio, there is a nice little dropdown option where I can change the output format:
However, when I use this to change between output formats, the YAML header is changed in an undesirable way (it is also changed appropriately, as the output value changes). That is, the date field, which has an R snippet to dynamically assign the value to date, gets munged-up because the double quotes are changed to single quotes, and the single quotes are changed to a pair of single quotes.
For example, when I use this button to change the out put format to HTML, the markdown file is altered to:
---
title: "PDF knit error"
output: html_document
date: '`r format(Sys.time(), ''%d %B, %Y'')`'
---
#Content
The date changes compared:
"`r format(Sys.time(), '%d %B, %Y')`" # before another output option selected
'`r format(Sys.time(), ''%d %B, %Y'')`' # after another output option selected
This change causes the Knit to fail:
processing file: yaml-header-test.Rmd
Quitting from lines 2-7 (yaml-header-test.Rmd)
Error in base::parse(text = code, keep.source = FALSE) :
<text>:1:29: unexpected input
1: format(Sys.time(), ''%d %B, %Y'')
^
Calls: <Anonymous> ... inline_exec -> withVisible -> eval -> parse_only -> <Anonymous>
Execution halted
Is there away to prevent the switching of formats via the dropdown menu from causing edits to the date field? And, pre-emptively, yes, I recognize I can accomplish the change in format by editing the output field directly, but my question is about consequences of the GUI button.
System details:
Ubuntu 16.04
pandoc 1.17.2
R 3.3.1
RStudio 0.99.489
I've got an Rmarkdown template that works well, and I've parameterized it so I can generate variants of the same report from different data sources. However, I'd like to change the title of the report in each case. How do I do that?
Here's the YAML header I have so far:
---
title: "My Title"
author: "Me, Inc."
date: "August 4, 2015"
output: pdf_document
params:
title: default
---
I've tried using params=list(title="ASDF") in the call to rmarkdown::render, and although my code can see that variable, it doesn't change the title. I've also tried using r params$title in the YAML, but that gives a syntax error.
Is there something else I should be trying? Thanks!
Try to use a second YAML metadata block, and put the parameterized metadata in there.
I got the following code to work as expected (i.e., producing a document title from the list of params):
---
output: html_document
params:
set_title: "My Title!"
---
---
title: `r params$set_title`
---
The RMarkdown documentation notes that YAML metadata blocks are combined by Pandoc. Use the first block to define the parameter set, and the second one to use the parameters as metadata. Knitr will execute the R code to interpret the parameters in the second block.Then Pandoc will merge the metadata blocks together.
Update (2017):
This can be accomplished in a single block, like so:
---
output: html_document
params:
set_title: "My Title!"
title: "`r params$set_title`"
---
This works because the title comes after the params definition. I put quotes around the in-line R code to prevent "Scanner errors".
This is a more simplified approach to the dynamic title challenge.
Decouple title from the top declaration like this:
From this:
---
title: "Sample Title"
output: pdf_document
---
To This:
---
output: pdf_document
---
```{r}
title_var <- "Sample Title"
```
---
title: `r title_var`
---
Within the R code chunks, declare title_var. Now the title is held within a variable. Hope this helps!
Adding this answer as it helps in making R markdown titles dynamic.
Just use !r followed by the object name defined (test_title in the case below) to make the title dynamic.
---
output: pdf_document
params:
set_title: !r test_title
---
---
title: `r params$set_title`
---