Bookdown Download for HTML single document - r

I have a bookdown book with multiple output formats.
I want the user to be able to download the content in multiple formats. This works wonders with PDF, EPUB and so on. But it does not work with single file HTML bookdown::html_document2 documents as they are rendered in the present working directory . and not the _book folder.
E.g., when I specify bookdown::git_book: ... it gets created in _book.
When I use bookdown::pdf_book: ... it also gets created in _book.
However, when I use bookdown::html_document2: ..., it gets created in ..
Setting output_dir for the single page document didn't work.
Do you know how to solve this?
MWE
## _bookdown.yml
book_filename: "The-book"
delete_merged_file: yes
## _output.yml
bookdown::gitbook:
split_by: rmd
config:
download:
- ["The-book.pdf", "PDF"]
- ["The-book.html", "HTML"]
bookdown::html_document2:
toc: true
bookdown::pdf_book:
keep_tex: no
dev: "cairo_pdf"
latex_engine: xelatex
and then in 01-intro.Rmd
# Intro
This is a test

I make book directly from a R script and this work for me:
bookdown::render_book(
input = "index.Rmd",
output_format = "bookdown::html_document2",
output_file = "_book/The-book.html"),
)
By making the book from R script you can start to have fun with name (like adding the date) and directory of the book, noting that if the directory is not _book you will need to create it before. With pdf output you can use output_dir to have the directory created for you, but it seems not to work and even conflict with output_file for html_document2. Ex:
## PDF report
bookdown::render_book(
input = "index.Rmd",
output_format = "bookdown::pdf_document2",
output_file = paste0("Proceedings-pdf-", format(Sys.time(), format = "%Y-%m-%d-%H%M%S"), ".pdf"),
output_dir = "Proceedings"
)
## HTML
dir.create("Proceedings", showWarnings = F)
bookdown::render_book(
input = "index.Rmd",
output_format = "bookdown::html_document2",
output_file = paste0("Proceedings/Proceedings-html-", format(Sys.time(), format = "%Y-%m-%d-%H%M%S"), ".html"),
)

Quarto is able to produce stand-alone html files (https://quarto.org/docs/output-formats/html-basics.html#self-contained). E.g.
---
project:
type: book
format:
html:
embed-resources: true
---
Note that this currently does not work if you specify an output path (see here).

Related

Rmarkdown: how to import all the *.bib files in a directory for bibliography?

In Rmarkdown file, how to import all the *.bib files in a directory for bibliography?
I have the following file tree
./
./the_Rmd_file_in_question.Rmd
./includes/
./includes/x.bib
./includes/y.bib
...
The followings don't work.
bibliography:
- ./includes/*.bib
or
bibliography:
`r library(stringr); str_replace(list.files(path = './report/includes/', pattern = '^.+\\.bib$', full.names = TRUE), pattern = './\\w+/', replacement = './')`
You can do this, but it's a little tricky. What Pandoc wants to see in the bibliography field is something like
bibliography: ["file1.bib", "file2.bib"]
I can get that with this code:
bibliography: ["`r paste(c('file1.bib', 'file2.bib'), collapse='\",\"')`"]
Given your updated structure of the directory, I think this should work:
bibliography: ["`r paste(list.files(path = './includes', pattern = '^.+\\.bib$', full.names = TRUE), collapse='\",\"')`"]
The list.files() call will give you usable names, you don't need to modify it.

Instructing RMarkdown to use relative filepaths or stop shortening folder names

I have a parameterized RMarkdown PDF report that I am running from a Shiny dashboard (after copying it to a temporary directory).
Example code:
Shiny dashboard:
---
title: "Company Report Frontend"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE}
library(flexdashboard)
library(shiny)
```
### Select Company
```{r}
textInput('name', label ='Firm')
```
### Report
```{r}
uiOutput("downloadUI")
# Create the actual downloadButton
output$downloadUI <- renderUI( {
downloadButton("downBtn", "Download Report")
})
# Add download handling
output$downBtn <- downloadHandler(
filename = "full_report.pdf",
content = function(file) {
tempReport <- file.path(tempdir())
file.copy("test_report.Rmd", paste0(tempReport, "/test_report.Rmd"), overwrite = TRUE)
rmarkdown::render(paste0(tempReport, "/test_report.Rmd"), output_file = file,
params = list(input=input),
envir = new.env(parent = globalenv()), clean = FALSE,
knit_root_dir = tempReport,
)
}
)
```
test_report.RMD:
---
title: "Report"
header-includes:
\usepackage{graphicx}
params:
input: NA
output:
pdf_document:
latex_engine: xelatex
---
```{r}
input <- params$input
data(mtcars)
plot(mtcars$hp, mtcars$mpg)
title(input$name)
```
When I run it, it successfully produces the .tex file but then can't compile it. When I try to compile the .tex file directly in a LaTeX editor, I get undefined control sequence and Missing endcsname defined errors on any \includegraphics lines like this one:
\includegraphics{C:/User/LONGUS~1/Temp/file5b381fa967c8_files/figure-latex/unnamed-chunk-1-1.pdf}
where LONGUS~1 is a shortened folder name from the actual Windows username LONGUSERNAME.
The error goes away, and the PDF compiles, if I replace LONGUS~1 with LONGUSERNAME, or just point it to the relative filepath. LaTeX does tend to get finicky about filepaths sometimes.
How can I instruct RMarkdown to either avoid shortening folder names, or skip the absolute filepath and just use the relative? test_report.RMD compiles fine if I run it by itself (and specify a default input), so I'd guess this is something to do with the use of tempdir() or at least something in the render() function. But I really should keep the tempdir() stuff in the case of multiple simultaneous users of the shiny app. I did try removing the knit_root_dir option but that didn't fix it.
Any suggestions welcome. Thank you!

Text (not syntax) highlighting in Rmarkdown/Papaja

I'm compiling a report using Papaja and Rmarkdown, and I want to highlight various text throughout. Doing so with latex or pure Rmarkdown is easy, but I'm receiving an "undefined control sequence" error when I compile the report to PDF using Papaja's application of Knitr.
A similar question about text highlighting within a single Rmarkdown file was answered here: RMarkdown / pandoc fails to knit Pdf with latex color commands. I'd like to know if an answer exists for multiple Rmarkdown files using Papaja.
I'll include a minimal example below.
1) File called papaja_file.Rmd
---
# https://crsh.github.io/papaja_man/index.html
title : "Some Title"
shorttitle : "HEADER"
author:
- name : "Name R Here"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Include addresss"
email : "randomemail#usa.edu"
affiliation:
- id : "1"
institution : "Any University"
author_note: |
....
abstract: |
Text here for abstract.
keywords : "Keyword1, keyword2, keyword3"
bibliography : ["references.bib"]
figsintext : no
figurelist : no
tablelist : no
footnotelist : no
lineno : yes
lang : "english"
class : "man"
output : papaja::apa6_pdf
---
```{r load_packages, include = FALSE}
library("papaja")
```
```{r analysis_preferences}
# Seed for random number generation
set.seed(42)
```
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', echo = TRUE, warning = FALSE, message = FALSE)
```
```{r child = 'child_document.Rmd'}
```
\newpage
# References
```{r create_references, echo = F}
r_refs(file = "references.bib")
```
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
Notice that it references a single child Rmarkdown document.
2) The child document with text, called child_document.Rmd
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
One sentence without highlighting. \hl{Another with highlighting.}
3) The preamble, called preamble.tex
\usepackage{color}
\usepackage{soul}
\definecolor{lightblue}{rgb}{0.90, 0.95, 1}
\sethlcolor{lightblue}
Knitting the main papaja file produces the error. Removing the \hl command within the child document allows the pdf to compile without issue.
Result after putting YAML header within the main papaja document rather than the child:
The YAML header in your child document is not evaluated, c.f.
The master document, for example, consists of the YAML front matter and includes the children, which are themselves R Markdown documents without a YAML front matter.
https://crsh.github.io/papaja_man/tips-and-tricks.html#splitting-an-r-markdown-document
But you can include your preamble.tex in you master file using:
output:
papaja::apa6_pdf:
includes:
in_header: preamble.tex
c.f. https://github.com/rstub/stackoverflow/commit/a92a67d4721ee9c06e995b08adbf8cb89daebcb4
Result:

How to include R code within Latex in YAML header for custom-formatted RMarkdown Template

I am creating an R package "mytemplate" containing derived RMarkdown format from rmarkdown::pdf_document (as an R script function with report as the output, it calls header.tex file):
report <- function() {
## location of resource files in the package
header <- system.file("resources/header.tex", package = "mytemplate")
## derives the style from default PDF template
rmarkdown::pdf_document(
latex_engine = "xelatex", fig_caption = FALSE,
includes = rmarkdown::includes(in_header = header)
)
}
Within the header.tex I use an image file detected by system.file() rested in the resources/ folder in the inst/ package directory:
\usepackage{fancyhdr}
\thispagestyle{fancy}
\fancyhead[LC]{
\includegraphics{`r system.file("resources/cover.png", package = "mytemplate")`}
}
Outside of my package and providing full YAML section in the .Rmd file, the pdf renders OK:
---
title: ""
output:
pdf_document:
latex_engine: xelatex
fig_caption: false
header-includes:
\usepackage{fancyhdr}
\thispagestyle{fancy}
\fancyhead[LC]{
\includegraphics{`r system.file("resources/cover.png", package = "mytemplate")`}
}
---
text
But after installation when I use mytemplate::report as RMarkdown output, I am returned the error:
! LaTeX Error: File ``r system.file("resources/cover.png", package = "mytemp
late")`' not found.
Is calling header.tex in the R script that's causing the error, or should I modify header.tex code and how?
You are using an inline R chunk inside a tex document. That cannot work.
Instead, use the argument pandoc_args of pdf_document() to pass variables to pandoc. Inside the header.tex you can then use a pandoc variable:
args <- pandoc_variable_arg("cover", system.file("resources/cover.png", package = "mytemplate"))
report <- function() {
## location of resource files in the package
header <- system.file("resources/header.tex", package = "mytemplate")
## derives the style from default PDF template
rmarkdown::pdf_document(
latex_engine = "xelatex", fig_caption = FALSE,
includes = rmarkdown::includes(in_header = header),
pandoc_args = args # pass to pandoc
)
}
And header.tex:
\usepackage{fancyhdr}
\thispagestyle{fancy}
\fancyhead[LC]{
\includegraphics{$cover$}
}

Rmarkdown: different output folders, shared libs

I have an .R file wherein I for each unique value in a list, render a number of different .Rmd files. Something like this:
for (uddannelse in unique(c("Monkey","Horse"))) {
rmarkdown::render("file1.Rmd", output_dir=file.path(getwd(), uddannelse) ,output_file=paste("file1", uddannelse,".html", sep="_"), encoding="UTF-8")
rmarkdown::render("file2.Rmd", output_dir=file.path(getwd(), uddannelse), output_file=paste("file2", uddannelse,".html", sep="_"), encoding="UTF-8")
}
As evident by the render parameters, the html-output should go into a separate folder for each value in the list, in the above example: folder: "Monkey" and folder "Horse".
Each .Rmd file has the following front matter (the files go to a static html website and needs to have self_contained: false:
---
output:
html_document:
theme: readable
self_contained: false
lib_dir: pub/libs
css: pub/libs/custom_css/custom.css
date: "`r format(Sys.time(), '%d %B, %Y')`"
---
However: When I call the render functions I recieve this error:
Error in relativeTo(basepath, dir) :
The path C:/Users/ac/Dropbox/2014_07_WIP/pub/libs/jquery-1.11.0 does not appear to be a descendant of C:/Users/ac/Dropbox/2014_07_WIP/Monkey/
So I guess the rmarkdwown::render first creates the lib directories relative to the Rmd file, yet expect the files to be placed relative to the output files.
How can I get around this, so that I can have a set of common Rmd input files in one folder, and have output in different folders, yet share a common lib?
I tried to place something like this in the fronmatter.
---
output:
html_document:
theme: readable
self_contained: false
lib_dir: "`r file.path(uddannelse, "libs")`"
css: "`r file.path(uddannelse, "libs", "custom_css", "custom.css")`"
date: "`r format(Sys.time(), '%d %B, %Y')`"
---
And I got this error:
Error in yaml::yaml.load(front_matter) :
Parser error: while parsing a block mapping at line 3, column 5did not find expected key at line 5, column 50
I solved my immediate problem by passing some of the front matter in the render call:
rmarkdown::render("file1.Rmd",
output_dir=file.path(uddannelse),
output_file=paste("file1", uddannelse,".html", sep="_"),
output_options=list(html_document =
list(self_contained = FALSE,
lib_dir = file.path(uddannelse, "lib"),
css = paste("lib", "custom_css", "custom.css",
sep="/"),
include = list(
after_body = file.path(uddannelse,
"footer_w_index.html")))),
encoding="UTF-8")
Notice that lib_dir has to be relative to the Rmdfile and css has to be relative to the output-file.
For some reason - regardless of whether I use paste or file.path(fsep="/", ...) the css path in the output file is linked with windows separator ("\") - and thus not usable in e.g. Firefox.

Resources