Parameterization of Rmarkdown Files - r

I'm trying to loop through a list of employee IDs to create a report for each id.
I know I have to declare the parameter in the YAML but I'm getting a scanner error.
---
title: "Employee Record"
params:
MASTER_ID: !r uniqueID
output: pdf_document
---
**Employee ID:** `r params$MASTER_ID`
The field of employee ids in the dataset is called MASTER_ID and uniqueID is just a list of each unique employee id (length = 880)
The error I'm getting is:
Error in yaml::yaml.load(string, ...) :
Scanner error: while scanning a simple key at line 3, column 1 could not find expected ':' at line 4, column 1
I don't have any extra white spaces or anything so i'm not sure what I'm missing

To follow up on what others have already said, it is best to call your Rmd from a separate R file using the rmarkdown::render function. This also allows you to easily control the file naming and output location.
employees <- 1:10
for (i in employees) {
rmarkdown::render("test_pdf.Rmd",
params = list(MASTER_ID = i),
output_file = paste0('employee-', i, ".pdf"),
output_dir = '/reports')
}
With test_pdf.Rmd containing:
title: "Employee Record"
params:
MASTER_ID:
value: 1
output: pdf_document
---
**Employee ID:** `r params$MASTER_ID`

Related

How to change the prefix word and separator in table captions in RMarkdown with bookdown?

I am writing a document in RStudio using RMarkdown, knitr and R scripts. The output should be in MSWord. My document contains a lot of tables, so I need to have neatly formatted talble titles and cross reffernces to them. The only way to have cross references I found was bookdown package, so I use it to knit Word document from RMarkdown.
My problem is I cannot change prefix word and separator in the table caption. For example, I need the prefix word to be "MyTable" and separator " -- ". So the table caption in MSWord should look something like:
MyTable 1.1 -- Table caption
| Col1 | Col2 | Col3 |
----------------------
| Text | Text | Text |
But what I finally managed to get is only:
MyTable 1.1: Table caption
| Col1 | Col2 | Col3 |
----------------------
| Text | Text | Text |
The main problem is to replace ":" witn "--".
My code in RMarkdown is as follows:
---
title: "Test"
output:
bookdown::word_document2:
reference_docx: template.docx
html_document: default
---
.```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(tab.cap.pre = "MyTable ", tab.cap.sep = " -- ")
library(bookdown)
library(flextable)
set_flextable_defaults(font.size = 11, font.family = "Times New Roman", decimal.mark = ",", big.mark = " ", theme_fun = "theme_box")
.```
# Chapter Header
Some paragraphs of text. Then goes comes the place for my table.
.```{r}
ft <- flextable(head(mtcars))
ft <- set_caption(ft, "My table caption", autonum = run_autonum(seq_id = "tab", post_label = " -- ", bkm = "tab1"))
ft
.```
Very important information is in MyTable \#ref(tab:tab1).
Besides I have _bookdown.yml file with the following language settings:
language:
label:
fig: 'MyPicture '
tab: 'MyTable '
The latter actually is the way I found to change prefix word "Table" to "MyTable".
I also have the MSWord file with styles, made according to Pandoc user manuals. In the output I get my caption formatted according to the style it should be (but the word "Table" and number is not formatted as it would be if the table caption were made in MSWord).
I have also tried to knit the output file without using bookdown. In this case the line: knitr::opts_chunk$set(tab.cap.pre = "MyTable ", tab.cap.sep = " -- ") seems to work,
I get the right prefix word and the right separator, but simple table number (the result is "MyTable 1" instead of "MyTable 1.1") and sure no cross references.
My package versions are:
bookdown - 0.27
flextable - 0.7.3
knitr - 1.3.8
officer - 0.4.3
Unfortunately, I did not have any success with lua filters. I ended using officedown package. For it the code of YAML will be:
---
title: "Test"
output:
officedown::rdocx_document:
reference_docx: template.docx
tables:
style: Table
caption:
pre: "Table "
sep: " -- "
# To prevent word "Table" and crossref numbers from being bold
fp_text: !expr officer::fp_text_lite(bold = FALSE)
---

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 -

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

First name and last name inverted in bibliography r markdown

I got a problem using R-markdown to write my master's work. I am using the papaja package. Everything's fine except that when I knit my document to produce a pdf the first name of the author in my bibliography are BEFORE the last name. Here is a little reproducible example of my problem. Here is the r-makrdown content:
---
title: "The title"
shorttitle: "Title"
author:
- name: First Author
affiliation: 1
corresponding: yes # Define only one corresponding author
address: Postal address
email: my#email.com
- name: Ernst-August Doelle
affiliation: "1,2"
affiliation:
- id: 1
institution: Wilhelm-Wundt-University
- id: 2
institution: Konstanz Business School
abstract: |
Enter abstract here (note the indentation, if you start a new paragraph).
note: |
Complete departmental affiliations for each author (note the indentation, if you start a new paragraph).
Enter author note here.
keywords: "keywords"
wordcount: X
class: man
lang: american
figsintext: yes
lineno: yes
bibliography:
- r-references.bib
output: papaja::apa6_pdf
---
```{r message = FALSE, warning = FALSE}
library("papaja")
apa_prepare_doc() # Prepare document for rendering
```
[#R-papaja]
# References
```{r create_r-references}
r_refs(file = "test-references.bib")
```
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
\setlength{\parskip}{8pt}
And here are my references which are in the "test-references.bib" file:
%% This BibTeX bibliography file was created using BibDesk.
%% http://bibdesk.sourceforge.net/
%% Created for Lucien at 2015-12-09 13:45:03 +0100
%% Saved with string encoding Unicode (UTF-8)
#Manual{R-base,
title = {R: A Language and Environment for Statistical Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2015},
url = {https://www.R-project.org/},
}
#Manual{R-papaja,
author = {Frederik Aust and Marius Barth},
title = {papaja: Create APA manuscripts with RMarkdown},
year = {2015},
note = {R package version 0.1.0.9054},
url = {https://github.com/crsh/papaja},
}
The output in the pdf for the reference is:
Frederik Aust and Marius Barth.papaja: Create APA manuscripts with RMarkdown, 2015.20URL https://github.com/crsh/papaja. R package version 0.1.0.9054.
And I expect:
Aust Frederik and Barth Marius .papaja: Create APA manuscripts with RMarkdown, 2015.20URL https://github.com/crsh/papaja. R package version 0.1.0.9054.
Your paper was probably already due... Anyway. You should have the authors in the format Lastname, Firstname [and Lastname, Firstname]*.
In your case:
#Manual{R-papaja,
author = {Aust, Frederik and Barth, Marius},
title = {papaja: Create APA manuscripts with RMarkdown},
year = {2015},
note = {R package version 0.1.0.9054},
url = {https://github.com/crsh/papaja},
}

Resources