| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/render_site.R |
| 3 |
\name{render_site} |
| 4 |
\alias{render_site} |
| 5 |
\alias{clean_site} |
| 6 |
\alias{site_generator} |
| 7 |
\alias{site_config} |
| 8 |
\alias{default_site_generator} |
| 9 |
\title{Render multiple documents as a website} |
| 10 |
\usage{ |
| 11 |
render_site( |
| 12 |
input = ".", |
| 13 |
output_format = "all", |
| 14 |
envir = parent.frame(), |
| 15 |
quiet = FALSE, |
| 16 |
encoding = "UTF-8" |
| 17 |
) |
| 18 |
|
| 19 |
clean_site(input = ".", preview = TRUE, quiet = FALSE, encoding = "UTF-8") |
| 20 |
|
| 21 |
site_generator(input = ".", output_format = NULL) |
| 22 |
|
| 23 |
site_config(input = ".", encoding = "UTF-8") |
| 24 |
|
| 25 |
default_site_generator(input, output_format_filter = NULL, ...) |
| 26 |
} |
| 27 |
\arguments{ |
| 28 |
\item{input}{Website directory (or the name of a file within the directory).} |
| 29 |
|
| 30 |
\item{output_format}{R Markdown format to convert to (defaults to "all").} |
| 31 |
|
| 32 |
\item{envir}{The environment in which the code chunks are to be evaluated |
| 33 |
during knitting (can use \code{\link{new.env}} to guarantee an empty new |
| 34 |
environment).} |
| 35 |
|
| 36 |
\item{quiet}{\code{TRUE} to suppress messages and other output.} |
| 37 |
|
| 38 |
\item{encoding}{Ignored. The encoding is always assumed to be UTF-8.} |
| 39 |
|
| 40 |
\item{preview}{Whether to list the files to be removed rather than actually |
| 41 |
removing them. Defaulting to TRUE to prevent removing without notice.} |
| 42 |
|
| 43 |
\item{output_format_filter}{An optional function which is passed the |
| 44 |
input file and the output format, and which returns a (potentially |
| 45 |
modified) output format.} |
| 46 |
|
| 47 |
\item{...}{Currently unused.} |
| 48 |
} |
| 49 |
\value{ |
| 50 |
\code{render_site} returns the name of the site output file (relative |
| 51 |
to the input directory). \code{clean_site} returns the names of the |
| 52 |
generated files removed during cleaning. \code{site_config} returns the |
| 53 |
contents of _site.yml as an R list. \code{default_site_generator} returns |
| 54 |
the default site generator for R Markdown websites. |
| 55 |
} |
| 56 |
\description{ |
| 57 |
Render all of the R Markdown documents within a directory as a website. |
| 58 |
} |
| 59 |
\details{ |
| 60 |
The \code{render_site} function enables you to render a collection of |
| 61 |
markdown documents within a directory as a website. There are two |
| 62 |
requirements for a directory to be rendered as a website: |
| 63 |
\enumerate{ |
| 64 |
\item{It must contain either an "index.Rmd" or "index.md" file.} |
| 65 |
\item{It must contain a site configuration file ("_site.yml").} |
| 66 |
} |
| 67 |
|
| 68 |
The most minimal valid website is an empty "index.Rmd" and an empty |
| 69 |
"_site.yml". With this configuration a single empty webpage would be |
| 70 |
generated via a call to \code{render_site}. If you add additional markdown |
| 71 |
documents to the directory they will also be rendered. By default a site is |
| 72 |
rendered in the following fashion: |
| 73 |
|
| 74 |
\enumerate{ |
| 75 |
\item{R Markdown (.Rmd) and plain markdown (.md) files in the root |
| 76 |
directory are rendered. Note however that markdown files beginning with "_" |
| 77 |
are not rendered (this is a convention to designate files that are included |
| 78 |
by top level documents).} |
| 79 |
\item{All output and supporting files are copied to a "_site" subdirectory |
| 80 |
of the website directory (this is configurable, see discussion below).} |
| 81 |
\item{The following files are \bold{not} copied to the "_site" |
| 82 |
sub-directory: |
| 83 |
\itemize{ |
| 84 |
\item{Files beginning with "." (hidden files).} |
| 85 |
\item{Files beginning with "_"} |
| 86 |
\item{Files known to contain R source code (e.g. ".R", ".s", ".Rmd"), R |
| 87 |
data (e.g. ".RData", ".rds"), configuration data (e.g. ".Rproj", |
| 88 |
"rsconnect") or package project management data (e.g. |
| 89 |
"packrat", "renv").} |
| 90 |
} |
| 91 |
Note that you can override which files are included or excluded via |
| 92 |
settings in "_site.yml" (described below).} |
| 93 |
\item{Normally R Markdown renders documents as self-contained HTML. |
| 94 |
However, \code{render_site} ensures that dependencies (e.g. CSS, |
| 95 |
JavaScript, images, etc.) remain in external files. CSS/JavaScript |
| 96 |
libraries are copied to a "site_libs" sub-directory and plots/images are |
| 97 |
copied to "_files" sub-directories.} |
| 98 |
} |
| 99 |
|
| 100 |
You can remove the files generated by \code{render_site} using the |
| 101 |
\code{clean_site} function. |
| 102 |
} |
| 103 |
\section{Configuration}{ |
| 104 |
|
| 105 |
A "_site.yml" file can be used to configure the behavior of site generation. |
| 106 |
Here is an example configuration file: |
| 107 |
|
| 108 |
\preformatted{ |
| 109 |
name: my-website |
| 110 |
output_dir: _site |
| 111 |
include: ["demo.R"] |
| 112 |
exclude: ["docs.txt", "*.csv"] |
| 113 |
navbar: |
| 114 |
title: "My Website" |
| 115 |
left: |
| 116 |
- text: "Home" |
| 117 |
href: index.html |
| 118 |
- text: "About" |
| 119 |
href: about.html |
| 120 |
output: |
| 121 |
html_document: |
| 122 |
toc: true |
| 123 |
highlight: textmate |
| 124 |
} |
| 125 |
|
| 126 |
The \code{name} field provides a suggested URL path for your website when it |
| 127 |
is published (by default this is just the name of the directory containing |
| 128 |
the site). The \code{output_dir} indicates which directory to copy site |
| 129 |
content into ("_site" is the default if none is specified). Note that this |
| 130 |
can be "." to keep all content within the root website directory alongside |
| 131 |
the source code. |
| 132 |
|
| 133 |
The \code{include} and \code{exclude} fields enable you to override the |
| 134 |
default behavior vis-a-vis what files are copied into the "_site" directory |
| 135 |
(wildcards can be used as in the above example). |
| 136 |
|
| 137 |
The \code{navbar} field can be used to define a navigation bar for websites |
| 138 |
based on the \code{\link{html_document}} format. |
| 139 |
|
| 140 |
Finally, the \code{output} field enables you to specify output options that |
| 141 |
are common to all documents within the website (you can also still provide |
| 142 |
local options within each document that override any common options). |
| 143 |
|
| 144 |
\code{new_session: true} causes each file to be rendered in a new R session. |
| 145 |
This prevents the masking problem that arises when different files use |
| 146 |
functions from different packages (namespaces) that share a common name, such |
| 147 |
as \code{here::here} and \code{lubridate::here} or \code{dplyr::filter} and |
| 148 |
\code{MASS::filter}. The default behaviour of \code{render_site} is to use a |
| 149 |
common R session. |
| 150 |
|
| 151 |
\code{autospin: true} causes \code{.R} files to be spinned and rendered |
| 152 |
(as well as \code{.Rmd} files). If \code{autospin} is set to false (the default), |
| 153 |
\code{.R} files will not be spinned nor rendered. \code{autospin} can also |
| 154 |
enumerate a list of .R files to be spinned and rendered. |
| 155 |
} |
| 156 |
|
| 157 |
\section{Custom Site Generation}{ |
| 158 |
|
| 159 |
The behavior of the default site generation function |
| 160 |
(\code{rmarkdown::default_site}) is described above. It is also possible to |
| 161 |
define a custom site generator that has alternate behavior. A site generator |
| 162 |
is an R function that is bound to by including it in the "site:" field of the |
| 163 |
"index.Rmd" or "index.md" file. For example: |
| 164 |
|
| 165 |
\preformatted{ |
| 166 |
title: "My Book" |
| 167 |
output: bookdown::gitbook |
| 168 |
site: bookdown::bookdown_site |
| 169 |
} |
| 170 |
|
| 171 |
A site generation function should return a list with the following elements: |
| 172 |
\describe{ |
| 173 |
\item{\code{name}:}{The name for the website (e.g. the parent directory |
| 174 |
name).} |
| 175 |
\item{\code{output_dir}:}{The directory where the website output is written |
| 176 |
to. This path should be relative to the site directory (e.g. "." or |
| 177 |
"_site")} |
| 178 |
\item{\code{render}:}{An R function that can be called to generate the |
| 179 |
site. The function should accept the \code{input_file}, |
| 180 |
\code{output_format}, \code{envir}, and \code{quiet} arguments.} |
| 181 |
\item{\code{clean}:}{An R function that returns relative paths to the files |
| 182 |
generated by \code{render_site} (these files are the ones which will be |
| 183 |
removed by the \code{clean_site} function.} |
| 184 |
\item{\code{subdirs} \emph{(optional)}:}{A logical flag that indicates if |
| 185 |
the generator supports nested source files in subdirectories of the project |
| 186 |
(\code{TRUE}) or only at the project root (\code{FALSE}). (e.g. |
| 187 |
\code{blogdown:::blogdown_site()})} |
| 188 |
} |
| 189 |
|
| 190 |
Note that the \code{input_file} argument will be \code{NULL} when the entire |
| 191 |
site is being generated. It will be set to a specific file name if a |
| 192 |
front-end tool is attempting to preview it (e.g. RStudio IDE via the Knit |
| 193 |
button). |
| 194 |
|
| 195 |
When \code{quiet = FALSE} the \code{render} function should also print a line |
| 196 |
of output using the \code{\link{message}} function indicating which output |
| 197 |
file should be previewed, for example: |
| 198 |
|
| 199 |
\preformatted{if (!quiet) |
| 200 |
message("\nOutput created: ", output) |
| 201 |
} |
| 202 |
|
| 203 |
Emitting this line enables front-ends like RStudio to determine which file |
| 204 |
they should open to preview the website. |
| 205 |
|
| 206 |
See the source code of the \code{rmarkdown::default_site} function for a |
| 207 |
example of a site generation function. |
| 208 |
} |
| 209 |
|