簡體   English   中英

如何在Shiny中獲取null值以使用selectInput

[英]How to get a null value to work with selectInput in Shiny

我正在嘗試將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")
  })
}

這是我得到的: 在此處輸入圖片說明

使用其余的代碼,我得到以下信息: 在此處輸入圖片說明 我怎樣才能解決這個問題?

解決方案:這是一個打開和關閉的盒子,約翰遜。 我的selectInput id是“ Draft Class”,但我將其稱為DraftClass,不帶空格。 但是,我發現即使采用其他解決方法(如此處和其他地方所建議的),它們也不會在filter(yearSeasonFirst == yearSeasonFirst)地方輸出任何內容,因此我嘗試了if if,如果輸入$ DraftClass ==我想要的null選項,運行不帶yearSeasonFirst過濾器的輸出代碼

沒有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")
  })
}

(也將反應式列表移到renderPlot外部,如@SmokeyShakers所述。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM