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
Related
I would like to include external markdown file covering inline code as well. Once I hit the Knit on RStudio, it does show only code text rather than the actual value of sys.time. If I place the content of about.md into the main.Rmd, there is no issue. The point should be related with includeMarkdown but it does not take any parameter except than path. Any suggestions ?
Thanks in advance
main.Rmd
---
title: "test"
author: "test"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
vertical_layout: fill
---
```{r}
htmltools::includeMarkdown('about.md')
```
about.md
Today is `r format(Sys.time(), "%d %B %A %Y")`
Current Output
Today is r format(Sys.time(), "%d %B %A %Y")
htmltools::includeMarkdown() only includes plain Markdown, not R Markdown. Your about.md is actually R Markdown---it contains R code to be evaluated.
To include an R Markdown document in another one, you can use the chunk option child:
```{r, child='about.Rmd'}
```
I suggest you rename about.md to about.Rmd.
I declare my parameter Report_Date in my YAML header in R Markdown. I want to use the Report_Date parameter as a forced header in header-includes the line after it. No matter which characters I use I can't seem to reference that parameter.
I've tried any variation I could think of and followed all the other SO I've found. Even though some have almost identical questions, the answers don't work for me, or I'm doing something else wrong.
- \fancyhead[LO,LE]{"'r params$Report_Date'"}
- \fancyhead[LO,LE]{r params$Report_Date}
- \fancyhead[LO,LE]{\"'r params$Report_Date'"}
- \fancyhead[LO,LE]{\r params$Report_Date'}
- \fancyhead[LO,LE]{'`r params$Report_Date`'}
- \fancyhead[LO,LE]{'r params$Report_Date'}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"$}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"}
- \fancyhead[LO,LE]{\$r params$Report_Date'"$}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"$}
- \fancyhead[LO,LE]{$'r params$Report_Date'$}
- \fancyhead[LO,LE]{"r params$Report_Date"}
even tried:
includes:
in_header:'`r params$Report_Date`'
as told in: YAML current date in rmarkdown
This is my current YAML R Markdown code (it is NOT in a separate template file, just in my regular .rmd file that I want to create)
---
title: "Monthly Diagnostic Report"
author: "Morgan :)"
date: "July 12, 2019"
toc: true
params:
Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Monthly Diagnostic Report}
- \fancyhead[LO,LE]{"'r params$Report_Date'"}
- \fancyfoot[RE,RO]{\thepage}
output: pdf_document
---
Preferably I'd have a left header that evaluates paste0("Report Created for: ", params$Report_Date) and a footer that evaluates paste0("Report Created on: ", format(Sys.time(), "%d %B, %Y")).
But for now I'd settle for just a header contains the Report_Date parameter.
Error messages include:
! Missing $ inserted.
<inserted text>
You may need to add $ $ around a certain inline R expression `r `
the only syntax that would make sense to me is
---
output: pdf_document
params:
Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[LO,LE]{`r params$Report_Date`}
---
If I use that I get a different error message about params being unknown. This does make sense, since params is defined only after the YAML header has been parsed. Fortunately one can use \fancyhead and \fancyfoot also in the body of your document (after \begin{document} in LaTeX speak). Hence the following should give you the desired output:
---
output: pdf_document
params:
Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Monthly Diagnostic Report}
- \fancyfoot[RE,RO]{\thepage}
---
\fancyhead[LO,LE]{Report created for: `r params$Report_Date`}
\fancyfoot[CO,CE]{Report created on: `r format(Sys.time(), '%d %B, %Y')`}
<actual content>
Note: I am setting the creation date in the body as well, since it is difficult to get a literal colon into the YAML header.
I am using this pseudo code to generate dynamic title on an flexdashboard in R
title_Var <- paste("Numurkah", format(max(Data$`Cutt Off daily`), "%d %b %Y"))
which works fine in the R console : Numurkah 09 Oct 2018
but when I use it inside the RMD file, (this code is inserted after I defined my variable in r)
---
title: \`r title_Var`\
---
it render in the HTML file as
09 Oct 2018
so YAML get my variable correctly, but it show only the date not the first text " Numurkah"
thanks for the reply, I found the correct answer
---
title: `r title_Var`
---
I think there are some restrictions on which YAML lines R code can execute from (see for example Inline R code in YAML for rmarkdown doesn't run). You can create the title with system parameters, it seems, but not with variables that are identified later in the markdown doc. For example this works:
---
title: '`r paste("Numurkah", format(Sys.Date(), "%d %b %Y"))`'
date: '`r format(Sys.time(), "%d %B %Y")`'
output: html_document
---
I'm trying to have a script knit an Rmd file to a PDF document and faced some errors.
I must say, document knits fine if I click on the knit button in Rstudio but it fails to knit if I use the command:
rmarkdown::render('diagnostic.Rmd',output_format = "pdf_document",
output_file = "diag.pdf")
The first error I get comes from this part of my code:
---
author: Zigo_datateam
date: "`r format(Sys.time(), '%d %B, %Y')`"
geometry: margin=1.6in
header-includes: "\\usepackage{fancyhdr} \\fancyfoot[CO,CE]{My footer}"
output:
pdf_document:
toc: false
title: "Graphic Layout(Report)"
---
and is:
Error in yaml::yaml.load(string, ...) :
Scanner error: while scanning a simple key at line 4, column 1could
not find expected ':' at line 5, column 1
So, if I remove the hole header-includesexpression it seems to start knitting but then I get a second error from this part (which comes after the second "---"):
\addtolength{\headheight}{1.0cm}
\pagestyle{fancyplain}
\rhead{\includegraphics[height=1.2cm]{logo.png}}
\renewcommand{\headrulewidth}{0pt}
ERROR:
! Undefined control sequence.
<argument> \undefinedpagestyle
l.130 ...headheight}{1.0cm} \pagestyle{fancyplain}
How can I get rid of the first error without getting the second one?
Update: I decided to move all footer/header styles to a separate file and my code looks like this:
---
author: Zigo_datateam
date: "`r format(Sys.time(), '%d %B, %Y')`"
geometry: margin=1.6in
output:
pdf_document:
includes:
in_header: mystyles.sty
title: "Graphic Layout(Report)"
---
The errors I talked about earlier stoped showing but I still get this:
pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
In your YAML, use
header-includes:
- \usepackage{fancyhdr}
- \fancyfoot[CO,CE]{My footer}
And it should work.
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)`"
---