Reputation: 141
Using the midwest
dataframe from ggplot2
:
# (in app_ui.r)
poverty_sidebar <- sidebarPanel(
radioButtons(
inputId = "state",
label = "State",
choices = list("IL" = 1, "IN" = 2, "MI" = 3, "OH" = 4, "WI" = 5),
selected = 1
))
poverty_plot <- mainPanel(
plotOutput(
outputId = "poverty_plot"
)
)
# (in app_server.r)
server <- function(input, output) {
output$poverty_plot <- renderPlot({
filtered <- filter(midwest, state == input$state)
plot <- ggplot(data = filtered) +
geom_col(x = county, y = poppovertyknown)
return(plot)
})
Doesn't seem to work, gives me a "Object county
not found" error. is doing filter(midwest, state == input$state)
the wrong approach? Or does the error lie with my radio buttons?
Upvotes: 0
Views: 375
Reputation: 1424
So the code provided is close, you are making two mistakes
choices
does not need to map to numericx = county, y = poppovertyknown
should be in the aes
of geom_col
so aes(x = county, y = poppovertyknown)
Hence the final working code would be (Note I have added assignment to ui
to make it work in single file with call to shinyApp(ui, server)
),
library(shiny)
library(dplyr)
library(ggplot2)
poverty_sidebar <- sidebarPanel(
radioButtons(
inputId = "state",
label = "State",
choices = list("IL", "IN", "MI", "OH", "WI"), # remove mapping to integers
selected = "IL"
))
poverty_plot <- mainPanel(
plotOutput(
outputId = "poverty_plot"
)
)
ui <-
fluidPage(
poverty_sidebar,
poverty_plot
)
# (in app_server.r)
server <- function(input, output) {
output$poverty_plot <- renderPlot({
filtered <- filter(midwest, state == input$state)
print(filtered)
print(input$state)
filtered0 <<- filtered
plot <-
filtered %>%
ggplot() +
geom_col(aes(x = county, y = poppovertyknown)) # used aes()
return(plot)
})
}
shinyApp(ui, server)
Upvotes: 1