RStudio includes a variety of features intended to make developing R Markdown websites more productive. These features are available only in the current Preview Release of RStudio so you should download and install it before trying out the features described below.
Note that all of the RStudio features for website authoring described below require the use of an RStudio Project tied to your website's directory. See the documentation on RStudio Projects for additional information on how to create and use projects.
As you work on the individual pages of your website you can render them using the Knit button just as you do with conventional standalone R Markdown documents:
Knitting an individual page will only render and preview that page, not the other pages in the website.
To render all of the pages in the website you use the Build pane, which calls rmarkdown::render_site
to build and then preview the entire site:
RStudio supports "live preview" of changes that you make to supporting files within your website (e.g. CSS, JavaScript, Rmd partials, R scripts, YAML config files, etc.).
Changes to CSS and JavaScript files always result in a refresh of the currently active page preview. Changes to other files (e.g. shared scripts and config files) trigger a re-knit of the active page preview (this behavior can be disabled via the options dialog available from the Build pane).
Note that only the active page is re-knit so once you are happy with the results of rendering you should be sure to rebuild the entire site from the Build pane to ensure that all pages inherit your changes.
When working iteratively on a page you might find it more convenient to preview it side-by-side with the editor rather than in an external window. You can configure RStudio to do this using the options menu on the editor toolbar:
If you aren't working within RStudio and/or want to build sites from the command line you can call the render_site
function directly from within the website directory. Pass no arguments to render the entire site or a single file in order to render just that file:
# render the entire site rmarkdown::render_site() # render a single file only rmarkdown::render_site("about.Rmd")
To clean up all of the files generated via render_site
you can call the clean_site
function, which will remove all files generated by rendering your site's markdown documents including knitr _cache
directories. You can specify the preview = FALSE
option to just list the files to be removed rather than actually removing them:
# list which files will be removed rmarkdown::clean_site(preview = TRUE) # actually remove the files rmarkdown::clean_site()
If your website is time consuming to render you may want to enable knitr caching during the development of the site so that you can more rapidly preview changes. To enable caching for an individual chunk just add the cache = TRUE
chunk option:
```{r, cache = TRUE}
data <- longComputation()
```
To enable caching for an entire document add cache = TRUE
to the global chunk option defaults:
```{r setup, include=FALSE}
knitr::opts_chunk$set(cache=TRUE)
```
Note that when knitr caching is enabled for an Rmd it's _files
directory will be copied rather than moved to the _site
directory (since the cache requires references to generated figures in the _files
directory).
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.