# template elements
2018-04-25
Bobae Kang
(Bobae.Kang@illinois.gov)
Source: R Markdown
“R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code.”
- Garret Grolemund from R Studio
rmarkdown
YAML header
Knit and preview
Knit options
Preview options
Markdown
Headers
# Header 1
## Header 2
### Header 3
Font types
_italic_ __bold__
*italic* **bold**
~~strikethrough~~
superscript^2^
Lists
Unordered list | Ordered list
|
* Item 1 | 1. Item 1
* Item 1a | 1. Item 1a
* Item 2 | 2. Item 2
+ Item 2a | 1. Item 2a
- Item 2b | 2. Item 2b
Mixed list
1. Item 1
* Item 1a
* Item 2
1. Item 2a
+ Item 2b
Hyperlinks
[text with hyperlink](http://link.path)
Images


Blockquotes
> A line of text following the "> " is a blockquote.
A line of text following the “> ” is a blockquote.
Horizontal line/page break
More then three asteriks or dashs
***
******
------
Math equations
Inline math equations look like: $y = (x + 1)^2$
A block of math equations look like:
$$y = x^2 + 2x + 1$$
R code chunk
Insert a new code chunk
Ctrl + Alt + i
Run code chunks
Ctrl + Enter
# use the following to control global options
knitr::opts_chunk$set(eval = TRUE, echo = TRUE, ...)
_```{r eval = FALSE, echo = FALSE, ...}
knitr
chunk optionsCommonly used options
Option (default) | Description |
---|---|
eval (TRUE ) |
Evaluate code in chunk? If FALSE, only code is shown |
echo (TRUE ) |
Show code in chunk? If FALSe, only output is shown |
error (FALSE ) |
Preserve the error? If TRUE, knitting continues in case of errors |
message (TRUE ) |
Display code messages? |
warning (TRUE ) |
Display code warnings? |
include (TRUE ) |
Include the code chunk? If FALSE, neither code nor output is shown but code is still evaluated |
fig.show (asis ) |
How to show/arrange plots? ("asis" , "hold" , "animate" , "hide" ) |
fig.width , fig.height (7 ) |
Plot width and height in inches. |
knitr::kable()
DT::datatable()
Default print example
my_table
my_fruits my_animals my_flavors my_colors my_cities
1 apple dogs chocolate red Chicago
2 banana cats vanila green New Work
3 clementine llamas cookie dough orange Los Angeles
kable
example
knitr::kable(my_table)
my_fruits | my_animals | my_flavors | my_colors | my_cities |
---|---|---|---|---|
apple | dogs | chocolate | red | Chicago |
banana | cats | vanila | green | New Work |
clementine | llamas | cookie dough | orange | Los Angeles |
datatable
example
DT::datatable(my_table)
Source: Wikimedia Commons
output
in the YAML header
Common output formats
output value |
Creates |
---|---|
html_document |
HTML file |
html_notebook |
R Notebook HTML file |
pdf_document |
PDF (requires Tex) file |
word_document |
Microsoft Word file |
md_document |
Markdown file |
github_document |
GitHub compatible markdown file |
Commonly used YAML render options
Option | Description | Available for |
---|---|---|
css |
CSS file to use to style document | html |
highlight |
Syntax highlighting | html, pdf, word |
number_sections |
Add section numbering to headers | html, pdf |
theme |
Bootswatch theme to use for page | html |
toc |
Add a table of contents | html, pdf, word, md, github |
toc_depth |
The lowest level of headings in toc | html, pdf, word, md, github |
toc_float |
Float the toc on the left | html |
Output appearances
"default"
, "tango"
, "pygments"
, "kate"
, "monochrome"
, "espresso"
, "zenburn"
, "haddock"
, and "textmate"
"default"
, "cerulean"
, "journal"
, "flatly"
, "readable"
, "spacelab"
, "united"
, "cosmo"
, "lumen"
, "paper"
, "sandstone"
, "simplex"
, and "yeti"
“An R Notebook is an R Markdown document with chunks that can be executed independently and interactively, with output visible immediately beneath the input.”
- “R Notebooks”, RStudio
.Rmd
) is savedhtmlwidgets
include:
plotly
and highcharter
for interactive visualizationsleaflet
for interactive mapsDT
for interactive data tableshtmlwidgets
for R website to find out more“RPubs is a quick and easy way to disseminate data analysis and R code and do ad-hoc collaboration with peers.”
- RStudio Team
Source: Wikimedia Commons
ioslides_presentation
(HTML)revealjs::revealjs_presentation
(HTML)slidy_presentation
(HTML)beamer_presentation
(PDF)---
title: "My first ioslide presentation"
output: ioslides_presentation
---
ioslides
output format is built into RStudio#
and ##
headingsioslides
, read the “Presentations with ioslides” article on RStudioioslides
sample source code
---
title: "My first ioslide presentation"
author: Bobae Kang
date: April 18, 2018
output: ioslides_presentation
---
# First section
## Normal slide
- Item one
- Item two
## Another slide | With a subtitle
This slide has a two-column layout
----

---
title: "My first revealjs presentation"
output: revealjs::revealjs_presentation
---
revealjs
R package, it is possible to use R Markdown (and its syntax) to generate slides using reveal.js, a JavaScript library for interactive slides in HTML
revealjs
, read the “Presentations with reveal.js” article on RStudioslidy
output: slidy_presentation
in the YAML headerbeamer
output: beamer_presentation
xaringan
output: xaringan::moon_reader
g
.RPres
) is savedSource: R Studio
“Shiny is an open source R package that provides an elegant and powerful web framework for building web applications using R. Shiny helps you turn your analyses into interactive web applications without requiring HTML, CSS, or JavaScript knowledge.”
- RStudio.com
runtime: shiny
and output: html_document
or output: ioslids_presentation
Structure of an interactive document
Source: “Introduction to interactive documents”. Shiny from R Studio
install.pacakges("shiny")
library(shiny)
runExample("01_hello")
server
and ui
server.R
and ui.R
app.R
function(input, output, session) {
## R code for server logic
}
server.R
contains the server-side logic of the applicationinput
object is an environment for storing user inputs modified by user's interaction with the application ui
output
object is an environment for storing elements (plots, tables, texts, etc) to be rendered and shown in the ui
session
object is an environment that can be used to access information relating to the session
session
is optionalfluidPage(
## R code for user interface
)
ui.R
defines the user interface elements:
shiny
offers many functions to take user inputsshiny
offers many functions to render outputs (as defined in server.R
)library(shiny)
server <- function(input, output) {
# server-side logic
}
ui <- fluidPage(
# user interface
)
shinyApp(ui, server)
app.R
must include shinyApp(ui, server)
at the endserver
is an object containing code for server-side logicui
is an object containing user interface elementsSource: Wikimedia Commons
_site.yml
file in the same folder as individual documents to be put together_site.yml
specifies the name and the routing structure of the resulting website_site.yml
and all HTML documents ready, run rstudio::render_site()
to generate the website_site.yml format example (Workshop website)
name: ICJIA R Workshop
output_dir: '.'
navbar:
title: ICJIA R WORKSHOP
right:
- text: Home
href: index.html
- text: Modules
href: modules.html
- text: About
href: about.html
output:
html_document:
includes:
after_body: include_footer.html
css: css/style.css
lib_dir: site_libs
self_contained: no
“GitHub Pages is a static site hosting service designed to host your personal, organization, or project pages directly from a GitHub repository.”
-“What is GitHub Pages?”, GitHub Help
bookdown
package, built on R Markdown, facilitates writing books and long articles/reports.
bookdown
publication is made downloadable in PDF, EPUB and MOBI formatsbookdown
package website to find out morebookdown
: Authoring Books and Technical Documents with R Markdown for a comprehensive guide for bookdown
bookdown
blogdown
is a package to generate static websites using R Markdown and the Hugo, a popular open-source static website generatorblogdown
examplesblogdown
: Creating Websites with R Markdown for a comprehensive guide for blogdown
Source: Giphy
References
Source: Wikimedia.org