knitr

knitr #

Straight from the source

Rendering and Keeping Visuals #

I often use R and its host of visualization packages + knitr to prepare and render visualizations (for a bunch of things on R-based visualizations, check out my visualizations section).

For static images, I like knitting and keeping the distinct files for the visuals.

Here’s a simple Rmarkdown code block that allows me to set up an .Rmd to be knit and keep the individual files.

Replace the quotes with backticks when actually using this.

---
title: "test"
output:
  html_document: 
    keep_md: yes
  html_notebook: default
---

"""{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
"""

"""{r packages}
library(tidyverse)
library(knitr)
"""

"""{r data}
# read in the data
"""

"""{r plot1, fig.width=16, fig.height=9, eval=TRUE}
# prepare the visuals
"""

There can be many plot blocks. Note that I can use an eval= flag to either render or not render a visual.

The fig.width and fig.height specifications are useful for setting output proportions. For a typical slide deck, a 16 x 9 ratio usually allows for a full-screen fit.

Once the document is not, the directory looks something like this:

├── test.Rmd
├── test.html
├── test.md
├── test.nb.html
└── test_files
    └── figure-html
        └── plot-1.png

Grab rendered visuals from the figure-html directory and use them as you please.