简体   繁体   中英

How to access R Shiny Input(uploaded) file in another input module?

Below I have an R Shiny code to upload an input file to the application. All csv files that'll be uploaded will have a column 'carname'. I want to have a dropdown that's able to access and select choices in that column. How do I access the uploaded file in the ui?

Error: Error in data$carname: object of type 'closure' is not subsettable

ui <- fluidPage(
  headerPanel("Webpapp"),
  sidebarPanel(
    fileInput(inputId = "filedata",
              label = "Upload the Raw Data File",
              accept = c("text/csv", "text/comma-separated-values,text/plain",
                         ".csv")),      
    selectInput("cars", "Select car:", choices = data$carname, selected = "Nissan Sentra"),
   ),
  mainPanel(
    plotOutput('plot'),
    )
)

# Define server code ####
server <- function(input, output) {
  
  data <- reactive({
    req(input$filedata)
    read.csv(input$filedata$datapath, skip = 15, header = F)
  })

  output$plot <- renderPlot({
     if (is.null(data())) {
      return(NULL)
    }
            
    plot_probe(input$cars) #Plotting function in global
  })
}

shinyApp(ui = ui, server = server)

This is not necessary and also I would say not recommend in this case to use renderUI . updateSelectInput() will be faster (Make R Shiny Dashboards Faster with updateInput, CSS, and JavaScript ) and require less rewriting of code:

library(shiny)

ui <- fluidPage(
  headerPanel("Webpapp"),
  sidebarPanel(
    fileInput(inputId = "filedata",
              label = "Upload the Raw Data File",
              accept = c("text/csv", "text/comma-separated-values,text/plain",
                         ".csv")),      
    selectInput("cars", "Select car:", choices = "", selected = "Nissan Sentra"),
  ),
  mainPanel(
    plotOutput('plot'),
  )
)

# Define server code ####
server <- function(session, input, output) {
  
  data <- reactive({
    req(input$filedata)
    read.csv(input$filedata$datapath, skip = 15, header = F)
  })
  
  observe({
    req(data())
    updateSelectInput(session, "cars", choices = data()$carname)
  })
  
  output$plot <- renderPlot({
    if (is.null(data())) {
      return(NULL)
    }
    
    plot_probe(input$cars) #Plotting function in global
  })
}

shinyApp(ui = ui, server = server)

What I have changed:

  1. In UI part I passed "" to the choice argument in selectInput , because as @gdevaux mentioned, you cannot use in this way data from server in UI .
  2. I have added session parameter to server function - it is necessary for update* functions.
  3. I have added updateSelectInput() inside observer - it will update selectInput if data() won't be NULL .
  4. As @gdevaux did, I have changed your choices = data$carname into choices = data()$carname . It is because data as a reactive you need to use as a function.

You cannot access the uploaded file from the UI. But you can create an UI element from the server, and send it to the UI with the functions renderUI and UIOutput .

ui <- fluidPage(
  headerPanel("Webpapp"),
  sidebarPanel(
    fileInput(inputId = "filedata",
              label = "Upload the Raw Data File",
              accept = c("text/csv", "text/comma-separated-values,text/plain",
                         ".csv")),     
    uiOutput("ui_select")
  ),
  mainPanel(
    plotOutput('plot'),
  )
)

# Define server code ####
server <- function(input, output) {
  
  data <- reactive({
    req(input$filedata)
    read.csv(input$filedata$datapath, skip = 15, header = F)
  })
  
  # UI element for select input
  output$ui_select <- renderUI({
    req(data())
    selectInput("cars", "Select car:", choices = data()$carname, selected = "Nissan Sentra")
  })
  
  output$plot <- renderPlot({
    if (is.null(data())) {
      return(NULL)
    }
    plot_probe(input$cars) #Plotting function in global
  })
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM