R Markdown

Prof. Dr. Mirco Schoenfeld

data reports

Data reports. What’s that again?


(Munroe 2021)

  • Cites other papers / journal articels / textbooks / data reports
  • Presents experimental results / calculations
  • Illustrates scenarios using drawings
  • References items in the text
  • Contains graphs / shows data

Two models of work

Two models of work.

  1. Office Model

  1. Engineering Model

The difference between those models

  1. Office Model

  1. Engineering Model

The Office Model

  1. Office Model

Center of your project: Word file

  • Changes are tracked inside that file
  • Citation & Reference managers plug into Word
  • Output of data analysis is pasted into the file
  • Often, the word file is the output

The Engineering Model

Center of your project: repository

  • Changes are tracked outside of files
  • Data analysis is kept in code
  • Code produces output reproducibly
  • Citation & references in separate files
  • Final output is assembled
  1. Engineering Model

draw the owl

R Markdown

Getting Started.

https://rmarkdown.rstudio.com/articles_intro.html (Grolemund 2014)

https://daringfireball.net/projects/markdown/basics (Gruber 2004)

Basic Document

Create a basic document with the following content

---
output:
  pdf_document: default
  html_document: default
---

# Say Hello to markdown

Markdown is an **easy to use** format for writing reports.
It resembles what you naturally write every time you compose an email. In fact, you may have already used markdown *without realizing it*. These websites all rely on markdown formatting

## Subheading

Fantastic!

* [Github](www.github.com)
* [StackOverflow](www.stackoverflow.com)
* [Reddit](www.reddit.com)
* and surely others do as well!

# Chapter Two Starts Here

Let me cite some clever person:

> Dorothy followed her through many of the beautiful rooms in her castle.

Document Metadata

Now, adjust the header of the file:

---
title: "Hello World in Markdown"
author: "Mirco"
date: "24. April 2024"
---

# Say Hello to markdown

Markdown is an **easy to use** format for writing reports.
[...]

Combined data analyses

Now, for the best part, add this to the end of your document:

## What is cool about R Markdown

It is easy to show what you did by including code and output of code. The max speed of a car in our database is `r max(cars)`mp/h. We have `r nrow(cars)` cars in our dataset.

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

With references

To use references, adjust the header and end of your document:

---
title: "Hello World in Markdown"
author: "Mirco"
date: "27. October 2022"
output:
  pdf_document: default
  html_document:
    df_print: paged
csl: apa.csl
bibliography: references.bib
---

[...]

# References

With references

…create a references.bib file…

@book{2020-healy-plain_text,
  author ={Healy, Kieran},
  title= {{The Plain Person’s Guide to Plain Text Social Science}},
  url={https://plain-text.co/},
  publisher={The Internet},
  year={2019}
}

…and download the apa.csl here (or look for more )

It’s your turn

  1. Download the dataset to the folder of your R Markdown file
    Don’t forget to set the working directory
  2. Within the R Markdown file, load the dataset using read.csv
    The file is tab-separated
  3. Visualize the data using the plot-function
    temperature on x-axis, pressure on y-axis

And please take a look at the next slide

It’s your turn - a tip

In case you face issues installing rmarkdown package on Windows

You need to install RTools: https://cran.rstudio.com/bin/windows/Rtools/

And please check

download.file("https://cran.rstudio.com/src/contrib/PACKAGES", "text.txt")

if that results in an error:

In download.file(“https://cran.rstudio.com/src/contrib/PACKAGES”, : URL ‘https://cran.rstudio.com/src/contrib/PACKAGES’: Status war ‘SSL connect error’

Then this helps:

options("download.file.method"="wininet")

and after that install rmarkdown package

Further Reading

Advanced Usage:

https://plain-text.co/pull-it-together.html#pull-it-together (Healy 2019)

References

Grolemund, Garrett. 2014. Introduction to R Markdown. R Studio. https://rmarkdown.rstudio.com/articles_intro.html.
Gruber, John. 2004. Markdown. The Internet. https://daringfireball.net/projects/markdown/basics.
Healy, Kieran. 2019. The Plain Person’s Guide to Plain Text Social Science. The Internet. https://plain-text.co/.
Munroe, Randall. 2021. “Types of Scientific Papers.” https://xkcd.com/2456/.