Writing academic papers with RMarkdown and friends

Cynthia Huang

ECC4860/ETC4860 Workshop, 9 Aug 2022

Today's Agenda

Key Concepts

  • Reproducible documents
  • R Markdown
  • LaTex, Bookdown
  • Quarto, pandoc and more

Try it Yourself

  • Hands-on authoring in RStudio
  • Render a .pdf
  • Choose your own adventure

Getting Help

  • Search queries
  • Good questions
  • Further Resources
  • Why reproducible authoring?
  • How does R Markdown work?
  • What about Bookdown, LaTex, Quarto and more?

Key Concepts

Why Reproducibility?

Image source: An Introduction to R

Why R Markdown?

according to the internet...

Two fuzzy round monsters dressed as wizards, working together to brew different things together from a pantry (code, text, figures, etc.) in a cauldron labeled “R Markdown”. The monster wizard at the cauldron is reading a recipe that includes steps “1. Add text. 2. Add code. 3. Knit. 4. (magic) 5. Celebrate perceived wizardry.” The R Markdown potion then travels through a tube, and is converted to markdown by a monster on a broom with a magic wand, and eventually converted to an output by pandoc. Stylized text (in a font similar to Harry Potter) reads “R Markdown. Text. Code. Output. Get it together, people.”

Source: Stats Illustrations by Allison Horst

Anatomy of .Rmd

metadata

about the document

natural language text

with formatting

"instructions"

procedural code

with options on how to

evaluate the code

Image source: Applied R code

Under the Hood

Quarto: R Markdown 2.0

  • Render a .Rmd
  • Add code chunks
  • Add equations with LaTex
  • Add images, tables and citations

Try It Yourself

  • Make a new .Rproj in RStudio
  • Add a new .Rmd document to your project
  • Render the default content
  • Change the title of the document and render again
  • Add a table of contents

Render This

output:
  pdf_document:
    toc: TRUE
  • Write a sentence that contains inline R code
  • What's the difference between these two examples?

Add Content: Code

I ate `r 2 + 5` pieces of chocolate.
```{r}
n_choc <- 7
```
I ate `r n_choc` pieces of chocolate.
  • Add a sentence with a Greek Letter in it
  • Add an equation block
  • Switch to Visual Mode and back

Add Content: Math

This is the symbol for the greek letter lambda: $\lambda$

This is an equation:

$$
x + y = 17
$$
  • Stay in Visual Mode
  • Add a table using the Table menu
  • Include a math symbol inside your table
  • Change the caption of your table

Add Content: Table

  • Stay in Visual Mode
  • Drag and drop any image into your document
  • Copy and paste an image into your document
  • Insert a image using a link or local path

Add Content: Images

![local image](path/to/image)

![image from internet](image_url)
  • Stay in Visual Mode
  • Add a citation using the Insert -> Citation menu
  • Use the Cross-Ref search to find a paper you know
  • Add a sentence using that citation

Add Content: Citation

  • Add cross-refs with {bookdown}
  • Make advanced tables with {gt}
  • Use templates to produce prettier documents (.docx)

What about..?

{bookdown} for more

Bookdown: Install

Bookdown: Cross-Refs

{gt} for beautiful tables

MS Word .docx

  • RStudio's Visual mode
  • Asking good questions

Getting Help

Tutorials and How-Tos:

Quick References:

Into the weeds:

Resources

20220809_Rmarkdown-and-friends

By Cynthia Huang

20220809_Rmarkdown-and-friends

  • 280