Skip to content

Instantly share code, notes, and snippets.

@cpsievert
Created February 24, 2021 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsievert/7e87f78d324509b27af624afe61fba99 to your computer and use it in GitHub Desktop.
Save cpsievert/7e87f78d324509b27af624afe61fba99 to your computer and use it in GitHub Desktop.
---
title: "My Themed R Markdown Document"
author: "Author: Your Name"
date: "Last update: `r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
theme:
bg: "#101010"
fg: "#FDF7F7"
primary: "#ED79F9"
base_font: !expr bslib::font_google("Fira Sans")
heading_font: !expr bslib::font_google("Merriweather")
code_font: !expr bslib::font_google("JetBrains Mono")
code-color: skyblue
font-base-size: 1.25rem
css: tabsets.scss
toc: true
toc_float:
collapsed: true
smooth_scroll: true
toc_depth: 3
fig_caption: yes
code_folding: show
code_download: true
number_sections: true
---
```{r, echo = FALSE}
thematic::thematic_rmd()
```
# R Markdown
## Overview
R Markdown combines markdown (an easy to write plain text format) with embedded
R code chunks. When compiling R Markdown documents, the code components can be
evaluated so that both the code and its output can be included in the final
document. This makes analysis reports highly reproducible by allowing to automatically
regenerate them when the underlying R code or data changes. R Markdown
documents (`.Rmd` files) can be rendered to various formats including HTML and
PDF. The R code in an `.Rmd` document is processed by `knitr`, while the
resulting `.md` file is rendered by `pandoc` to the final output formats
(_e.g._ HTML or PDF). Historically, R Markdown is an extension of the older
`Sweave/Latex` environment. Rendering of mathematical expressions and reference
management is also supported by R Markdown using embedded Latex syntax and
Bibtex, respectively.
## Quick Start
### Install R Markdown
```{r install_rmarkdown, eval=FALSE}
install.packages("rmarkdown")
```
### Initialize a new R Markdown (`Rmd`) script
To minimize typing, it can be helful to start with an R Markdown template and
then modify it as needed. Note the file name of an R Markdown scirpt needs to
have the extension `.Rmd`. Template files for the following examples are available
here:
+ R Markdown sample script: [`sample.Rmd`](https://raw.githubusercontent.com/tgirke/GEN242/gh-pages/_vignettes/07_Rbasics/sample.Rmd)
+ Bibtex file for handling citations and reference section: [`bibtex.bib`](https://raw.githubusercontent.com/tgirke/GEN242/gh-pages/_vignettes/07_Rbasics/bibtex.bib)
Users want to download these files, open the `sample.Rmd` file with their preferred R IDE
(_e.g._ RStudio, vim or emacs), initilize an R session and then direct their R session to
the location of these two files.
### Learning Markdown
The basic syntax of Markdown and derivatives like kramdown is extremely easy to learn. Rather
than providing another introduction on this topic, here are some useful sites for learning Markdown:
+ [Markdown Intro on GitHub](https://guides.github.com/features/mastering-markdown/)
+ [Markdown Cheet Sheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
+ [Markdown Basics from RStudio](http://rmarkdown.rstudio.com/authoring_basics.html)
+ [R Markdown Cheat Sheet](http://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf)
+ [kramdown Syntax](http://kramdown.gettalong.org/syntax.html)
### Tables
There are several ways to render tables. First, they can be printed within the R code chunks. Second,
much nicer formatted tables can be generated with the functions `kable`, `pander` or `xtable`. The following
example uses `kable` from the `knitr` package.
```{r kable}
library(knitr)
kable(iris[1:12,])
```
A much more elegant and powerful solution is to create fully interactive tables with the [`DT` package](https://rstudio.github.io/DT/).
This JavaScirpt based environment provides a wrapper to the DataTables library using jQuery. The resulting tables can be sorted, queried and resized by the
user.
```{r dt}
library(DT)
datatable(iris, filter = 'top', options = list(
pageLength = 100, scrollX = TRUE, scrollY = "600px", autoWidth = TRUE
))
```
### Figures
Plots generated by the R code chunks in an R Markdown document can be automatically
inserted in the output file. The size of the figure can be controlled with the `fig.height`
and `fig.width` arguments.
```{r some_jitter_plot, warning=FALSE}
library(ggplot2)
dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
ggplot(dsmall, aes(color, price/carat)) + geom_jitter(alpha = I(1 / 2), aes(color=color))
```
# Tab sets {.tabset}
## Summary {.tab}
Here is a summary table.
```{r}
summary(cars)
```
## Plot {.tab}
Here is a plot:
```{r echo=FALSE}
plot(pressure)
```
# Pill sets {.tabset .tabset-pills}
## Summary {.pill}
Here is a summary table.
```{r}
summary(cars)
```
## Plot {.pill}
Here is a plot:
```{r echo=FALSE}
plot(pressure)
```
// https://rstudio.github.io/bslib/articles/theming.html#utility-classes-1
.tab {
@extend .p-3; @extend .border; @extend .border-top-0; @extend .rounded-bottom;
}
.pill {
@extend .p-3; @extend .border; @extend .rounded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment