I am trying to add a file upload feature to my Shiny app. Ideally, I want the user to be able to upload a csv file and then select the variable they want to use from a dropdown menu. When the app starts, I initially have a dataset preloaded and the dropdown displays the correct variable names for this dataset. However, when I try to upload a different csv file, I cannot seem to get the dropdown to update to display the variables contained in the uploaded csv. Here's my code:
server.R
library(shiny)
library(ggplot2)
inFile <- mtcars
shinyServer(function(input, output) {
tableData <- reactive({
inFile <<- input$file1
if (!is.null(inFile)){
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
}
else {
inFile <<- mtcars
}
})
#for x values
output$opt.x <- renderUI({
selectInput("xcolumn", "X column to Plot", names(inFile))
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("App"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept = c(
'.csv',
'.tsv')
),
uiOutput("opt.x")
),
mainPanel()
))
I've also tried placing the output$opt.x assignment inside the reactive function that reads in the file, but that didn't seem to help.