### Utilities. Do not modify. # Installation of packages if necessary InstallPackages <- function(Packages) { InstallPackage <- function(Package) { if (!Package %in% installed.packages()[, 1]) { install.packages(Package, repos="https://cran.rstudio.com/") } } invisible(sapply(Packages, InstallPackage)) } # Basic packages InstallPackages(c("bookdown", "formatR", "kableExtra", "magick", "ragg")) # kableExtra must be loaded if (knitr::opts_knit$get("rmarkdown.pandoc.to") == "docx") { # Word output (https://stackoverflow.com/questions/35144130/in-knitr-how-can-i-test-for-if-the-output-will-be-pdf-or-word) # Do not use autoformat (https://github.com/haozhu233/kableExtra/issues/308) options(kableExtra.auto_format = FALSE) } library("kableExtra") # Chunk font size hook: allows size='small' or any valid Latex font size in chunk options def.chunk.hook <- knitr::knit_hooks$get("chunk") knitr::knit_hooks$set(chunk = function(x, options) { x <- def.chunk.hook(x, options) ifelse(options$size != "normalsize", paste0("\n \\", options$size,"\n\n", x, "\n\n \\normalsize"), x) }) # Output hook for full-width figures defOut <- knitr::knit_hooks$get("plot") knitr::knit_hooks$set( plot = function(x, options) { # Apply the original hook x <- defOut(x, options) # Only full-width figures if (options$out.width == "\\widthw") { # Modify output of the original hook, see code on: # https://github.com/yihui/knitr/blob/master/R/hooks-latex.R # Modify align1 output with align=center. Original code: # switch(a, left = '\n\n', center = '\n\n{\\centering ', right = '\n\n\\hfill{}', '\n') # replace centering by a minipage x <- gsub( "\\n\\n\\{\\\\centering", "\\\\setbox0=\\\\hbox\\{\\\\begin\\{minipage\\}\\[h\\]\\{\\\\widthw\\}\\\\centering", x ) # Modify align2 output with align=center. Original code: # switch(a, left = '\\hfill{}\n\n', '\n\n}\n\n', right = '\n\n', '') # replace } by the necessary code to place the minipage x <- gsub( "\\n\\n\\}\\n\\n", "\\\\end\\{minipage\\}\\}\\\\needspace\\{\\\\ht0+\\\\dp0+2\\\\baselineskip\\}\\\\definesHSpace\\\\hspace\\{-\\\\rf\\}\\\\box0", x ) } return(x) } ) # Figures with side captions if (rmarkdown::metadata$largemargins) knitr::opts_chunk$set(fig.env='SCfigure')
### Customized options for this document # Add necessary packages here Packages <- c("tidyverse") # Install them InstallPackages(Packages) # Create packages.bib file to reference them as @R-package knitr::write_bib(Packages, file="packages.bib") # knitr options knitr::opts_chunk$set( cache = FALSE, # Cache chunk results include = TRUE, # Show/Hide chunks echo = TRUE, # Show/Hide code warning = FALSE, # Show/Hide warnings message = FALSE, # Show/Hide messages # Figure alignment and size fig.align = 'center', out.width = '80%', fig.asp = .75, # Graphic devices (ragg_png is better than standard png) dev = c("ragg_png", "pdf"), # Code chunk format tidy = TRUE, tidy.opts = list(blank=FALSE, width.cutoff=60), size = "scriptsize", knitr.graphics.auto_pdf = TRUE ) options(width = 60) # ggplot style library("tidyverse") theme_set(theme_bw()) theme_update(panel.background=element_rect(fill="transparent", colour=NA), plot.background=element_rect(fill="transparent", colour=NA)) knitr::opts_chunk$set(dev.args=list(bg="transparent")) # Random seed set.seed(973)
This document allows you to create a book in PDF format (and ePub format) at the same time as an HTML version to be published on the web. The syntax is that of Markdown with some extensions.
The bookdown package must be installed from CRAN or GitHub:
install.packages("bookdown") # or the development version # devtools::install_github("rstudio/bookdown")
The book is organized in chapters.
Each chapter is an Rmd file, whose name normally begins with its number (e.g. 01-intro.Rmd
).
All Rmd files in the project folder are actually treated as chapters, sorted by filename.
The index.Rmd file is special: it contains the document header and the first chapter.
This first chapter is placed in the foreword of the printed book: it should not be numbered (hence the {-}
code next to the title) in the HTML version.
It must end with the LaTeX command \mainmatter
which marks the beginning of the body of the book.
The outline levels start with #
for chapters (only one per file), ##
for sections, etc.
Compilation in PDF format is done by XeLaTeX, which must be installed.
While writing, it is strongly advised to create only the HTML file, which is much faster than a LaTeX compilation. Each chapter can be viewed very quickly by clicking on the Knit button above the source window. The entire book is created by clicking on the Build Book button in the RStudio Build window. The button's drop-down list allows you to create all documents or limit yourself to one format.
\mainmatter
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.