[英]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.