如何访问另一个输入模块中的 R Shiny 输入(上传)文件?

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

下面我有一个 R Shiny 代码,用于将输入文件上传到应用程序。将要上传的所有 csv 文件都有一列 'carname'。我想要一个能够访问该列中的 select 选项的下拉菜单。如何访问 ui 中上传的文件?

错误:data$carname 中的错误:'closure' 类型的对象不是子集

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)

您无法从 UI 访问上传的文件。 但是您可以从服务器创建一个 UI 元素,然后使用函数 renderUIUIOutput.

将其发送到 UI
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)

这不是必需的,而且我会说在这种情况下不建议使用 renderUIupdateSelectInput() 会更快(Make R Shiny Dashboards Faster with updateInput, CSS, and JavaScript)并且需要更少的代码重写:

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)

我改变了什么:

  1. 在 UI 部分我将 "" 传递给 selectInput 中的 choice 参数,因为正如@gdevaux 提到的,您不能以这种方式使用来自 serverUI.
  2. 我已将 session 参数添加到 server 函数 - 这是 update* 函数所必需的。
  3. 我在 observer 中添加了 updateSelectInput() - 如果 data() 不是 NULL,它将更新 selectInput
  4. 正如@gdevaux 所做的那样,我已将您的 choices = data$carname 更改为 choices = data()$carname。这是因为 data 作为一个 reactive 你需要作为一个函数来使用。