9 Reporting


9.1 R and R-Markdown

We will use R Markdown for communicating results to each other. Note that R and R Markdown are both languages. R studio interprets R code make statistical computations and interprets R Markdown code to produce pretty documents that contain both writing and statistics. Altogether, your project will use

  • R: does statistical computations
  • R Markdown: formats statistical computations for sharing
  • Rstudio: graphical user interface that allows you to easily use both R and R Markdown.

Homework reports are probably the smallest document you can create. These little reports are almost entirely self-contained (showing both code and output). To make them, you will need to

First install Pandoc on your computer.

Then install any required packages

# Packages for Rmarkdown
install.packages("knitr")
install.packages("rmarkdown")

# Other packages frequently used
#install.packages("plotly") #for interactive plots
#install.packages("sf") #for spatial data

9.2 Simple Reports

We will create reproducible reports via R Markdown.

Example 1: Data Scientism.

See DataScientism.html and then create it by

  • Clicking the “Code” button in the top right and then “Download Rmd”
  • Open with Rstudio
  • Change the name and title to your own, make other edits
  • Then point-and-click “knit”

Alternatively,

  • Download the source file from DataScientism.Rmd
  • Change the name and title to your own, make other edits
  • Use the console to run
rmarkdown::render('DataScientism.Rmd')

Example 2: Homework Assignment. Below is a template of what homework questions (and answers) look like. Create a new .Rmd file from scratch and produce a .html file that looks similar to this:

Problem: Simulate 100 random observations of the form y=xβ+ϵ and plot the relationship. Plot and explore the data interactively via plotly, https://plotly.com/r/line-and-scatter/. Then play around with different styles, https://www.r-graph-gallery.com/13-scatter-plot.html, to best express your point.

Solution: I simulate 400 observations for ϵ2×N(0,1) and β=4, as seen in this single chunk. Notice an upward trend.

# Simulation
n <- 100
E <- rnorm(n)
X <- seq(n)
Y <- 4*X + 2*E
# Plot
library(plotly)
dat <- data.frame(X=X,Y=Y)
plot_ly( data=dat, x=~X, y=~Y)
# To Do:
# 1. Fit a regression line
# 2. Color points by their residual value

9.3 Posters and Slides

Posters and presentations are another important type of scientific document. R markdown is good at creating both of these, and actually very good with some additional packages. So we will also use flexdashboard for posters and beamer for presentations.

Poster.

See DataScientism_Poster.html and recreate from the source file DataScientism_Poster.Rmd. Simply change the name to your own, and knit the document.

Slides.

See DataScientism_Slides.pdf

Since beamer is a pdf output, you will need to install Latex. Alternatively, you can install a lightweight version TinyTex from within R

install.packages('tinytex')
tinytex::install_tinytex()  # install TinyTeX

Then download source file DataScientism_Slides.Rmd, change the name to your own, and knit the document.

If you cannot install Latex, then you must specify a different output. For example, change output: beamer_presentation to output: ioslides_presentation on line 6 of the source file.

9.4 More Literature

For more guidance on how to create Rmarkdown documents, see

If you are still lost, try one of the many online tutorials (such as these)

Some other good packages for posters/presenting you should be aware of