【问题标题】:SelectInput choices are not getting populated dynamically based on csv file chosen in ShinySelectInput 选项不会根据在 Shiny 中选择的 csv 文件动态填充
【发布时间】:2020-12-05 11:44:18
【问题描述】:

在dashboardBody 的tabPanel-“tab2”title="plot" 中,我有一个selectInput 对象,它的选择基于来自服务器的dataTable 输出“contents2”。这样做时,我没有在 selectInput 对象的下拉菜单中填充任何选项,而且当我尝试根据 selectInput 的选择绘制直方图时,我收到错误消息:“找不到对象‘contents2’” 请有人在这里指导我。

library(shinyWidgets)
library(shiny)
library(shinydashboard) 

ui <-  dashboardPage(
       dashboardHeader(title="Test"),

       dashboardSidebar(
         sidebarMenu(id = 'sbar', verbatimTextOutput("text1"),
            menuItem("File Selection", tabName = 'page1', icon = icon('line-chart'),
                     fileInput("file1", "Select CSV File", accept = c("text/csv","text/comma- 
            separated-values,text/plain",".csv")),
                     menuSubItem(actionButton(inputId="next1", label="NEXT"), tabName="next", 
                         icon="") ),
            menuItem('File Edit', tabName = 'page2',icon = icon('line-chart')),
            menuItem('Section 3',tabName = 'page3',icon = icon('line-chart')) )
          ),

     dashboardBody(
          tabItems( 
                 tabItem(tabName = "next",fluidRow(
                                     tabBox(id = "tabset1", height = "650px", width=12, 
                       tabPanel("Input Data", value="tab1", " ",
                              # fluidRow(tags$head(tags$style(HTML(" label {float:left;} "))),
                            radioGroupButtons("disp", "",label=NULL, 
                                              choices = c('Display head data'="head",'Display entire 
                                              data'="all"), selected=NULL),
                            fluidRow(DT::dataTableOutput("contents1"),style = "height:500px; 
                                     overflow-y: scroll;overflow-x: scroll;",
                                     title = "Dashboard example") ),
                   tabPanel("Plot", value="tab2", " ",  
                            selectInput("select1","Select Variable for display",choices = 
                                       c(colnames(DT::dataTableOutput("mydata")))),
                            fluidRow(plotOutput("plot1"))),
                   tabPanel("tab3 title", value='tab3', " ",
                            valueBoxOutput('tab3_valuebox'))
            ) ) ),
                 tabItem(tabName="page2", fluidRow(
                       tabBox(id = "tabset2", height = "650px", width=12, title = "My Page2 info",
                   tabPanel("Input Data", value="tab1", " ",
                            fluidRow(DT::dataTableOutput("contents2"))),
                   tabPanel("Plot", value="tab2", " ",
                            fluidRow(plotOutput("plot2")) )
            ) ) ) ) ) )

    server <- function(input, output, session) {
      
        observeEvent(input$next1, {
        updateTabItems(session, "sbar", "next")

       req(input$next1)
        if (input$next1 == 0) { 
        return(NULL)
          }else if (input$next1 == 1 & is.null(input$file1)) {
          return(NULL)
         }else { 
       inFile <- input$file1
       myfile <- read_csv(inFile$datapath)
     output$contents1 <- renderDataTable({
       if(input$disp == "head") {
       return(head(myfile))
       }else {
       return(myfile) }})
  
   output$contents2 <- renderDataTable({
    myfile }) }
  })
   observe(input$select1)
   output$text1 <- renderText(print(input$sbar))
   output$plot1 <- renderPlot({hist(contents2$input$select1)})
   output$plot2 <- renderPlot({hist(rnorm(20))})

  output$tab3_valuebox <- renderValueBox({
  valueBox('2020',subtitle = "Need to use this in future",icon = icon("car"),
         color = "red") })
  }  
 shinyApp(ui, server)

【问题讨论】:

  • 您需要在renderUI 中的服务器端处理selectInput,然后在ui 中处理uiOutput

标签: shiny shinydashboard shiny-server shinyapps shiny-reactivity


【解决方案1】:

在服务器端处理 selectInput 并创建一个响应式数据框以供使用。下面的代码有效。

ui <-  dashboardPage(
  dashboardHeader(title="Test"),
  
  dashboardSidebar(
    sidebarMenu(id = 'sbar', verbatimTextOutput("text1"),
                menuItem("File Selection", tabName = 'page1', icon = icon('line-chart'),
                         fileInput("file1", "Select CSV File", accept = c("text/csv","text/comma- 
            separated-values,text/plain",".csv")),
                         menuSubItem(actionButton(inputId="next1", label="NEXT"), tabName="next", 
                                     icon="") ),
                menuItem('File Edit', tabName = 'page2',icon = icon('line-chart')),
                menuItem('Section 3',tabName = 'page3',icon = icon('line-chart')) )
  ),
  
  dashboardBody(
    tabItems( 
      tabItem(tabName = "next",fluidRow(
        tabBox(id = "tabset1", height = "650px", width=12, 
               tabPanel("Input Data", value="tab1", " ",
                        # fluidRow(tags$head(tags$style(HTML(" label {float:left;} "))),
                        radioGroupButtons("disp", "",label=NULL, 
                                          choices = c('Display head data'="head",'Display entire 
                                              data'="all"), selected=NULL),
                        fluidRow(DT::dataTableOutput("contents1"),style = "height:500px; 
                                     overflow-y: scroll;overflow-x: scroll;",
                                 title = "Dashboard example") ),
               tabPanel("Plot", value="tab2", " ",  uiOutput("selectvar"),
                        # selectInput("select1","Select Variable for display",choices = 
                        #               c(colnames(DT::dataTableOutput("mydata")))),
                        fluidRow(plotOutput("plot1"))),
               tabPanel("tab3 title", value='tab3', " ",
                        valueBoxOutput('tab3_valuebox'))
        ) ) ),
      tabItem(tabName="page2", fluidRow(
        tabBox(id = "tabset2", height = "650px", width=12, title = "My Page2 info",
               tabPanel("Input Data", value="tab1", " ",
                        fluidRow(DTOutput("contents2"))),
               tabPanel("Plot", value="tab2", " ",
                        fluidRow(plotOutput("plot2")) )
        ) ) ) ) ) )

server <- function(input, output, session) {
  
  observeEvent(input$next1, {
    updateTabItems(session, "sbar", "next")
    
    req(input$next1)
    if (input$next1 == 0) { 
      return(NULL)
    }else if (input$next1 == 1 & is.null(input$file1)) {
      return(NULL)
    }else { 
      inFile <- input$file1
      #myfile <- read_csv(inFile$datapath)
      myfile <- reactive(read_csv(inFile$datapath))
      output$contents1 <- renderDataTable({
        if(input$disp == "head") {
          return(head(myfile()))
        }else {
          return(myfile()) }})
      
      output$contents2 <- renderDT(myfile()) 
      
      output$selectvar <- renderUI({
        req(input$file1)
        selectInput("select1", "Select Variable for display",
                    choices = c(colnames(myfile())))
        
      })
      output$plot1 <- renderPlot({hist(myfile()[[input$select1]])})
    }
  })
  
  #observe(input$select1)
  output$text1 <- renderText(print(input$sbar))
  
  output$plot2 <- renderPlot({hist(rnorm(20))})
  
  output$tab3_valuebox <- renderValueBox({
    valueBox('2020',subtitle = "Need to use this in future",icon = icon("car"),
             color = "red") })
}  
shinyApp(ui, server)

【讨论】:

  • 我尝试执行您共享的上述代码,但出现错误:read_csv 中的错误:找不到函数“read_csv”。请帮忙。
  • 谢谢我修好了。感谢您的帮助。
猜你喜欢
  • 2019-01-19
  • 2019-04-06
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2018-02-27
  • 1970-01-01
相关资源
最近更新 更多