Set output_file in YAML - r-markdown

When I run render("test.Rmd") with these YAML parameters, the output is still test.docx and not teeeeeeeest.docx:
---
title: "mytitle"
author: "Me, myself and I"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: word_document
output_file: "teeeeeeeest.docx"
---
However, if I set the argument inside the render function (rmarkdown::render("test.Rmd", output_file = "teeeeeeeest.docx")), it works fine.
Is it possible to set the output_file argument inside the YAML header? Else, my purpose is to include the current date in the output file name when knitting with RStudio, how could I do that?

As shown here, it is possible to set the output file name inside the YAML by customizing the knit function within the YAML header. For this, you just need to add a 'knit:' hook in the header, such as in this example:
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile,
encoding=encoding,
output_file='new_file_name.html')) })

Related

Unable to find file 'path/NA' when knitting to word_document2 using tbl_summary but works for PDF, HTML etc

before you click "next", this is not a path file error.
I am building on a blogpost: https://labrtorian.com/2019/08/26/rmarkdown-template-that-manages-academic-affiliations/
It works well for scientific papers.
My problem is:
In Rmarkdown when calling a data frame I am returned with the error about not being able to find file 'path/NA'
---
output:
bookdown::word_document2:
pandoc_args:
- --reference-doc=Extras/Reference_Document.docx
keep_tex: TRUE
---
```{r MRE}
library(gtsummary)
a <- c(10,20,30,40)
b <- c('book', 'pen', 'textbook', 'pencil_case')
c <- c(TRUE,FALSE,TRUE,FALSE)
d <- c(2.5, 8, 10, 7)
df <- data.frame(a,b,c,d)
tbl_summary(df)
tbl_summary
which returns:
Error: could not find file '/Users/brianbrummer/Documents/Rstudio/Affilitations_template/NA'
Execution halted
So what gives? I would usually know how to fix a "path not found" error, but not a file...and the file is NA?
files referenced in YAML can be found:
https://github.com/bridaybrummer/author_affiliations_extras.git
Thanks in advance
I did some digging and found the issue and a solution. First, if you update your yaml to call the reference document like this, you won't get the error any longer.
---
output:
word_document:
reference_doc: Reference_Document.docx
---
The issue was with the flextable package. If you're looking for community service points, you can post this reprex to their github page to alert them to the issue.
---
output:
word_document:
pandoc_args:
- --reference-doc=Reference_Document.docx
---
```{r MRE}
mtcars |>
flextable::flextable()
```

Bookdown/rmarkdown: "Could not find data file templates/--number-sections.latex Error: pandoc document conversion failed with error 97"

Following: render_book("index.Rmd")
I was getting this other error associated with three part table:
LaTeX Error: Environment ThreePartTable undefined
I applied the solution suggested there(remotes::install_github('rstudio/rmarkdown')). Now I have this error:
"Could not find data file templates/--number-sections.latex
Error: pandoc document conversion failed with error 97"
My index.Rmd looks like this:
title: "Figures and Tables"
author: "name"
date: "`r Sys.Date()`"
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
keep_tex: yes
citation_package: natbib
fontsize: 12pt
classoption: oneside
linestretch: 2
documentclass: book
bibliography: "bibs.bib"
biblio-style: "apalike"
geometry: "left=1.5in, right=1in, top=1in, bottom=1in"
---
preamble.tex has:
\usepackage[none]{hyphenat}
\usepackage{booktabs}
\pagestyle{plain}
\raggedbottom
While render_book("index.Rmd") does not generate a pdf and gives me this pandoc error, it successfully generates a merged markdown file "FiguresTables.Rmd." When I try to knit the merged file it works, it does generate the pdf. I just cannot figure-out why I am unable to generate the pdf from render_book("index.Rmd").
Never mind! Writing the question made me think of an answer. I just re-installed r-markdown from the cran repository instead of github and tried again. And it works fine now.

rmarkdown to preserve line breaks in YAML header variable during export

I am exporting an rmarkdown file to odt, to html, and to pdf. But let's focus on the odt export first.
I want to have an address as part of the YAML header:
---
title: Test Multi
address:
First Name
Institute
Street
City
output:
odt_document:
template: default.opendocument
---
# Just a test document
With some text
This address should then be exported as part of the 'header'. Therefore, I have added the following snippet into the default odt-template (saved as default.opendocument):
$if(address)$
<text:p text:style-name="Author">$address$</text:p>
$endif$
But at the export the line breaks are lost:
Following this answer I tried pipes as in
---
title: Test Multi
address: |
| First Name
| Institute
| Street
| City
output:
odt_document:
template: default.opendocument
---
# Just a test document
With some text
But then, the address is missing completely from the odt.
So, my question is: How can I have the multi-line address from the YAML added to the export preserving the line breaks?
PS: I am using rmarkdown 1.12 and pandoc-2.7.3
There are two things going on.
As you discovered, you need address: | for YAML to not gobble up your line-breaks.
But the string is treated by pandoc as markdown and pandoc's markdown by default treats linebreaks inside a paragraph as spaces. But you can escape them:
address: |
First Name \
Institute \
Street \
City

Setting citation_package option under pdf_document in YAML Header and using rmarkdown::pdf_document function

The help file of the pdf_document function of the Rmarkdown CRAN package says that the function is defined with the following options:
pdf_document(toc = FALSE, toc_depth = 2, number_sections = FALSE,
fig_width = 6.5, fig_height = 4.5, fig_crop = TRUE,
fig_caption = TRUE, dev = "pdf", df_print = "default",
highlight = "default", template = "default", keep_tex = FALSE,
latex_engine = "pdflatex", citation_package = c("none", "natbib",
"biblatex"), includes = NULL, md_extensions = NULL,
pandoc_args = NULL, extra_dependencies = NULL)
I am under the impression that I could add any of these arguments as options under pdf_document in the output section of the YAML header in my Rmarkdown document. Is this correct? The document managed to compile when I used the following options/arguments of pdf_document: toc, number_sections, fig_caption, df_print, and keep_tex. However, when I add citation_package to the list, I get an error in compiling. For instance, consider the following template Rmarkdown document.
---
title: "Untitled"
author: "Unknown"
date: "4/18/2019"
output:
pdf_document:
# toc: true
number_sections: true
fig_caption: true
df_print: kable
keep_tex: true
citation_package: biblatex
bibliography: sample_bib.bib
csl: /Users/Shared/Zotero/styles/multidisciplinary-digital-publishing-institute.csl
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
I love R [#rcoreteamLanguageEnvironmentStatistical2018].
The sample_bib.bib mentioned is the bib file that looks like
#software{rcoreteamLanguageEnvironmentStatistical2018,
location = {{Vienna, Austria}},
title = {R: {{A Language}} and {{Environment}} for {{Statistical Computing}}},
url = {https://www.R-project.org/},
version = {3.5.1},
organization = {{R Foundation for Statistical Computing}},
date = {2018},
author = {{R Core Team}}
}
However, I was unable to knit this Rmarkdown document. I got the following error.
/usr/local/bin/pandoc +RTS -K512m -RTS rmarkdown_pdf_document.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output rmarkdown_pdf_document.tex --template /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --number-sections --highlight-style tango --pdf-engine pdflatex --biblatex --variable graphics=yes --variable 'geometry:margin=1in' --variable 'compact-title:yes'
output file: rmarkdown_pdf_document.knit.md
INFO - This is Biber 2.7
INFO - Logfile is 'rmarkdown_pdf_document.blg'
INFO - Reading 'rmarkdown_pdf_document.bcf'
INFO - Found 1 citekeys in bib section 0
INFO - Processing section 0
INFO - Looking for bibtex format file 'sample\_bib.bib' for section 0
ERROR - Cannot find 'sample\_bib.bib'!
INFO - ERRORS: 1
Error: Failed to build the bibliography via biber
Execution halted
Warning message:
LaTeX Warning: Citation 'rcoreteamLanguageEnvironmentStatistical2018' on page 1
undefined on input line 137.
LaTeX Warning: Empty bibliography on input line 169.
Package rerunfilecheck Warning: File `rmarkdown_pdf_document.out' has changed.
(rerunfilecheck) Rerun to get outlines right
(rerunfilecheck) or use package `bookmark'.
LaTeX Warning: There were undefined references.
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.
Package biblatex Warning: Please (re)run Biber on the file:
(biblatex) rmarkdown_pdf_document
(biblatex) and rerun LaTeX afterwards.
Luckily, this problem is easily fixed by moving the "citation_package" from an option under pdf_document to a separate line in the YAML header. Or:
---
title: "Untitled"
author: "Unknown"
date: "4/18/2019"
output:
pdf_document:
# toc: true
number_sections: true
fig_caption: true
df_print: kable
keep_tex: true
citation_package: biblatex
bibliography: sample_bib.bib
csl: /Users/Shared/Zotero/styles/multidisciplinary-digital-publishing-institute.csl
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
I love R [#rcoreteamLanguageEnvironmentStatistical2018].
Upon knitting this adjusted Rmarkdown document, the result is a pdf_document.
So my question is: Was I right that you could add any of the arguments found in the pdf_document help file on CRAN as options under "pdf_document" in the YAML header in a Rmarkdown file? Or, upon using this particular citation_package argument/option, could this be a possible bug in the rmarkdown::pdf_document() function?
ALSO POSTED ON RMARKDOWN's GITHUB; see for additional comments.

Where to put header.tex to change page to Landscape Rmarkdown

I read here how to add what is needed to give me the ability to change my page to landscape:
enter link description here
However I have saved the header.tex file everyplace I could think of and I still end up getting the following error:
Called from: (function (e)
{
if (inherits(e, "shiny.silent.error"))
return()
handle <- getOption("shiny.error")
if (is.function(handle))
handle()
})(list(message = "pandoc document conversion failed with error 43",
call = NULL))
Browse[1]>
I know this is do to me typing \blandscape and \elandscape because the PDF works as soon as I delete those commands. What I need help with is figuring out where to save the header.tex file to so that Rmarkdown knows how to locate it. Thanks in advance for everyone's your help!
Follow up:
Taking scoa's advice about adding it to my header I edited the top of my Rmd file to the following. Unfortunately it still didn't work.
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---
\newpage
\blandscape
```{r Non-Seasonal MK results, echo=FALSE}
# MKanalysis <- reactive(HM_MK(data = NonSeasonalData(),n = 4))
kable(MKanalysis(), caption = "Non-Seasonal Trend Analysis")
```
The results of the Mann Kendall analysis....
\elandscape
Latex program/verision: pdfTex, Version 3.14159265-2.6-1.40.17
Save the header.tex in the same directory your Rmd file is. Also, rather than an external file, you could just add those commands in your yaml front matter, a more portable solution:
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---