【问题标题】:How to get a null value to work with selectInput in Shiny如何获取空值以在 Shiny 中使用 selectInput
【发布时间】:2020-03-21 08:03:22
【问题描述】:

我正在尝试将selectInput 与 Null 选项集成,以便我可以按运动员在 NBA 的第一年进行过滤,但是当我运行该应用程序时,图表将无法加载。 这是我的代码的精简版:

ui <- fluidPage(
titlePanel(title = h4("NBA StatsR", align = "center")),
tabsetPanel(type = "tabs",
  tabPanel("NBA 2019-2020",
   sidebarLayout(
     sidebarPanel(
       selectInput(inputId = "Draft Class",
                   label = "DraftClass",
                   choices = c("NULL","2007"),
                   selected = NULL)
),
mainPanel(
 plotOutput(outputId = "plot4", width = "104%",
            height = "500px")
     )))))

server <- function(input, output, session) {
  output$plot4 <- renderPlot({
    listVal <- reactive({
      if (is.null(input$DraftClass)){
        "yearSeasonFirst"}
      else {
        input$DraftClass}
    })
    NBA1920withLuka %>%
      filter(minutesPerGame >= 15) %>%
      filter(ratioPER >= 10) %>%
      filter(countGames >= 9) %>%
      filter(yearSeasonFirst == listVal()) %>%
      top_n(10, get(input$select4)) %>%
      ggplot(aes(x = reorder(namePlayer,
                             ptsPerGame),
                 y = ptsPerGame)) +
      geom_bar(stat = "identity")
  })
}

这是我得到的:

使用我的其余代码,我得到了: 我该如何解决这个问题?

解决方案:Johnson,这是一个打开和关闭的案例。我的 selectInput id 是“Draft Class”,但我将其称为 DraftClass,没有空格。但是,我发现即使使用其他解决方法(如此处和其他地方所建议的那样),它们也不会输出 filter(yearSeasonFirst == yearSeasonFirst) 的任何内容 所以我尝试了一个 if else,如果 input$DraftClass == 我想要的 null 选项,运行输出代码而不使用 yearSeasonFirst 的过滤器

【问题讨论】:

  • 你不应该把你的反应列表放在你的渲染图中。无论如何,问题是 NULL 不会触发 listVal 的反应。尝试将 ignoreNULL 设置为 FALSE 的 `eventReactive。

标签: r shiny null


【解决方案1】:

如果没有 NBA1920withLuka 数据框,我无法完全测试这个,但是用字符串替换 NULL 应该可以:

ui <- fluidPage(
  titlePanel(title = h4("NBA StatsR", align = "center")),
  tabsetPanel(type = "tabs",
              tabPanel("NBA 2019-2020",
                       sidebarLayout(
                         sidebarPanel(
                           selectInput(inputId = "Draft Class",
                                       label = "DraftClass",
                                       choices = c("[first season]", "2007"),
                                       selected = "[first season]")
                         ),
                         mainPanel(
                           plotOutput(outputId = "plot4", width = "104%",
                                      height = "500px")
                         )))))

server <- function(input, output, session) {
  listVal <- reactive({
    if (input$DraftClass == "[first season]"){
      "yearSeasonFirst"}
    else {
      input$DraftClass}
  })
  output$plot4 <- renderPlot({
    NBA1920withLuka %>%
      filter(minutesPerGame >= 15) %>%
      filter(ratioPER >= 10) %>%
      filter(countGames >= 9) %>%
      filter(yearSeasonFirst == listVal()) %>%
      top_n(10, get(input$select4)) %>%
      ggplot(aes(x = reorder(namePlayer,
                             ptsPerGame),
                 y = ptsPerGame)) +
      geom_bar(stat = "identity")
  })
}

(如@SmokeyShakers 所述,还将反应列表移到renderPlot 之外。)

【讨论】:

    猜你喜欢
    • 2021-03-24
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2021-10-05
    • 1970-01-01
    相关资源
    最近更新 更多