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