15/04/2016

ReporteRs

an R package for reporting automation

Motivations

Reporting automation from R

MS formats are still ubiquitous in corporate environments

Formats and complex layouts are strong requirements

Document updates and graphical annotations are time consuming

Deploiement must be easy

Solution

An R API for creating

  • MS documents
  • that look like corporate documents
  • with complex tables
  • and editable graphics where specific annotations can be made by non R coders

Example

Graphical and tabular objets to use (1)

library(ReporteRs)
## Loading required package: ReporteRsjars
library(magrittr)
library(ggplot2)

myplot <- ggplot(data = mtcars,
                 aes(x = wt, y = qsec,
                     color = drat)) + geom_point()
  
my_ft <- vanilla.table(mtcars)

Example

Graphical and tabular objets to use (2)

myplot

Example

Graphical and tabular objets to use (3)

Word example

docx() %>% 
  addTitle("Tabular example") %>% 
  addFlexTable( my_ft ) %>% 
  addTitle("Graphic example") %>% 
  addPlot(fun = print, x = myplot, vector.graphic = TRUE) %>% 
  writeDoc("docs/first_.docx")

It produces this document.

PowerPoint example

pptx() %>% 
  addSlide("Title and Content") %>% 
  addTitle("Tabular example") %>% 
  addFlexTable( my_ft ) %>% 
  addSlide("Title and Content") %>% 
  addTitle("Graphic example") %>% 
  addPlot(fun = print, x = myplot) %>% 
  writeDoc("docs/first_.pptx")

It produces this document.

Workflow

add functions

addFlexTable addPlot
addParagraph addTitle
addTOC addBreakPage
addSlide addSection
addImage addMardown

FlexTable

build complex formatted tables

FlexTable features

  • Layout
    • Free table header and footer
    • Merge cells (by rows or columns)
  • Format : cells, borders, paragraphs, text
  • Annotate : add text and images

FlexTable output

FlexTable API

FlexTable example

data

## Source: local data frame [12 x 6]
## Groups: Status, Gender [?]
## 
##          Status Gender Ulceration     n  mean    sd
##           (chr)  (chr)      (chr) (int) (dbl) (dbl)
## 1         Alive Female     Absent    68 1.693 2.004
## 2         Alive Female    Present    23 2.972 2.593
## 3         Alive   Male     Absent    24 1.468 1.719
## 4         Alive   Male    Present    19 4.319 2.423
## 5      Melanoma Female     Absent     8 2.139 1.184
## 6      Melanoma Female    Present    20 4.724 4.128
## 7      Melanoma   Male     Absent     8 3.266 4.681
## 8      Melanoma   Male    Present    21 5.143 2.862
## 9  Non-melanoma Female     Absent     3 1.667 1.141
## 10 Non-melanoma Female    Present     4 3.302 3.713
## 11 Non-melanoma   Male     Absent     4 2.420 2.499
## 12 Non-melanoma   Male    Present     3 8.053 4.019

FlexTable example

layout (1)

my_flextable = FlexTable( data ) %>%
  spanFlexTableRows(j = 1, runs = data$Status) %>% 
  spanFlexTableRows(j = 2, runs = data$Gender) %>% 
  addHeaderRow( value = c("Group by", "Statistics"), 
                colspan = c(3, 3), first = T ) %>% 
  setFlexTableWidths(widths = c(1.5, 1.5, 1.5, .75, .75, .75))

FlexTable example

layout (2)

FlexTable example

format (1)

my_flextable[to = "header"] = textBold(font.size=12)
my_flextable[to = "header"] = parCenter(padding=2)

my_flextable[] = textNormal(font.size=11)

my_flextable[data$mean < 2, "mean" ] = 
  textItalic(color = "gray", font.size=11)
my_flextable[data$mean > 3, "mean" ] = 
  textBold(color = "red", font.size=11)

my_flextable[] = parCenter(padding=2)

FlexTable example

format (2)

FlexTable example

annotate (1)

my_flextable[data$sd > 2, "sd", 
             text.properties = textProperties(
               vertical.align = "superscript") ] = "*"
my_flextable <- addFooterRow(my_flextable, 
       value = "*", colspan = 6, 
       text.properties = textProperties(
         vertical.align = "superscript") )
my_flextable[1, 1, to = "footer"] = 
  "standard deviation is > 2"

FlexTable example

annotate (2)

Editable Vector Graphics

Features

Vector graphics

  • high quality rendering
  • scaled without loss of image resolution

Editable

  • Graphics can be annotated
  • Modifications can be disabled

Function addPlot

  • Use option vector.graphics=TRUE
pptx() %>% addSlide("Title and Content") %>% 
  addPlot(fun = print, x = myplot, 
    vector.graphic = TRUE) %>% 
  writeDoc("docs/evg_.pptx")

docx() %>% addPlot(fun = print, 
    x = myplot, vector.graphic = TRUE) %>% 
  writeDoc("docs/evg_.docx")

Outputs

evg_.pptx and evg_.docx .

Ressources and informations

Get packages

install.packages(c( "ReporteRs", "rtable") )

rtable is complementing ReporteRs for tabular reporting.

Support

Tutorials

Live demo

Demos

  • Clinical reporting
    • for R users
    • shiny app for non R users
  • Financial reporting