Using YAML set parameters IN YAML header Knitr with parameters - r

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.

Related

R Markdown Knitting header with JPG issue

I am new to markdown and have to generate a weekly report with the proper header including a jpg image. I have the following yaml
---
title: "My Title"
author: "Author Name"
date: "`r Sys.Date()`"
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \pretitle{\begin{center}
\includegraphics[width=2in]{picture.jpg}}
- \posttitle{\end{center}}
mainfont: Calibri
output:
pdf_document:
latex_engine: xelatex
---
Company policy states that all documents have to be in Calibri(I know) which was the main issue prior to this.
When I go to knit I get this error
Error in yaml::yaml.load(..., eval.expr = TRUE) :
Scanner error: while scanning a simple key at line 11, column 5 could not find expected ':' at line 12, column 5
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted
Any help on a resolution is much appreciated.
The first error Error in yaml::yaml.load(..., eval.expr = TRUE) : Scanner error: while scanning a simple key at line 11, column 5 could not find expected ':' at line 12, column 5 (as described in the question) originates from the fact that you are missing a - in line 11, so that line is considered as a key of YAML and since this key is missing a :, hence the error.
And after adding -, you are having another error ! Undefined control sequence. l.68 \pretitle {\begin{center} Error: LaTeX failed to compile (as said in the comment), because the command \pretitle is not recognised here. The command \pretitle (and also \postitle) comes from the latex package titling which is not specified here, hence the error.
Solution
Simply add the package titling using \usepackage in header-includes.
---
title: "My Title"
author: "Author Name"
date: "`r Sys.Date()`"
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \usepackage{titling}
- \pagestyle{fancy}
- \pretitle{\begin{center}
- \includegraphics[width=2in]{picture.jpg}}}
- \posttitle{\end{center}}
mainfont: Calibri
output:
pdf_document:
latex_engine: xelatex
---

How to add logo to first slide of beamer presentation using rmarkdown YAML header?

I am new to Rmarkdown, Beamer presentations and the like and have been having some trouble inserting my logo to the header. I don't exactly know how the code works, Here is the code I am trying to use for my header, but it is giving me a "Undefined Control Sequence" error.
---
title: "Lifewerks Reporting Numbers 2021-2022"
date: "`r Sys.Date()`"
header-includes:
- \usepackage{subfig}
- \logo{\incudegraphics[width = 3cm]{sfp-logo.png}}
output: beamer_presentation
---
Any help or resources that I could learn from would be greatly appreciated! Thank you!!
there is a small typo, it should be \includegraphics [the l is missing]
if you want the image just on the first page, I would use \titlegraphic instead of \logo (the later would add the image to all frames)
---
title: "Lifewerks Reporting Numbers 2021-2022"
date: "`r Sys.Date()`"
header-includes:
- \titlegraphic{\includegraphics[width = 3cm]{example-image-duck}}
output:
beamer_presentation:
keep_tex: true
---
test

Formatting issue when creating page header in rmarkdown

I am trying to learn Rmarkdown, but there is something that's quite elusive. I am trying to put my name at the top of every page of the PDF I print, however I also get the various headers from the text in the markdown file in the header, and it often overlaps and makes everything unreadable. I learned the basics from another thread on here, but still struggling. I only want my name in the header. I tried to modify it like what you see below, but then I don't get anything at all. This is my YAML:
---
title: "Something"
author: "something"
date: "42 42 42"
output: pdf_document
includes:
in_header: {My name}
fontsize: 12pt
header-includes:
\usepackage{setspace}
\onehalfspacing
---
Anyone who could write it so that I get only my name at the top of the page? Also, the onehalfspacing is the same as setting spacing to 1.5 in word right?
I think there is a conceptual problem: The things you put in includes: in_header: in the yaml are the files included in the preamble of your tex document, similar to what you have in header-includes:. This is not in any way related to the headline of your document.
If you like to change this, have a look at the fancyhdr latex package
Please also note that the syntax for multiline header-includes: is wrong in your example. You need to prefix each line with -
---
title: "Something"
author: "something"
date: "42 42 42"
output: pdf_document
fontsize: 12pt
header-includes:
- \usepackage{setspace}
- \onehalfspacing
- \usepackage{fancyhdr}
- \fancyhead[c]{your name}
- \pagestyle{fancy}
- \setlength{\headheight}{15pt}
---
test
\newpage
test

Why Rmarkdown Dynamic TITLE in YAML does not appear in the pdf file

I am trying to generate dynamic PDF reports from Rmarkdown with dynamic titles. To do this I use the function rmarkdown::render() in my R script and I pass in the argument params the title parametre as set.title = title.
The YAML of my Rmarkdown script I have the next code:
author: "Author test"
date: "`r Sys.setlocale('LC_TIME','C');format(Sys.Date(),'%B %d, %Y')`"
output:
pdf_document:
toc: yes
toc_depth: 5
keep_tex: yes
html_document:
theme: united
toc: yes
classoption: table
header-includes:
- \usepackage{array}
- \usepackage{float}
- \usepackage{xcolor}
- \usepackage{caption}
- \usepackage{longtable}
#- \usepackage{mulicol}
params:
set.title: title
title: "`r params$set.title`"
Once I run my code everything goes well and generate the different pdfs that I expect, the problem is that neither of these PDFs show the title, author and date. I am working with version 1.8 of rmarkdown package.
Can anyone help me with this issue?
For closing the conversation of this question I write how has been solved this question.
At the beginning I was thinking this issue was related with the code in YAML (where the title was informed from parameters) but eventually I discovered that there was nothing to do that. The problem was that I was using a the function cat() in the varible title that I pass as a parametre and this function changes the value of variable title into NULL.
For knowing exactly all the explanation about how to write dynamic titles in rmarkdown documents redirect yourself to the next entrance: Setting document title in Rmarkdown from parameters

R Markdown - remove left-sided header in pdf output

I am trying to create an automated report in rmarkdown for business partners and I am a bit stuck on how to set up the headers using the Latex package fancyhdr.
The report includes a table of contents set to level 1 headers, but there are sub-sections in the report that are created using level 2 headers.
When I generate the PDF the fancy header has the level 1 header on the right header (which is fine), the message I set in the central header (also fine), and the sub-section (level 2 header) in the left header.
I would like to remove this, but the documentation is very vague as to how to do so - I've just spent a considerable amount of fruitless time trying to get this to work.
Here are the YAML settings at the top of the R Markdown document:
title: "Report Title"
author: "Authors"
date: 'Date'
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 1
header-includes:
- \usepackage{fontspec}
- \setmainfont{Gotham Book}
- \usepackage{booktabs}
- \usepackage[tocflat]{tocstyle}
- \usetocstyle{standard}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Center Message}
- \fancyfoot[CO,CE]{Footer Message}
- \fancyfoot[LE,RO]{\thepage}
---
These are the "left-odd" and "right-even" headers. If you set them to nothing using \fancyhead[LO,RE]{} they will go away:
---
title: "Report Title"
author: "Authors"
date: 'Date'
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 1
header-includes:
- \usepackage{fontspec}
- \usepackage{booktabs}
- \usepackage[tocflat]{tocstyle}
- \usetocstyle{standard}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Center Message}
- \fancyfoot[CO,CE]{Footer Message}
- \fancyfoot[LE,RO]{\thepage}
- \fancyhead[LO,RE]{}
- \usepackage{blindtext}
---
\blinddocument
(I have removed your font and inserted some sample text to show the effect.)

Resources