Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Created May 12, 2018 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aagarw30/ad3e0feb23ade99b85cffe3bf79cb3ae to your computer and use it in GitHub Desktop.
Save aagarw30/ad3e0feb23ade99b85cffe3bf79cb3ae to your computer and use it in GitHub Desktop.
fileinput - demo upload multiple files, perform operation and download the resultant dataset
library(shiny)
# use the below options code if you wish to increase the file input limit, in this example file input limit is increased from 5MB to 9MB
# options(shiny.maxRequestSize = 9*1024^2)
# options(shiny.trace=T)
shinyServer(function(input,output) {
## input$file is a data frame and contains the details around the name, size and temp location of the files uploaded
# this reactive output display the content of the input$file dataframe
output$filedf <- renderTable({
if(is.null(input$file)){return ()}
input$file # the file input data frame object that contains the file attributes
})
# Extract the file path for file
output$filedf2 <- renderTable({
if(is.null(input$file)){return ()}
input$file$datapath # the file input data frame object that contains the file attributes
})
## Below code to display the structure of the input file object
output$fileob <- renderPrint({
if(is.null(input$file)){return ()}
str(input$file)
})
# Merging the data files using rbind
# assumption that all files have same columns
datamerge <- reactive({
if(is.null(input$file))
return()
else
{
nfiles = nrow(input$file)
csv = list()
for (i in 1 : nfiles)
{
csv[[i]] = read.csv(input$file[[i, 'datapath']])
}
do.call(rbind, csv) # rbind the datasets
}
})
## Display the merged data
output$newdata <- renderTable({
datamerge()
})
## DownloadHandler to download the merged dataset
output$download <- downloadHandler(
# This function returns a string which tells the client
# browser what name to use when saving the file.
filename = function() {
fileext = switch(input$sep,
"," = "csv",
";" = "csv",
"\t" = "txt",
" " = "doc")
paste("merged", fileext, sep = ".") # example : iris.csv, iris.doc, iris.txt
},
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
# Write to a file specified by the 'file' argument
write.table(datamerge(), file, sep = input$sep,
row.names = FALSE)
}
)
## Side bar select input widget coming through renderUI()
# Following code displays the select input widget with the list of file loaded by the user
output$selectfile <- renderUI({
if(is.null(input$file)) {return()}
list(hr(),
helpText("Select the files for which you need to see data and summary stats"),
selectInput("Select", "Select", choices=input$file$name)
)
})
## Summary Stats code ##
# this reactive output contains the summary of the dataset and display the summary in table format
output$summ <- renderPrint({
if(is.null(input$file)){return()}
summary(read.table(file=input$file$datapath[input$file$name==input$Select],
sep=input$sep,
header = input$header,
stringsAsFactors = input$stringAsFactors))})
## Dataset code ##
# This reactive output contains the dataset and display the dataset in table format
output$table <- renderTable({
if(is.null(input$file)){return()}
read.table(file=input$file$datapath[input$file$name==input$Select], sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
## MainPanel tabset renderUI code ##
# the following renderUI is used to dynamically generate the tabsets when the file is loaded.
# Until the file is loaded, app will not show the tabset.
output$tb <- renderUI({
if(is.null(input$file)) {return()}
else
tabsetPanel(
tabPanel("Input File Object DF ", tableOutput("filedf"), tableOutput("filedf2")),
tabPanel("Input File Object Structure", verbatimTextOutput("fileob")),
tabPanel("Dataset", tableOutput("table")),
tabPanel("Summary Stats", verbatimTextOutput("summ")),
tabPanel("Merged Dataset", downloadButton("download", "Download" ), tableOutput("newdata"))
)
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("Demo - File Input - Upload, access and perform operation on multiple files"),
sidebarLayout(
sidebarPanel(
fileInput("file","Upload the file", multiple = T), # fileinput() function is used to get the file upload contorl option
helpText("Default max. file size is 5MB"),
helpText("Select the read.table parameters below"),
checkboxInput(inputId = 'header', label = 'Header', value = TRUE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
radioButtons(inputId = 'sep', label = 'Separator', choices = c("Comma" = "," , "Semicolon" = ";", "Tab"= "\t", "Space"= " "), selected = ','),
uiOutput("selectfile")
),
mainPanel(
uiOutput("tb")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment