I would like to
- create a report that displays
formattabletables side-by-side, - create an HTML, PDF, or image version of that report, and
- email it to users so they can either view or print the documents.
Ideally, I would be able to generate either HTML or PDF documents, but will be happy with just one of those provided that I can set page breaks, and that green checks and red X will properly print when people without R print the HTML or PDF version of the document.
Attempt 1. One option I tried is RMarkdown. In this case, I don't know how to put formattable tables side-by-side. Here is sample code:
---
title: "test"
author: "me"
date: "September 21, 2017"
output:
html_document: default
pdf_document: default
---
```{r,echo=F, warning=F}
library(formattable)
library(htmlwidgets)
## Create some data for the tables. I'll want 5 different tables, but for simplicity of this code, let's just repeat the same table 5 times.
d1 = mtcars[5:8, c(1:4,4)]
```
### Three tables I would like to be side-by-side
```{r,echo=F}
# create some formatting for the tables.
hp.formatter = formatter("span", style = x ~ style(color = ifelse(x>=110, "green", ifelse(x<=93, "red", 'white'))), x ~ icontext(ifelse(x>110, "ok", ifelse(x<=93, "remove", ''))))
# Create the tables.
t1 = as.htmlwidget(formattable(d1, list(hp.1=hp.formatter)), width='33%')
t1
t1
t1
```
### Two tables I would like to be side-by-side
```{r,echo=F}
t1
t1
```
Attempt 2. Alternatively, I could use shiny. In that case, I know how to put the formattable tables side-by-side. However, I do not know how to download the output as an HTML or PDF so that it can be emailed to users so that they can print it. Here is some sample code:
library(shiny)
library(formattable)
library(htmlwidgets)
# Define UI
ui <- fluidPage(
fluidRow(h3(paste0('Three formattable tables side-by-side'))),
fluidRow(column(3, div(tabPanel("Left" , formattableOutput("d1")))), column(1),
column(3, div(tabPanel("Center", formattableOutput("d2")))), column(1),
column(3, div(tabPanel("Right" , formattableOutput("d3")))), column(1)
),
fluidRow(h3(paste0('Two formattable tables side-by-side'))),
fluidRow(column(2),
column(3, div(tabPanel("Left" , formattableOutput("d4")))), column(2),
column(3, div(tabPanel("Right" , formattableOutput("d5")))), column(2)
)
)
server <- function(input, output) {
## Create some data for the table.
## I'll want 5 different tables, but for simplicity, let's just repeat the same data 5 times.
d1 = mtcars[5:8, c(1:4,4)]
## Define some formatting for the formattable tables
hp.formatter = formatter("span", style = x ~ style(color = ifelse(x>=110, "green", ifelse(x<=93, "red", 'white'))), x ~ icontext(ifelse(x>110, "ok", ifelse(x<=93, "remove", ''))))
## Create the table.
t1 = formattable(d1, list(hp.1=hp.formatter))
## Create the outputs. (Repeat same table 5 times)
output$d1=renderFormattable(t1)
output$d2=renderFormattable(t1)
output$d3=renderFormattable(t1)
output$d4=renderFormattable(t1)
output$d5=renderFormattable(t1)
}
# Run the application
shinyApp(ui = ui, server = server)
Thanks for your help!