Passing multiple parameters in RMarkdown document - r

I am looking to pass multiple parameters in my RMarkdown document. I have the following as an example:
mvndr_nm <- c('name1', 'name2', 'name3', 'name4', 'name5', 'name6')
I also have
mvndr_nbr<- c('60031167', '60688509', '60074051', '60148060', '60086898', '60080204')
I created a unique Markdown document using params and a loop that prints each out doing the following:
for (i in c('60031167', '60688509', '60074051', '60148060', '60086898', '60080204')) {
rmarkdown::render("C:/Users/santi/Documents/R Scripts/Export_Data_CSV.Rmd",
output_file = sprintf("MVNDR_%s.html", i),
params = list(MVNDR_NBR = i))
}
My print out look as such, I would like the first circle to actually be the names, instead of numbers. I tired to do that in the comment below, however it did not show the name despite calling the params function. Below the image is my logic :
---
title: "`r params$MVNDR_NBR`"
author: "Santiago Canon"
date: "5/26/2021"
output:
html_document:
highlight: monochrome
theme: flatly
params:
MVNDR_NBR: sample_vendor_tbl$MVNDR_NBR
MVNDR_NM: vendor_nm$MVNDR_NM
---
<font size="4"> This document will provide a summary of "`r params$MVNDR_NM`" performance within in QC: </font>
To be clear what I simply want is to pass the names through the title and some comments I will make it in the document. It works perfectly fine with the numbers but not the names

RMD file -
---
title: "`r params$MVNDR_NBR`"
author: "Santiago Canon"
date: "5/26/2021"
output:
html_document:
highlight: monochrome
theme: flatly
params:
MVNDR_NBR: NA
MVNDR_NM: NA
---
<font size="4"> This document will provide a summary of "`r params$MVNDR_NM`" performance within in QC: </font>
for loop -
mvndr_nm <- c('name1', 'name2', 'name3', 'name4', 'name5', 'name6')
mvndr_nbr<- c('60031167', '60688509', '60074051', '60148060', '60086898', '60080204')
for (i in seq_along(mvndr_nm)) {
rmarkdown::render("C:/Users/santi/Documents/R Scripts/Export_Data_CSV.Rmd",
output_file = sprintf("MVNDR_%s.html", mvndr_nbr[i]),
params = list(MVNDR_NBR = mvndr_nbr[i], MVNDR_NM = mvndr_nm[i]))
}
Output -
MVNDR_60031167.html
MVNDR_60688509.html -

Related

How to insert two separate bibliographies (without citing them in the body text) under two separate headers in R Markdown?

Consider the following R Markdown code:
---
title: 'John Doe'
output: pdf_document
bibliography: [bib1.bib, bib2.bib]
nocite: '#*'
csl: vancouver.csl
---
# Peer-reviewed publications
# Other publications
bib1.bib code:
#article{article1,
Author = {Doe, John},
Title = {{Article 1 name}},
Journal = {{Journal 1 name}},
Year = {{2021}},
Volume = {{1}},
Pages = {{1-2}},
}
#article{article2,
Author = {Doe, John},
Title = {{Article 2 name}},
Journal = {{Journal 2 name}},
Year = {{2020}},
Volume = {{1}},
Pages = {{1-2}},
}
bib2.bib code:
#article{article3,
Author = {Doe, John},
Title = {{Article 3 name}},
Journal = {{Journal 3 name}},
Year = {{2021}},
Volume = {{1}},
Pages = {{1-2}},
}
The knit output:
However, I'm rather looking after the following output:
Please note that I don't want in-text citations but rather the whole bibliographies listed, which is why I approached the problem by adding the "nocite: '#*'" to the code, but I couldn't get the result I'm after.
By the way, I used the csl style vancouver.csl (please see: https://www.zotero.org/styles?q=vancouver) here.
Any help is greatly appreciated.
You could use biblatex to split the bibliographies:
---
title: 'John Doe'
output:
pdf_document:
keep_tex: true
csl: vancouver.csl
header-includes:
- \include{preamble.tex}
---
with preamble.tex:
\usepackage[style=vancouver]{biblatex}
\addbibresource{bib1.bib}
\addbibresource{bib2.bib}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\perdatasource{bib1.bib}
\step[fieldset=keywords, fieldvalue={,peer}, append]
}
}
}
\AtEndDocument{\nocite{*}
\printbibliography[keyword={peer},title={Peer-reviewed publications}]
\DeclareFieldFormat{labelnumber}{%
\ifinteger{#1}
{\number\numexpr#1-2\relax}
{#1}}
\printbibliography[notkeyword={peer},title={Other publications}]}
And then compile the resulting filename.tex file with
pdflatex filename
biber filename
pdflatex filename

Tables appears in disorder in appendix section (R-markdown)

I'm writing my Ms thesis with R markdown, and I have a problem with the Appendix section. I need to put some tables in the appendix section but when I render it the header of the subsection appears below de table. like this:
I would like to know if there are some way to fix this.
The way I write the code is:
# Appendix {-}
## Subsection title
chunk with table
R-markdown chunk:
title: "Title of my thesis"
subtitle:
author:
- "Student :"
- "Director: "
output:
pdf_document:
number_sections: true
latex_engine: xelatex
template: NULL
word_document: default
degree: Ms Biology
field: Experimental biology
year: 2021
geometry: left=2cm,right= 2 cm,top=2 cm, bottom=2 cm
papersize: a4
header-includes:
- \usepackage{amsmath}
- \usepackage{booktabs}
- \usepackage{pdflscape}
- \usepackage[justification=raggedright,labelfont=bf,singlelinecheck=false]{caption}
- \usepackage{setspace}
bibliography: library.bib
csl: apa7.csl
fontsize: 11 pt
indent: true
spacing: 1.5
And chunk options
knitr::opts_chunk$set(
echo = FALSE,
warning = FALSE,
message = FALSE,
error =FALSE)
Well, in order to stop putting tables under de subtitle, I have to add
- \usepackage{flafter}
Then the table appears in the next page, in order to avoid that, I added
kbl(data)%>%
kable_styling(latex_options = "hold_position")
That's all.

How to use pass variables in rmarkdown pdf?

I'm trying to pass variables to rmarkdown report as params but i'm not able to render it in the report
the below one is my code the my_value param is dataframe having only single row
---
title
output:
pdf_document: default
output:
pdf_document:
keep_tex: true
classoption: svgnames
header-includes:
- \usepackage{amsfonts,amssymb,amsmath}
params:
my_class:"smile"
my_value: data$percent
--
`r params$my_value` is the total percentage of customers
`r params$my_class` is product which has been doing better
The YAML header you're using isn't valid.
title tag should finish by : and have a value : title: "test"
output should only be used once
finish header with ---
Try:
---
title: "test"
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{amsfonts,amssymb,amsmath}
classoption: svgnames
params:
my_class: "smile"
my_value: NA
---
Regarding parameters, you can't directly use data$percent as parameter value in YAML header.
Knitting is done in a new environment, data$percent doesn't exist in it.
In YAML, you could use following settings:
params:
my_class:"smile"
my_value: NA
In calling script:
# data definition
data <- ...
params <- list(
my_class = "more smiles",
my_value = data$percent
)
rmarkdown::render("MyDocument.Rmd", params = params)
For more information : https://bookdown.org/yihui/rmarkdown/params-knit.html

R Markdownd: ! LaTeX Error: Two \documentclass or \documentstyle commands

I am trying to knit a pdf in Rmarkdown, but I receive the same error over and over again:
! LaTeX Error: Two \documentclass or \documentstyle commands.
I already different approaches like deleting some rows, putting \begin{document} in different places or changing the order of loading packages.
This is my code:
---
title: "Feedback"
author: "Me"
date: "09 02 2021"
toc: true
toc_depth: 2
toc-title: "Verzeichnis"
fig.caption: "Tabelle"
smooth_scroll: false
header-includes:
\documentclass{article}
\PassOptionsToPackage[usenames,dvipsnames]{xcolor}
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage[default]{sourcesanspro}
\usepackage{tikz}
mainfont: SourceSansPro
output: pdf_document
---
\begin{document}
\addtolength{\headheight}{1.0cm}
\fancypagestyle{plain}{}
\thispagestyle{fancy}
\fancyhead[L]{\includegraphics[width = 500pt]{"/Users/lisak/One Drive/OneDrive/Dokumente/Masterarbeit/Erhebung/Anschreiben/alt/banner.png"}}
\renewcommand{\headrulewidth}{0pt}
{r, echo = FALSE, message = FALSE}
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
if (knitr::is_latex_output()) {
load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
if (load_packages) {
usepackage_latex("booktabs")
usepackage_latex("longtable")
usepackage_latex("array")
usepackage_latex("multirow")
usepackage_latex("wrapfig")
usepackage_latex("float")
usepackage_latex("colortbl")
usepackage_latex("pdflscape")
usepackage_latex("tabu")
usepackage_latex("threeparttable")
usepackage_latex("threeparttablex")
usepackage_latex("ulem", "normalem")
usepackage_latex("makecell")
usepackage_latex("xcolor")
}
}
auto_format <- getOption("kableExtra.auto_format", default = TRUE)
if (auto_format) auto_set_format()
if (!is.null(rmarkdown::metadata$output) &&
rmarkdown::metadata$output %in% c(
"ioslides_presentation", "slidy_presentation",
"gitbook", "bookdown::gitbook", "radix_article", "radix::radix_article",
"distill_article", "distill::distill_article"
)) {
options(kableExtra.html.bsTable = TRUE)
}
if (!is.null(knitr::opts_knit$get("rmarkdown.pandoc.to")) &&
knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("epub3", "epub")) {
options(kableExtra.knit_print.dependency = FALSE)
}
}
stuff....
\end{document}
Rmarkdown will automatically insert stuff like \documentclass{article} and \begin{document} when converting the rmarkdown document into a tex document. You must not insert it a second time
\PassOptionsToPackage[usenames,dvipsnames]{xcolor} must be used before the documentclass. As rmarkdown takes away your ability to insert the document class yourself, you can't use this
rmarkdown has trouble to correctly parse the square brackets of optional arguments. As a workaround, you can hide these commands in a separate .tex file
the syntax of your r chunk is wrong. You must surround it with backticks
---
title: "Feedback"
author: "Me"
date: "09 02 2021"
toc: true
toc_depth: 2
toc-title: "Verzeichnis"
fig.caption: "Tabelle"
smooth_scroll: false
mainfont: SourceSansPro
output:
pdf_document:
keep_tex: true
includes:
in_header: preamble.tex
---
```{r, echo = FALSE, message = FALSE}
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
if (knitr::is_latex_output()) {
load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
if (load_packages) {
usepackage_latex("booktabs")
usepackage_latex("longtable")
usepackage_latex("array")
usepackage_latex("multirow")
usepackage_latex("wrapfig")
usepackage_latex("float")
usepackage_latex("colortbl")
usepackage_latex("pdflscape")
usepackage_latex("tabu")
usepackage_latex("threeparttable")
usepackage_latex("threeparttablex")
usepackage_latex("ulem", "normalem")
usepackage_latex("makecell")
usepackage_latex("xcolor")
}
}
auto_format <- getOption("kableExtra.auto_format", default = TRUE)
if (auto_format) auto_set_format()
if (!is.null(rmarkdown::metadata$output) &&
rmarkdown::metadata$output %in% c(
"ioslides_presentation", "slidy_presentation",
"gitbook", "bookdown::gitbook", "radix_article", "radix::radix_article",
"distill_article", "distill::distill_article"
)) {
options(kableExtra.html.bsTable = TRUE)
}
if (!is.null(knitr::opts_knit$get("rmarkdown.pandoc.to")) &&
knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("epub3", "epub")) {
options(kableExtra.knit_print.dependency = FALSE)
}
}
```
stuff....
and preamble.tex:
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage[default]{sourcesanspro}
\usepackage{tikz}
\addtolength{\headheight}{1.0cm}
\fancypagestyle{plain}{}
\thispagestyle{fancy}
\fancyhead[L]{\includegraphics[width = 500pt]{example-image}}
\renewcommand{\headrulewidth}{0pt}

Spliting title line when using params in R / Rmd to generate a pdf

I have a R file running a Rmd file to generate a pdf (rmarkdown::render).
I use fancyhdr to tune the layout, header,...
As it is dynamic, I also use parameters (params).
My question is, which command should I use to split the title in two lines ?
I refer to that post, but none of the proposed solutions work. And I don't know if it is a question for R people or for those of Latex/Lualatex...
Here is my code in R, you may use your own path and fname (figure name) if you want to reproduce the code.
rmarkdown::render("your path/Fig_generator.Rmd",
output_file = file.path(your path, paste("test",".pdf", sep = "")),
encoding = "native.enc",
params = list(
dyntitle = "This is a very very very very long line that I would like | to split where the | is",
dynsubtitle = "Some subtitle text",
dynfigno = "Fig. xx-x",
dynprojectname ="xxx / xxx",
dynimage = paste("your path/",fname,".","pdf",sep = "")))
Here is the code in Rmd:
---
#knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, ) })
template: default-1.17.0.2.tex
output:
pdf_document:
latex_engine: lualatex
keep_tex: true
documentclass: article
mainfont: Arial
fontsize: 12pt
params:
dyntitle: !r dyntitle
dynsubtitle: !r dynsubtitle
dynfigno: !r dynfigno
dynprojectname: !r dynprojectname
dynimage: !r dynimage
title: "`r params$dyntitle`"
subtitle: "`r params$dynsubtitle`"
figureno: "`r params$dynfigno`"
projectname: "`r params$dynprojectname`"
image: "`r params$dynimage`"
---
\includegraphics[width=7.27in]{`r params$dynimage`}
Can someone help me with that, please ?
https://stackoverflow.com/a/28895263/11497260
If you combine that solution with your inline code - `r params$dyntitle` - it should work I guess.

Resources