I'm trying to integrate selectInput
with a Null option so that I can filter by an athlete's first year in the NBA, but when I run the app, the graph just won't load. Here's a condensed version of my code:
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")
})
}
With the rest of my code, I get this: How can I fix this?
Solution: It's an open and shut case, Johnson. My selectInput id was "Draft Class", but I referred to it as DraftClass without a space. However, I found that even with other workarounds (as those suggested here and in other places), they would just not output anything where the filter(yearSeasonFirst == yearSeasonFirst)
So I tried an if else, where if input$DraftClass == the null option I wanted, run the output code without the filter for yearSeasonFirst
I can't fully test this without the NBA1920withLuka
data frame, but replacing NULL
with a string should work:
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")
})
}
(Also moved the reactive list outside renderPlot
, as noted by @SmokeyShakers.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.