R Markdown is a document authoring format used by many data scientists. In this lab, you will explore some of the advanced formatting features of R Markdown to achieve a professional look.
Goal: by the end of this lab, you will be able to format an article in R Markdown using many advanced features.
An R Markdown document can be rendered into many different formats. Since the piece we are writing is for the Web, we will render our document into HTML. In addition to the
knitr chunk options that control how your R code gets rendered, R Markdown provides a number of features that can make your HTML document more expressive.
These features can be unlocked by setting parameters in the YAML header. YAML is an abbreviation for “Yet Another Markup Language”, and it is just a syntax for specifying options (like you might in a configuration file).
The following features are described in the R Markdown HTML documentation. Please consult that for instructions on how to use these features.
For the web, it’s a good idea to make your figures as wide as the text around which they are inserted. Please also use captions to contextualize the graphic! [By “figures”, here we mean data graphics—not images.]
fig_widthYAML setting and note how it changes the figure widths.
You can have two plots go side-by-side by using the
out.width chunk options. Set
"hold" and set
"50%". This will result in each plot only occupying half of the space allotted to the line.
library(tidyverse) <- ggplot(mtcars, aes(x = cyl, y = mpg)) + plot geom_point() # left image plot# right image + plot geom_line()
If you have to display a large data table, it would be nice to allow your readers the chance to page and scroll through it. Use the Paged Printing option by setting
df_print: paged in YAML.
--- title: "My document" output: html_document: df_print: paged ---
Please use code folding, with the default set to
code_foldingfor your Markdown document and set the default to
If you want to experiment with different themes, choose one from this Bootswatch theme gallery.
In addition to having chunks, you can include R code inline. This is very convenient when you have a sentence that contains a value that you need to compute. For example, you can write the current date like this:
`r format(Sys.Date(), "%B %e, %Y")`The date is
and it will render like this: The date is February 4, 2021.
Here is another example: the average of the first 100 uniform random numbers that I drew was 0.518509.
# Sample solution `r format(Sys.time(), "%B %e, %Y at %H:%M:%S"))`This document was rendered at
Recall that R Markdown is an extension to the more general document authoring syntax called Markdown. A popular and versatile software for rendering Markdown documents is Pandoc. Thus, R Markdown documents can leverage many (all?) of the features available through Pandoc Markdown. These features have nothing to do with R coding, but provide a rich (and growing) authoring experience.
I now write all of my letters, class notes, slides, websites, articles, and even my CV in Markdown (or LaTeX). Word processing software is dead to me!
Please see the extensive Pandoc Markdown documentation. Use only what you need!
In particular, check out:
In this lab, your overall goal is to mimic the appearance of “Three Reasons ‘Wonder Woman’ Has Already Made History”, an article published at FiveThirtyEight.com. Please consult the R Markdown HTML documentation here:
Copy the text of this article and paste it into a new R Markdown document. Add the paragraph breaks manually if they are not already there.
Add the image of Wonder Woman.
Add some links to the text, where appropriate.
Add a footnote.
Add the data graphic as an embedded image.
Use the Paged data frame Printing option and display a data table. [It doesn’t matter what the content of the data table is.]
Change the Bootstrap theme.
Insert an inline bit of R code that will automatically display the current date when the page is rendered.
Please respond to the following prompt on Slack in the
Prompt: What questions do you still have about the HTML features of R Markdown?