I'm working on a Shiny app and am having trouble understanding an error. I have created a much simpler reproducible example below that captures the issue. Ultimately, I just want to pass the input from the city
selection box to dplyr::filter()
. I want to then take the filtered data set and plot it using ggplot2
. Lastly, I want to use ggplotly()
(from the plotly
package) to convert the static plot to an interactive plot. The code below shows how I have constructed the app, and when ran two different errors show up.
The first error is Warning: Error in filter_impl: Result must have length 9, not 0
. The length 9 part is dependent on the data, so for my larger app the error is the same but with length 9721.
I also get this error, Warning in origRenderFunc() : Ignoring explicitly provided widget ID "154376613f66f"; Shiny doesn't use them
.
Probably the most puzzling part is that my app works after throwing these two errors, and all the functionality appears to be working correctly. If you copy and paste the code below and run it, the app will launch and throw an error, before rendering the output of the plot.
I'm trying to build an app that displays a unique line chart for certain U.S. cities. I would like the UI to have an option to select a state, and then have the city options be filtered to those cities within the state. The code below produces the desired interface but with the two errors. Anyone have any insight? Since the app works correctly and has the functionality I am after, it is tempting to just suppress the error message, but I would much rather understand what is breaking and fix it. Many thanks in advance!
library(tidyverse)
library(shiny)
library(plotly)
df <- tibble(
city = c(rep("city1", 3), rep("city2", 3), rep("city3", 3)),
state = c(rep("state1", 6), rep("state2", 3)),
year = c(rep(c(2016, 2017, 2018), 3)),
dummy_value = c(1, 2, 3, 6, 7, 8, 15, 16, 17)
)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "state",
label = "",
choices = c("state1", "state2")),
uiOutput("selected_city")
),
mainPanel(
plotlyOutput("example_plot")
)
)
),
server = function(input, output, session) {
output$selected_city <- renderUI({
selectInput(inputId = "city",
label = "",
choices = dplyr::pull(
unique(df[df$state == input$state, "city"]), city)
)
})
output$example_plot <- renderPlotly({
plot_to_show <- plotly::ggplotly(
df %>%
dplyr::filter(city == input$city) %>%
ggplot() +
geom_point(aes(x = year, y = dummy_value))
)
})
}
)