Skip to content

Instantly share code, notes, and snippets.

@dcomtois
Created February 27, 2021 02:06
Show Gist options
  • Save dcomtois/3ceb928b78b9840d1ba2b15660a9ae52 to your computer and use it in GitHub Desktop.
Save dcomtois/3ceb928b78b9840d1ba2b15660a9ae52 to your computer and use it in GitHub Desktop.
A possible implementation of dfSummary as a shiny App
# Based on Thomas' answer on this Stack Overflow question:
# https://stackoverflow.com/questions/65792953/upload-a-file-and-use-summarytools-in-shiny/
# I fixed his answer so that it works but have no idea if it's optimal, having limited
# experience with shiny
library(shiny)
library(summarytools)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
tabsetPanel(
tabPanel( "tab1", tableOutput("contents")),
tabPanel("dfSummary Output", htmlOutput("profileSummary")))
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = input$header)
})
output$profileSummary <- renderUI({
inFile <- input$file1
if (is.null(inFile)) {
return(NULL)
} else {
tmp <- read.csv(inFile$datapath, header = input$header) # -----------------------------+
print(dfSummary(tmp), # This is the modified part |
method = "render", # |
Data.frame = inFile[[1]]) # -----------------------------+
}
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment