Quarto and knitr to external file - r

I have a directory Project/Package/R with three files: script.R, knitr.Rmd and quarto.qmd.
This is knitr.Rmd:
---
title: "Essai"
output: pdf_document
---
```{r, include=FALSE}
library(here)
knitr::opts_knit$set(root.dir = here())
source(here("Package","R","script.R"), local = knitr::knit_global())
```
This is quarto.qmd:
---
title: "Essai"
format:
pdf:
toc: true
toc-depth: 2
---
```{r, include=FALSE}
library(here)
knitr::opts_knit$set(root.dir = here())
source(here("Package","R","script.R"), local = knitr::knit_global())
```
All is OK with knitr compilation, but quarto can't find the script R:
Quitting from lines 10-14 (stack.qmd)
Erreur dans readChar(con, 5L, useBytes = TRUE) :
impossible d'ouvrir la connexion
Appels : .main ... source -> withVisible -> eval -> eval -> load -> readChar
Exécution arrêtée
What is wrong? Thank you in advance.

Related

create PDF with preamble from rmarkdown::render

I am trying to create a PDF from R. I am successful from within RStudio (choosing "Knit to PDF"), but I get an error invoking from R (either with ctl-enter in Rstudio or with Rscript --vanilla invoke_from_R.R from the command line). My actual need is to create from the command line.
For example:
invoke_from_R.Rmd
---
title: "Lumber Jacks"
author: "Washington Irving"
date: "2022-09-19"
output:
pdf_document:
keep_tex: true
keep_md: true
dev: pdf
includes:
in_header: invoke_from_R_preamble.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Where are we?
Dove siamo?
invoke_from_R_preamble.tex
\newcommand\T{\rule{0pt}{2.6ex}} % Top strut
\newcommand\B{\rule[-1.2ex]{0pt}{0pt}} % Bottom strut
invoke_from_R.R
rmarkdown::render(
input = "invoke_from_R.Rmd"
, output_file = "invoke_from_R.pdf"
, output_format = rmarkdown::pdf_document(
rmarkdown::includes(in_header = "invoke_from_R_preamble.tex"),
toc = TRUE
, toc_depth = 2
, number_sections = FALSE
, keep_md = TRUE
, keep_tex = TRUE
, fig_width = 6.5
, fig_height = 4.5
, fig_crop = FALSE
)
)
The error that I get is:
$ Rscript --vanilla invoke_from_R.R
Error in !implicit_figures : invalid argument type
Calls: <Anonymous> ... <Anonymous> -> output_format -> pandoc_options -> from_rmarkdown
Execution halted
I have not yet found a workaround.
When I comment out this line:
rmarkdown::includes(in_header = "invoke_from_R_preamble.tex"),
I don't get the error, but I need to use the preamble because some of my LaTeX macros get processed even when I use the setting that is supposed to prevent this.
I am using (on Linux):
R version 4.2.1 (2022-06-23)
rmarkdown 2.14
Suggestions?
Thank you.

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}

How to fix errors in building a book in r bookdown

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.

Markdown not finding params when knitting

When I try to knit the following document
----
title: "jerHW52"
output: html_document
params:
missiontype: "Successful.Unmanned"
---
```{r warning=FALSE,echo=FALSE}
knitr::opts_chunk$set(echo=FALSE)
library(tidyr)
library(ggplot2)
Launch_Data<-read.csv("NASA_Launch_Data.csv",header=TRUE)
NASA_Launch_Data<- Launch_Data %>% filter(Launch_Data$Type==params$missiontype)
ggplot(NASA_Launch_Data, aes(x=Year, y=Missions)) + geom_line()
```
I get the following error
Error in filter(., Launch_Data$Type == params$missiontype) : object
'params' not found Calls: ... _fseq -> freduce ->
withVisible -> -> filter Execution halted
Why would it not be able to find params?
Here is the start of the data in NASA_Launch_Data.csv
"","Year","Type","Missions"
"1",1957,"Successful.Unmanned",3
"2",1958,"Successful.Unmanned",28
"3",1959,"Successful.Unmanned",23
"4",1960,"Successful.Unmanned",40
"5",1961,"Successful.Unmanned",48
"6",1962,"Successful.Unmanned",76
"7",1963,"Successful.Unmanned",67
"8",1964,"Successful.Unmanned",99
"9",1965,"Successful.Unmanned",119
"10",1966,"Successful.Unmanned",129
The answer is that I'm an idiot....
Note the YAML header at the top of my document
----
title: "jerHW52"
output: html_document
params:
missiontype: "Successful.Unmanned"
---
There are four dashes on the first line, not three as there should be. Remove one and everything works...

R: table of contents unused argument in tufte_handout in rmarkdown

I am trying to add table of contentsinto tufte_handout template in rmarkdown package as it is suggest here at the bottom of a page but when I creat yaml like this:
---
title: "Tufte Handout"
author: "John Smith"
date: "August 13th, 2014"
output:
rmarkdown::tufte_handout:
toc: true
---
I recieve an error like this:
Error in (function (fig_width = 4, fig_height = 2.5, fig_crop = TRUE, :
unused argument (toc = TRUE)
Calls: <Anonymous> -> create_output_format -> do.call -> <Anonymous>
Execution halted
Anyone knows how to handle this issue?

Resources