2

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 `
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
morg
  • 381
  • 2
  • 17

1 Answers1

4

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.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • how do I format it? I add the bottom 2 lines to the last of my code (not in a chunk) and it didn't even show anything on those two extra headers, so I added it to a chunk and it didn't like that it started with a / `Quitting from lines 28-31 (Monthly_Diagnostic_Reporting_Code.Rmd) Error in parse(text = x, srcfile = src) : :2:1: unexpected input 1: # ALL COMMENTS ARE IN THE 6TH CHUNK, CHUNKS AFTER ARE ALL IDENTICAL 2: \ ^` So I added a - to it and now I get a parse code error ... – morg Jul 29 '19 at 14:28
  • 2
    @morg these two lines should come *before* the actual content. – Ralf Stubner Jul 29 '19 at 14:31
  • 1
    yes this worked! Thanks so much!! I tried FOR DAYS and no other forum had this answer stated in such easy terms :) – morg Jul 29 '19 at 14:41