I am working on a shiny app which takes user datas in input and complete first classical descriptive statistics and then a full statistical analysis. Indeed, the descriptive statistic part is done with table1 package table1 documentation used. At the end of the app I want to allow the user to download a full report containing all the plot and tables (especially descriptive statistic tables).
I am able to write some Rmarkdown scripts to include ggplots and DT datatables in the report. The problem is about the table1 object.
I render them using renderUI in the server part and uiOutput in the ui part.
Here is the code used :
Shiny script
library(shiny)
library(sas7bdat)
library(dplyr)
library(ggplot2)
library(dplyr)
library(tidyr)
library(factoextra)
library(FactoMineR)
library(corrplot)
library(cluster)
library(NbClust)
library(clValid)
library(dendextend)
library(summarytools)
library(RColorBrewer)
library(table1)
options(shiny.maxRequestSize=30*1024^2)
ui <-fluidPage(
navlistPanel(
#Welcome Page with logo and explanations
tabPanel("Welcome",textOutput("welcome_message"),textOutput("welcome_message_2"),img(src="logo_danone.jpg", height = 350, width = 350)),
#Selector for file upload
tabPanel("Input Datas",fileInput('datafile_LB', 'Choose LB Database file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
fileInput('datafile_DS', 'Choose DS Database file',accept=c('text/csv', 'text/comma-separated-values,text/plain')),
fileInput('datafile_VS', 'Choose VS Database file',accept=c('text/csv', 'text/comma-separated-values,text/plain')),
fileInput('datafile_SV', 'Choose SV Database file',accept=c('text/csv', 'text/comma-separated-values,text/plain')),
fileInput('datafile_DM', 'Choose DM Database file',accept=c('text/csv', 'text/comma-separated-values,text/plain'))),
# Display of the input data without any modification
tabPanel("Raw Datas",
DT::dataTableOutput("filetable_LB"), radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
inline = TRUE), downloadButton('downloadReport')
),
tabPanel("test plot", plotOutput("graph")),
tabPanel("Disposition of subjects", uiOutput("disposition"))
))
##### Server part #####
server <- function(input, output, session ) {
# Seting directory and import dependent functions
##### Input Loading #####
filedata_LB <- reactive({
infile <- input$datafile_LB
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
read.sas7bdat(infile$datapath)
})
filedata_DS <- reactive({
infile <- input$datafile_DS
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
read.sas7bdat(infile$datapath)
})
tableau <- reactive({table1(~ DSDECOD, data= filedata_DS(), topclass="Rtable1-zebra")})
#######################################################################################################################
################################# #################################
################################# Primary Endpoint Output #################################
################################# #################################
#######################################################################################################################
#This previews the CSV data file
output$filetable_LB <- DT::renderDataTable({DT::datatable(filedata_LB())})
output$graph <- renderPlot({ggplot(data = filedata_LB(), aes(y = LBSTRESN)) + geom_boxplot()})
output$disposition <- renderUI(print(table1(~ DSDECOD, data= filedata_DS(), topclass="Rtable1-zebra")))
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- "C:/Users/graditse/Desktop/test_markdown/report_file.Rmd"
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
}
)
}
shinyApp(ui, server)
Rmd script
---
title: "test"
author: "seb"
date: "26 juin 2019"
output: html_document
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r, fig.align='center', fig.pos='htb!', echo=FALSE, cache=FALSE, warning = FALSE, message = FALSE, tidy=TRUE}
library(DT)
library(webshot)
datatable(filedata_LB(),rownames=FALSE, options = list(dom='t',ordering=F))
```
```{r, echo=FALSE}
# library(ggplot2)
# p <- ggplot(data = filedata_LB(), aes(y = LBSTRESN)) + geom_boxplot()
# p
output$graph
```
```{r, echo=False}
library(webshot)
library(table1)
library(shiny)
reactive(table1(~ DSDECOD, data= filedata_DS(), topclass="Rtable1-zebra", output = "markdown"))
```
When I try to download the report, this can't be done I got :
Warning: Error in $.shinyoutput: Reading objects from shinyoutput object not allowed. [No stack trace available]
I don't really know how to handle this problem. This is strange because the DT table is also reactive and can be displayed into the report.
Is there a solution to display readable descriptive statistic tables into the Shiny app and in the report ?
I used the table1 package beacause it offers th possibility to process continuous and discrete variables, on different groups.
Thanks in advance !