Data Analysis with R

11 - R Markdown for communication

Saskia A. Otto
Postdoctoral Researcher

R Markdown

How to go from R to any output format for sharing your results?

By using R Markdown

  • R Markdown is an easy-to-write plain text formatter designed to make web content, reports or presentations easy to create,
  • can weave the outputs of your R code, like figures and tables, with text to create a report,
  • supports not only the reproducibility of your analysis but also the entire report,
  • supports various different static as well as dynamic output formats.
  • How does it work? R Markdown encapsulates various processs into a single render function:

modified from the R Markdown cheat sheet (under CC-BY-SA license)

A quick introduction

How to create an .Rmd file

Structure of an .Rmd file

Structure of an .Rmd file

Rendering Output

(Markdown) Syntax

HTML vs Markdown vs R Markdown Syntax

Get more infos

Your turn...

Task: Convert an R script into a Markdown file

Start your first R Markdown file that should render a html document and save it under any name. In lecture 8 on visualizations you were asked to answer the following questions using the hydro dataset (file "data/1111473b.csv"):

  1. What happens if you make a scatterplot of station (x) vs temp (y)? Why is the plot not useful? What would be a better plot?
  2. What happens if you make a boxplot of cruise (x) vs psal (y)? Why is this plot less suitable? What could be an alternative?

If you have done this exercise you can simply use your code and copy and paste it into the code chunks of your .Rmd file. If you haven't done the exercise you have now the opportunity to make up leeway.

Implement the following in your .Rmd file

  1. Start with R code chunks for loading the data and required libraries
  2. Add code chunks for i) any data modifications ii) any plot
  3. Think about which code chunks should be evaluated (eval=TRUE) or not displayed (echo=FALSE)
  4. Think about the dimensions of the figure and add the required specifications in the chunk options
  5. Include different headers and subheaders
  6. Add your answers and think about whether you want to use i) any ordered or unordered lists ii) text in bold or italic
  7. Add a webside link that fits to the topic
  8. Add an image

Note:

Try to knit your .Rmd file frequently (after any major addition)!! It is highly likely that you will run into an error message and that way you can identify the cause much faster.

Now its time for your FIRST CASE STUDY!!!

Photo by NASA (ID ISS040-E-12110), accessed here

How do you feel now.....?

Totally confused?

Totally bored?

plot of chunk unnamed-chunk-7

Once your done, change in the YAML header the output format to e.g. PDF and knit your .Rmd file again. How do you like that output? Play around with all the options and output formats that R Markdown provides. Convert any of your R scripts you wrote so far into an .Rmd file

Totally content?

Thank You

For more information contact me: saskia.otto@uni-hamburg.de

http://www.researchgate.net/profile/Saskia_Otto
http://www.github.com/saskiaotto

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License except for the
borrowed and mentioned with proper source: statements.
Image on title and end slide: Section of an infrared satallite image showing the Larsen C ice shelf on the Antarctic
Peninsula - USGS/NASA Landsat: A Crack of Light in the Polar Dark, Landsat 8 - TIRS, June 17, 2017
(under CC0 license)