The following code/output/plots were served as examples of presenting analysis using R Markdown.
A pseudo dataset was created to illustrate functions/codes. The data was created using random sampling completely, and the topic of the data is mimicking a class of college students.
library(rmarkdown)
library(knitr)
## Warning: package 'knitr' was built under R version 4.0.5
First thing first, check out this cheatsheet.
Next, check out the online version of the original book.
Under output:
, put indented xxxx_document:
, followed by certain document specifications, including:
xxxx_document:
toc: ture (include table of content, indented within xxxx_document:)
toc_depth: 3 (include top 3-levels of headers in the toc)
toc_float: ture (make the table of content float to the left of the document)
toc_collapsed: true (only show the top levels (H1-H2) in the table of contents)
code_folding: hide (show R code by default, but allow readers to hide the code if they wish)
df_print: default (a generic method of printing data frame), paged (cerate pageable table), kable (use knitr::kable function), tibble (print tibble version)
number_sections: true (allow the table of content automatically assign numbers to each section)
theme: whatever_you_choose (specify theme, e.g. default, cerulean, journal flatly, readable, spacelab, unitetd, cosmo, lumen, paper, sandstone, simplex, yeti, null)
highlight: tango (specify syntax highlighting style, e.g. default, tango, pygments, kate, monochrome, espresso, zenburn, haddock, textmate, null)
css: xxx.css (add customized CSS to the html document; this will require the theme & highlight to be set as null)
These specifications need to be further indented.
If you want to include parameter for the whole report, you can specify it in the YAML, like:
output: xxx_document
params:
date: !r lubridate::today()
year: !r lubridate::year(today())
location: "Tampa, FL"
author: "Eli"
data:
printcode: TRUE
Then a list called params
will be created containing all the specified object. However, note not to include rm(list=ls())
inside the markdown file. Otherwise, the params
object will be removed.
Subsequently, without specifying them in any R code chunk, you can call them from the params
list. Note, they can be used inside an R code chunk, and also in the specifications of the code chunk.
paste0("Today is ", params$date, ". This document is created by ", params$author, ", when he lives in ", params$location, ". He kept working on it during the summer of ", params$year, ".")
## [1] "Today is 2021-10-01. This document is created by Eli, when he lives in Tampa, FL. He kept working on it during the summer of 2021."
Instead of knitting the document directly from RStudio, one can choose to use the render function. While using the render function, one can change the existing parameters to create a different report. For example:
render("A personal summary of R Markdown coding.rmd",
output_format = "html_document",
params = list(year = 2018,
location = "Wuhan, Hubei",
author = "Yayi",
printcode = F))
It is obvious now that you can functionize this process and loop through the render function and create a list of documents using just the render function.
Believe it or not, you can also set the parameters in a cool and interactive interface by:
render("A personal summary of R Markdown coding.rmd",
output_format = "html_document",
params = "ask")
under xxxx_document:
, put indented includes:
, followed by certain styling specifications which should be also indented, including:
xxxx_document:
includes:
in_header: header.html (header.tex if using pdf_document)
before_body: before.html (similar as above, before.tex if using pdf_document)
after_body: after.html (...)
Note: all these html files should have the same YAML settings as the main document.
This is specified at the top-level, not under output:
.
editor_options: (can also include specified editor's option)
chunk_output_type: console (specify to show output in console, instead of the default which is below the code-chunk, indented within editor_options:)
One can use the exact same style of docx document in the R Markdown file they are writing. This is especially useful when you want to output a set of word_documents and keep them the same format.
word_documents:
reference_docx: "referenced.docx"
The prettydoc
package contains a few other themes (and code highlighting style) that are not included in the original rmarkdown package. One can use them by changing the YAML to:
output:
prettydoc::html_prettuy:
theme: cayman, tactile, architect, leonids, hpstr
highlight: github, vignette
The rmdformats
package also provides several html output formats of unique and attractive style:
output: rmdformats::material, readthedown, html_clean, html_docco
A logo can be added besides to title by including an R code chunk like the following:
|```{r, echo=F}
|htmltools::img(src = "logo.png",
| alt = 'logo',
| style = 'position:absolute; top:80px, right:30px; padding:10px; width:200px; height:65px;')
|```
See this RMD file for details of R Markdown application on presentation.
See this output file for the example of ioslide presentation.
library("rmarkdown")
library("knitr")
render("R Markdown Presentation.rmd", output_format = "ioslides_presentation", quiet = T)
See this RMD file for details of R Markdown application on dashboard.
library("rmarkdown")
library("knitr")
render("R Flex Dashboard.Rmd", quiet = T)
See this output file for example of Flex dashboard.
Shiny dashboard is also a great use of presenting data-based story in a dashboard format. We will look into that in more details when we get to read the book “Mastering Shiny” by Hadley Wickham.
R Markdown is also able to output other formats of files, such as:
Tufte Handouts;
xaringan presentation;
Website;
HTML document for R package;
books;
journal/manuscript;
interactive tutorial;
However, the above mentioned are not particularly what I use regularly, thus we’ll save them for the future. If I were ever need to use any of the above formats, I’ll be adding them to the main document.
In case you came from inline texts.
An example of footnote↩︎