Reports, Dashboards and Slides with R

Introduction

In this lesson we will learn how to create

  • Scientific reproducible reports, books, papers…

  • Slides

  • Statistical Dashboards

Before starting you have to make sure that LaTeX is installed in your computer:

Producing Reports

Overleaf

  • Create your own account

  • For your first document choose one template and change it! (Here’s a demo)

  • We will show:

    • How to add a figure and a table: Caption, position in the text, reference and labels?
    • How to add Bibliography: .bib file, several options: bibtex code: Jabref (for windows users) and Bibdeck (for Mac users) Or Google scholar

rmarkdown:

  • Document

    • HTML (for websites)
    • PDF (in Latex)
    • Word document
  • Presentation

    • HTML: 2 types isoslides and slidy
    • PDF: beamer presentations
    • PowerPoint (see Example below)
  • From template

    • Revealjs (Presentation)
    • Tufte (HTML and PDF)
    • Bioconductor (PDF)
    • Flexdashboard

Flexdashboard (from Template)

  • YAML
 ---
title: "rbokeh iris dataset"
author: "Ryan Hafen"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---
  • Loading packages that will be used
library(rbokeh)
library(flexdashboard)
  • Part 1

    • Declare the column
Column {data-width=600}
-----------------------------------------------------------------------
+ Make your plot
figure(width = NULL, height = NULL) %%
  ly_points(Sepal.Length, Sepal.Width, data = iris, color = Species)
  • Part 2

    • Declare 2nd column
Column {data-width=400}
-----------------------------------------------------------------------
  • Figure 2
figure(width = NULL, height = NULL, legend_location = "top_left") %%
  ly_quantile(Sepal.Length, group = Species, data = iris)

and

figure(width = NULL, height = NULL) %%
  ly_points(Sepal.Length, Sepal.Width, data = iris,
    color = Petal.Width)

Flexdashboard with Shiny app

  • YAML
---
title: "Iris K-Means Clustering"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
runtime: shiny
---
  • Loading packages
library(datasets)
data(iris)
  • Declaring the sidebar
Column {.sidebar}
-----------------------------------------------------------------------
  • Input part (UI)
selectInput('xcol', 'X Variable', names(iris))
    
selectInput('ycol', 'Y Variable', names(iris),
                selected=names(iris)[[2]])
    
numericInput('clusters', 'Cluster count', 3,
              min = 1, max = 9)
  • Declaring the Main panel
Column
-----------------------------------------------------------------------
  • Displaying results
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
  "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))

selectedData <- reactive({
  iris[, c(input$xcol, input$ycol)]
})

clusters <- reactive({
  kmeans(selectedData(), input$clusters)
})

renderPlot({
  par(mar = c(5.1, 4.1, 0, 1))
  plot(selectedData(),
       col = clusters()$cluster,
       pch = 20, cex = 3)
  points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})

Power Point with R (Example)

  • Installing packages:
> library(officer)
> library(magrittr)
  • Create an R object representing a PowerPoint document.
> my_pres <- read_pptx() 
  • To add a slide in your presentation
> my_pres <- my_pres %>% 
+   add_slide(layout = "Title and Content", master = "Office Theme")
  • You can now generate you pptx file
> print(my_pres, target = "first_example.pptx") 
  • We can check if the file is created
> file.exists("first_example.pptx") ## The answer should be TRUE 

We can now manipulate my_pres object to create more slides, write text on the slides or add figures:

  • A slide can be added with the add_slide() function.
> my_pres <- read_pptx() %>% 
+   add_slide(layout = "Two Content", master = "Office Theme") 

Our document will contain 1 slide

> length(my_pres)
  • We will add a text to the first slide and print in the pptx file
> my_pres <- my_pres %>% 
+   ph_with_text(type = "title", str = "A title", index = 1)
> 
> print(my_pres, target = "first_example.pptx")
  • We add date in the footnote
> my_pres<-my_pres %>%
+   ph_with_text(type = "dt", str = format(Sys.Date()), index = 1)
> 
> print(my_pres, target = "first_example.pptx")
  • We add a body text
> my_pres <- my_pres %>% 
+   ph_with_text(type = "body", str = "A first text", index = 1)
> 
> print(my_pres, target = "first_example.pptx")
  • we add an image
> img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
> 
> my_pres <- my_pres %>% 
+   ph_with_img(type = "body", index = 1, src = img.file, height = 1.06, width = 1.39 )
  • We will add now a ggplot figure in a new file. We will first add a slide without a title and “two contents”
> my_pres<-my_pres%>%
+   add_slide(layout = "Two Content", master = "Office Theme")
  • We create the ggplot
> library(ggplot2)
> gg_plot <- ggplot(data = iris ) +
+   geom_point(mapping = aes(Sepal.Length, Petal.Length), size = 3) +
+   theme_bw()
> gg_plot

  • We will add on the left column of the slide
> if( capabilities(what = "png") )
+   my_pres <- ph_with_gg(my_pres, value = gg_plot, index=1 )
  • We can then add a text in the right column of the slide
> my_pres <- my_pres %>%
+   ph_with_text(type = "body", str = "Iris Data", index = 2)
> 
> ## Save the file
> print(my_pres, target = "first_example.pptx")

Plots in LaTeX format: tikzDevice

We want to change this figure into LaTeX code

>  library(ggpubr)
>  data("ToothGrowth")
>  ggboxplot(ToothGrowth, x = "dose", y = "len", width = 0.8)
library(tikzDevice)
tikz("fig.tex",standAlone=TRUE)
 ggboxplot(df, x = "dose", y = "len", width = 0.8)
Measuring dimensions of: \char77
Measuring dimensions of: \char100
Measuring dimensions of: \char111
....
 dev.off()
null device 
          1

Latex file is created in your work directory

% Created by tikzDevice version 0.10.1 on 2017-11-21 18:43:00
% !TEX encoding = UTF-8 Unicode
\documentclass[10pt]{article}
\usepackage{tikz}

\usepackage[active,tightpage,psfixbb]{preview}

\PreviewEnvironment{pgfpicture}

\setlength\PreviewBorder{0pt}
\begin{document}

\begin{tikzpicture}[x=1pt,y=1pt]
\definecolor{fillColor}{RGB}{255,255,255}
.... # truncated output. 

HTML tables: DT package

Basic example

>  library(DT)
>  datatable(iris, options = list(
+  searching = FALSE,
+    pageLength = 5,
+    lengthMenu = c(5, 15, 35, 50)
+  ))

DT tables with options: color in one column

> datatable(iris, options = list(pageLength = 5)) %>%
+   formatStyle('Sepal.Length',  color = 'red', backgroundColor = 'orange', fontWeight = 'bold')

Adding buttons to download the table

> datatable(
+   iris, extensions = 'Buttons', options = list(
+     dom = 'Bfrtip',
+     buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
+   )
+ )

Reordering columns

You can click and drag the table header to move a certain column to a different place

> datatable(iris[1:10,], extensions = 'ColReorder', options = list(colReorder = TRUE))

HTML table with sjPlot package

> library(sjmisc)
> library(sjPlot)
> xCSS = list(css.table = "border: 2px solid;",
+                 css.tdata = "border: 1px solid;",
+                 css.firsttablecol = "color:#003399;
+                 font-weight:bold;")
> data(efc)
> tab_df(efc[1:5,], CSS=xCSS)
c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4 c86cop5 c87cop6 c88cop7 c89cop8 c90cop9 c160age c161sex c172code c175empl barthtot neg_c_7 pos_v_4 quol_5 resttotn tot_sc_e n4pstu nur_pst
16 2 2 83 3 3 2 2 2 1 1 2 3 3 56 2 2 1 75 12 12 14 0 4 0 NA
148 2 2 88 3 3 3 3 3 4 1 3 2 2 54 2 2 1 75 20 11 10 4 0 0 NA
70 1 2 82 3 2 2 1 4 1 1 1 4 3 80 1 1 0 35 11 13 7 0 1 2 2
168 1 2 67 4 4 1 3 1 1 1 1 2 4 69 1 2 0 0 10 15 12 2 0 3 3
168 2 2 84 4 3 2 1 2 2 2 1 4 4 47 2 2 0 25 12 15 19 2 1 2 2

Online LaTex and HTML tables

https://www.tablesgenerator.com

LaTeX and tables with xtable package

LaTeX

>  library(xtable)
> data(tli)
> head(tli) 

grade sex disadvg ethnicty tlimth 1 6 M YES HISPANIC 43 2 7 M NO BLACK 88 3 5 F YES HISPANIC 34 4 3 M YES HISPANIC 65 5 8 M YES WHITE 75 6 5 M NO BLACK 74

> tli$grade<-tli$grade+rnorm(nrow(tli))
>  tli.table <- xtable(tli[1:3,],align=c("l","c","l","l","c","c"),
+                      digits=c(0,2,0,0,0,0),
+                      caption="My table",label="tab1")
> tli.table
% latex table generated in R 3.5.1 by xtable 1.8-3 package % Fri Nov 2 09:25:24 2018

HTML

> print(tli.table,type="html")

Models in LaTeX and HTML tables with stargazer package

Data Summary

>  library(stargazer)
>  data("attitude")
>  head(attitude,3)

rating complaints privileges learning raises critical advance 1 43 51 30 39 61 92 45 2 63 64 51 54 63 73 47 3 71 70 68 69 76 86 48

>  stargazer(attitude,flip=T)
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu % Date and time: Fri, Nov 02, 2018 - 09:25:24

Display the data

>  stargazer(attitude[1:5,], summary=FALSE)
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu % Date and time: Fri, Nov 02, 2018 - 09:25:24

Displaying Estimating and comparing Models

> library(stargazer)
> attitude$high.rating <- (attitude$rating > 70)
> linear.1 <- lm(rating ~ complaints + privileges + learning 
+                         + raises + critical, data=attitude)
> 
> linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
> probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude,
+                     family = binomial(link = "probit"))
>  
> stargazer(linear.1, linear.2, probit.model, title="Regression Results")
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu % Date and time: Fri, Nov 02, 2018 - 09:25:24

In HTML format

> library(stargazer)
> attitude$high.rating <- (attitude$rating>70)
> linear.1 <- lm(rating ~ complaints + privileges + learning 
+                         + raises + critical, data=attitude)
> 
> linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
> probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude,
+                     family = binomial(link = "probit"))
>  
> stargazer(linear.1, linear.2, probit.model,
+           type="html",
+           title="Regression Results",
+           single.row=TRUE,
+           ci=TRUE, ci.level=0.9, 
+           omit.stat=c("f", "ser"))
Regression Results
Dependent variable:
rating high.rating
OLS probit
(1) (2) (3)
complaints 0.692*** (0.447, 0.937) 0.682*** (0.470, 0.894)
privileges -0.104 (-0.325, 0.118) -0.103 (-0.316, 0.109)
learning 0.249 (-0.013, 0.512) 0.238* (0.009, 0.467) 0.164*** (0.077, 0.252)
raises -0.033 (-0.366, 0.299)
critical 0.015 (-0.227, 0.258) -0.001 (-0.073, 0.072)
advance -0.062 (-0.131, 0.007)
Constant 11.011 (-8.240, 30.262) 11.258 (-0.779, 23.296) -7.476** (-13.349, -1.604)
Observations 30 30 30
R2 0.715 0.715
Adjusted R2 0.656 0.682
Log Likelihood -9.087
Akaike Inf. Crit. 26.175
Note: p<0.1; p<0.05; p<0.01