How to fix errors in building a book in r bookdown - r

building my book in R bookdown using the build tab but running into errors
I have updated my R and R Studio to the latest version
---
title: "Lessons for Africa"
subtitle:
- "By"
- "Saanuo"
author:
- "A thesis submitted to"
- "MASTER’S OF PHILOSOPHY"
- "[FINANCE]"
date: "June, 2020"
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
keep_tex: yes
latex_engine: xelatex
citation_package: none
toc: true
fig_width: 8
fig_height: 6
fig_caption: true
mainfont: Timesnewroman
fontsize: 12pt
linestretch: 1.5
toc-depth: 3
lot: True
lof: True
site: bookdown::bookdown_site
documentclass: book
bibliography: library.bib
csl: harvard-educational-review.csl
link-citations: yes
geometry: "left=4cm, right=2.5cm, top=2.5cm, bottom=2.5cm"
---
\pagenumbering{roman}
# ->Declaration<- {-}
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.
===
# ->Dedication<- {-}
You can also embed plots, for example:
===
# ->Acknowledgement<- {-}
You can also embed plots, for example:
===
# ->Abstract<- {-}
You can also embed plots, for example:
===
# ->Abbreviations<- {-}
library(tidyverse)
library(knitr)
data_frame(
Abbreviation = c("WM", "STM", "LTM"),
Term = c("Working memory", "Short-term memory", "Long-term memory")) %>%
arrange(Abbreviation) %>%
kable(booktab = T)
I wanted to build my book by encountering this error message. Xie please this the my entire index file, as requested. Thank you for your concern. Much appreciated. Hope this helps give my clarity to my problem.
Error in yaml::yaml.load(..., eval.expr = TRUE) :
Scanner error: mapping values are not allowed in this context at line 19, column 14
Calls: <Anonymous> ... enumerate_output_formats -> yaml_load_file -> yaml_load -> <Anonymous>
Execution halted
Exited with status 1.

Related

bibliography or csl directory path in rmarkdown for windows system

My testing files with related directory paths (in windows operating system) are listed as followed
E:\mywork\test.rmd
E:\myref\apa.csl
E:\myref\ref.bib
E:\mydata\xx.csv
In test.rmd, it shows
---
title: "Untitled"
author: "xxx"
date: "xxx"
output: pdf_document
bibliography: "E:/myref/ref.bib" #ok!
# bibliography: "../myref/ref.bib" #ok!
# bibliography: r"(E:\myref\ref.bib)" #no!
# bibliography: "E:\\myref\\ref.bib" #no!
csl: "E:/myref/apa.csl" #https://www.zotero.org/styles
---
# R Markdown
#efron2004least
```{r}
# d0 = write.csv(iris[1:10,],file=r"(E:\mydata\xx.csv)")
d1 = read.csv(r"(E:\mydata\xx.csv)"); head(d1)
d2 = read.csv("E:\\mydata\\xx.csv"); head(d2)
```
# References
and works well, where ref.bib reads
#Article{efron2004least,
author = {Efron, Bradley and Hastie, Trevor and Johnstone, Iain and Tibshirani, Robert},
title = {Least angle regression},
journal = {The Annals of Statistics},
year = {2004},
volume = {32},
number = {2},
pages = {407--499},
}
and apa.csl is downloaded from https://www.zotero.org/styles. In particular, bibliography: "../myref/ref.bib" is also working!
After running ?Quotes in the Console I note that
## A Windows path written as a raw string constant:
r"(c:\Program files\R)"
Thus, I try bibliography: r"(E:\myref\ref.bib)" and bibliography: "E:\\myref\\ref.bib", but these two choices failed. I don't know how to fix them. Or the latter two choices are not permitted in the yaml? (PS: The latter two work for the xx.csv data file case!)

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

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.

! Illegal unit of measure (pt inserted) PDF Knit Error

I'm trying to knit a report as a PDF in RStudio. The code I have so far works fine (I've been testing it chunk by chunk) until it gets to the below chunk:
## HCT-CI
```{r}
summaryTable(y=wdata$HCTCI.score, table.title="HCT-CI Raw Score", y.labels="Number of Readmissions")
summaryTable(y=wdata$HCTCI.score, table.title="Readmissions", freq.tab=T, y.name="HCT-CI Raw Score")
summaryTable(y=wdata$HCTCI.3cat, table.title="HCT-CI Score (3 Groups)", caption_heading="Note:", caption="1: Score=0; 2: Score=1-2; 3: Score=3+", freq.tab=T, y.labels=c("0", "1-2", "3+"))
```
\pagebreak
My goal is to generate the output successfully as a PDF.
It's the same functions and format I've used in all the other chunks. When I run the chunk by itself, it also works. I've looked up the issue on google and other stackoverflow posts, but am having a hard time applying their solutions as they all use HTML or a different language.
My full report looks something like this (with some chunks taken out for the sake of brevity):
DOCUMENT BEGINS
---
title:
- \vspace{2in}Analysis for Study$\colon$ Readmissions
subtitle:
- \vspace{.2in}\emph{`r format(Sys.time(), '%d %B, %Y')`}
author:
- \emph{PI$\colon$ Name}
- \emph{College}
- \vspace{.5in}\emph{Author}
- \emph{Position}
- \emph{email}
date:
- \vspace{4in}
header-includes:
- \usepackage{fancyhdr}
- \usepackage{titling}
- \usepackage{caption}
- \fancyhead[L]{\includegraphics[width=2cm]{pic.png}}
- \fancyhead[R]{\includegraphics[width=4cm]{pic.png}}
- \fancyfoot[C]{Department}
- \fancypagestyle{plain}{\pagestyle{fancy}}
geometry: left=.5in,right=.5in,top=1in,bottom=1in
mainfont: Times New Roman
output:
pdf_document:
keep_tex: true
highlight: tango
toc: true
toc_depth: 4
number_sections: true
fig_caption: true
df_print: kable
fontsize: 12pt
---
\captionsetup[table]{labelformat=empty}
```{r setup, include=FALSE}
source("Revised Code.R")
knitr::opts_chunk$set(fig.path='Figs/', fig.align='center', fig.pos="H", echo=FALSE, warning=FALSE, message=FALSE, results = 'asis', fig.width = 6.5, fig.height = 4.5, dev = 'pdf')
\pagebreak
Summary of ___ within 100 days
summaryTable(wdata, table.title="Within 100 Days", y.labels="Number of Readmissions")
summaryTable(wdata, table.title="Within 100 Days", y.labels="Number of Readmissions", freq.tab=T)
summaryTable(wdata, table.title="Within 100 Days", freq.tab=T, y.name="Readmission", y.labels=c("No Readmission", "Any Readmission"))
\pagebreak
Association Between Readmissions and Donor Type
summaryTable(y=wdata$donor, table.title="Donor Type", freq.tab=T, y.name="Donor Type")
summaryTable(y=wdata$donor.3cat, table.title="Association Between Readmissions and Donor Type (3 Groups)", freq.tab=T, y.name="Donor Type", y.labels=c("Matched Sibs", "MUD", "Others"))
regTable.logistic(formula=wdata$readm.any~as.factor(wdata$donor.3cat), response = "Readmission", y.name="Donor Type (3 Groups)", y.labels=c("MUD vs Matched Sibs", "Others vs Matched Sibs"))
summaryTable(y=wdata$readm.any, bygroup=as.numeric(wdata$donor.3cat), freq.tab=T, bygroup.name="Donor Type", y.name="Readmissions", bygroup.labels=c("Matched Sibs", "MUD", "Others"), caption_heading="Note:", caption = "0: No Readmission; 1: Any Readmission")
\pagebreak
Association
HCT-CI
summaryTable(y=wdata, table.title="Raw Score", y.labels="Number of Readmissions")
summaryTable(y=wdata, table.title="Readmissions", freq.tab=T, y.name="Raw Score")
summaryTable(y=wdata, table.title="Score (3 Groups)", caption_heading="Note:", caption="1: Score=0; 2: Score=1-2; 3: Score=3+", freq.tab=T, y.labels=c("0", "1-2", "3+"))
\pagebreak
DOCUMENT ENDS
It's only when I attempt to knit it as a PDF do I get the following error:
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
! Illegal unit of measure (pt inserted).
<to be read again>
)
l.1949 \item
\underline{\textit{Note:}}
Error: Failed to compile Revised_Code.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See Revised_Code.log for more info.
Execution halted
How do I resolve the issue?
The "Illegal unit of measure (pt inserted)." error may also appear when a numerical value is entered for the chunk option out.height. For example out.height=200 generates this error, it should be replaced by out.height="200px".

Resources