Unsure why my ggplot2 graph is not rendering in Shiny.
I used updateSelectizeInput function as there are a few input choices.
I have given an example of this code working in a standalone script where the x-axis shows a single area of interest.
Any help or suggestions very much appreciated.
Code
...
library(dplyr)
library(ggplot2)
library(RPostgres)
library(shiny)
library(sf)
server <- function(input, output, session) {
conn <- dbConnect(drv = RPostgres::Postgres(),
dbname= "xxx",
user = "xxx",
password ="xxx",
host = "xxx")
a_mvmt_prop <- st_read(conn, query = "SELECT * FROM public.weekly_mvmt_proportions;")
data <- reactive({
req(input$sel_dca)
df <- a_mvmt_prop %>% filter(from_dca_id %in% input$sel_dca)
})
#Update sel_dca Dynamically
observe({
updateSelectizeInput(session, "sel_dca", choices = a_mvmt_prop$from_dca_id)
})
#Plot
output$plot <- renderPlot({ggplot(data()) +
aes(x = from_dca_id, y = to_dca_id_proportion, fill = to_dca_id_proportion) +
geom_col()
geom_text(aes(label = to_dca_id), position=position_stack(vjust=0.5), colour = "white")
})
}
ui <- basicPage(
h1("R Shiny Dynamically create Drop Down List"),
selectInput(inputId = "sel_dca",
label = "Choose DCA Number",
"Names",
selectize = TRUE),
plotOutput("plot")
)
shinyApp(ui = ui, server = server)
Shiny Script Terminal Output
Listening on http://127.0.0.1:6677
Warning in st_read.DBIObject(conn, query = "SELECT * FROM public.weekly_mvmt_proportions;") :
Could not find a simple features geometry column. Will return a data.frame
.
Warning: The select input "sel_dca" contains a large number of options; consider using server-side selectize for massively improved performance. See the Details section of the ?selectizeInput help topic.
Working Standalone Script
library(dplyr)
library(ggplot2)
library(RPostgres)
library(shiny)
library(sf)
conn <- dbConnect(drv = RPostgres::Postgres(),
dbname= "xxx",
user = "xxx",
password ="xxx",
host = "xxx")
a_test_filter_mvmt_prop <- filter(a_mvmt_prop, from_dca_id == 366)
a_plot_mvmt_prop_single_dca <- ggplot(a_test_filter_mvmt_prop,
aes(from_dca_id, to_dca_id_proportion, fill = to_dca_id_proportion)) +
geom_col() +
geom_text(aes(label = to_dca_id), position=position_stack(vjust=0.5), colour = "white")
show(a_plot_mvmt_prop_single_dca)
Plot Output for Pre-Filtered Data