2

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.

kawaiinekochan
  • 212
  • 3
  • 12

1 Answers1

4

Hi don't pass variable to the global environnement, use shiny reactive feature instead, your server should be :

library(shiny)
library(ggplot2)

# Test data
# write.csv(x = iris, file = "iris.csv", row.names = FALSE)
# write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)

shinyServer(function(input, output) {

  tableData <- reactive({
    inFile <- input$file1

    if (!is.null(inFile)){
      read.csv(inFile$datapath)
    } else {
      mtcars
    }
  })

  #for x values
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot", names(tableData()))
  })

 output$your_data <- renderTable({
   head(tableData())
 })
})

And replace your main panel with :

mainPanel(
  tableOutput("your_data")
)

I remove the params in read.csv since those input are not defined in the UI.

EDIT

Try this app, it's not a shiny solution but a jquery/js one (the difference is in the ui) (see this answer) :

library("shiny")
ui <- fluidPage(
  titlePanel("App"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept = c('.csv','.tsv')
      ),
      tags$script(
        '$( "#file1" ).on( "click", function() {
          this.value = null; 
        });'
      ),
      uiOutput("opt.x")
    ),
    mainPanel(
      tableOutput("your_data")
    )
    )
  )
server <- function(input, output) {

  tableData <- reactive({
    inFile <- input$file1
    if (!is.null(inFile)){
      read.csv(inFile$datapath)
    } else {
      mtcars
    }
  })
  #for x values
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot", names(tableData()))
  })

  output$your_data <- renderTable({
    head(tableData())
  })
}
shinyApp(ui = ui, server = server)

For testing I used :

write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)
write.csv(x = data.frame(V11 = 11:20, V12 = rnorm(10), ABC = letters[1:10]), file = "test.csv", row.names = FALSE)
Community
  • 1
  • 1
Victorp
  • 13,636
  • 2
  • 51
  • 55
  • I offered the bounty, however, I am still having an issue. How do you re-upload the same file ? I don't see shiny allowing you to upload the same file more than once (even though the file is modified locally) – Sriram Murali Dec 23 '15 at 17:54
  • I get "maximum upload size exceeded" for moderately large files. What is limitation? And can that be modified? – Antex Dec 23 '15 at 22:09
  • With `options(shiny.maxRequestSize=30*1024^2) ` (for 30MB) (See https://groups.google.com/forum/#!msg/shiny-discuss/rU3vwGMZexQ/zeKhiYXrtEQJ) – Victorp Dec 23 '15 at 22:18
  • Great answer Victor, can i aslo modify a different UI item with the tags$script. similar to "this.value = null", can i use "itemID.value = null" – Sriram Murali Dec 23 '15 at 22:22
  • Thank you! What is itemID ? You probably should ask a new question. – Victorp Dec 24 '15 at 10:10