Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Created July 12, 2016 04:14
Show Gist options
  • Save aagarw30/2012feb176eeb9bb9a7e082c45e9e937 to your computer and use it in GitHub Desktop.
Save aagarw30/2012feb176eeb9bb9a7e082c45e9e937 to your computer and use it in GitHub Desktop.
fileInput R Shiny
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)
shinyServer(function(input,output){
# This reactive function will take the inputs from UI.R and use them for read.table() to read the data from the file. It returns the dataset in the form of a dataframe.
# file$datapath -> gives the path of the file
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
# this reactive output contains the summary of the dataset and display the summary in table format
output$filedf <- renderTable({
if(is.null(data())){return ()}
input$file
})
# this reactive output contains the summary of the dataset and display the summary in table format
output$sum <- renderTable({
if(is.null(data())){return ()}
summary(data())
})
# This reactive output contains the dataset and display the dataset in table format
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
# 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(data()))
h5("Powered by", tags$img(src='RStudio-Ball.png', heigth=200, width=200))
else
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("File Input"),
sidebarLayout(
sidebarPanel(
fileInput("file","Upload the file"), # fileinput() function is used to get the file upload contorl option
helpText("Default max. file size is 5MB"),
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
br(),
radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(
uiOutput("tb")
# use below code if you want the tabset programming in the main panel. If so, then tabset will appear when the app loads for the first time.
# tabsetPanel(tabPanel("Summary", verbatimTextOutput("sum")),
# tabPanel("Data", tableOutput("table")))
)
)
))
@Psych-Machine
Copy link

Aagarw30--I am attempting to use your code so that I can have a user upload a file.csv and the app will then look inside that .csv, do math on the columns, and then generate a table to download and potentially plot to graph.

A simple example of this would be reading in a file of 10 patients with their height (in inches) and weight (in lbs). So the goal would be to read in the .csv and calculate their BMI (703*weight/(height^2)). I would also like them to be able to view a plot and then download the plot and BMI data.

I think I can figure out the plot and download from your other examples/youtube videos, but I can't seem to access what is inside the .csv. Could you create an example where you access the code--functions like length(), how to call specific columns, and do math. This is all very easy within the local R environment, but I am thrown off by Shiny. Thank you for the help!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment