How to use pass variables in rmarkdown pdf? - r

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

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

Passing multiple parameters in RMarkdown document

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 -

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.

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.

Increase header levels of child documents

I want to create a single PDF document from many .Rmd child documents using pdf_document2().
I have a wrapper file pdf_wrapper.Rmd that looks as follows
---
title: "title"
author: "author"
output:
bookdown::pdf_document2:
number_sections: true
---
# Header1
```{r child = 'child1.Rmd'}
```
With child1.Rmd having the following content
---
title: "Title2"
output: rmarkdown::html_document
---
## Title 3
Now when I render pdf_wrapper.Rmd, I get the following:
# Header 1
## Title 3
I want to end up with
# Header 1
## Title 2
### Title 3
I put all level 2 headers manually in the main document pdf_wrapper.Rmd, e.g. Title2.
Additionally, I decreased all remaining headers in all child documents by one level using the following bash call:
for i in *.Rmd; do
sed -i '# /## /g' $i
done
The main document then looks as follows:
---
title: "title"
author: "author"
output:
bookdown::pdf_document2:
number_sections: true
---
# Header1
## Title2
```{r child = 'child1.Rmd'}
```
Initially I though that the <title> fields in the YAML metadata of the child documents get processed as level 1 headers but this is not the case.

Resources