In your project, create two directories: scripts
and reports
Create a script prepare.R
in the scripts
directory
Create an RMarkdown document from template, save it as final.Rmd
in the reports
directory.
Add code to prepare.R
to transform the flights
data in some way. (Use subsetting, add new variables, or compute a summary. Anything works.) Assign the result to a global variable using <-
. Save this variable to a file using the saveRDS()
function.
Hint: Use library(here)
and here()
to control where the file is saved.
Leave only the header and the “setup” chunk in final.Rmd
. In the “setup” chunk, insert a call to the readRDS()
function for the file you saved in the previous step.
Hint: Use here
again, you should be using the same file name that you have used for saveRDS()
.
Knit the document. This should work without failure (but show an empty document).
Create a second code chunk that shows a plot of your transformed data. Do not refer to flights
. Knit again.
A convenient way to create a website/an html document from several Rmd-files within a project is provided by the function rmarkdown::render_site()
. We will give an overview of how it works in this section, but you can always consult the help page of the function with ?rmarkdown::render_site()
.
An example of the workings of this function and the resulting html file can be found under the following link: https://github.com/krlmlr/vistransrep/tree/master/render-site-example. Just call render_site()
in the associated project (render-site-example.Rproj
) and the html-files will be generated. You can see the result when opening index.html
in your browser.
There are important ingredients that need to be present for this to work:
It is best to have all the Rmd-documents you want to include in the website in the main directory of the repository. This way render_site()
will detect the files by default and create html files from them in the directory given in "_site.yml".
Further possibilities:
You can setup any github-repo to automatically create and update a website from a subdirectory of the main repo directory called “doc”, containing the html file. This website will then be updated, whenever the html file changes in the master branch of the repo.
This here is a quick and easy way of binding together a few Rmd-Files as a website. For the creation of larger html-documents we strongly recommend using bookdown
instead. For more information see https://bookdown.org/.
Choose File / New File / R Markdown… from the RStudio menu. In the dialog, select “From template” in the pane on the left-hand side and then “Ninja presentation” to create an example slide deck. Knit it.
What templates are available from the “rticles” package? Are any of them useful for your work?
Navigate to https://github.com/rstudio/bookdown-demo (or search for “bookdown demo” in a search engine). Extract the demo to a directory of your choice, and follow the instructions in the README.md
file.
Copyright © 2019 Kirill Müller. Licensed under CC BY-NC 4.0.