RShiny: Update ggplot2 geom_col() graph using updateSelectizeInput()

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

My advice is to have a script that can perform an example of your requirements outside of a shiny context, then adapt it to shiny having overcome the basic challenges.
There seems to be somewhat confused intent around the nature of the data source and how it is used.
Are you doing map work, with geometry ? you seem to want to read such data (and are getting told that this fails) , but then theres no example of attempting to leverage any mapping things in the rest of the app. If you arent looking to use map data then you should remove all the map related code.

I have edited the post to show the standalone output of the ggplot2() function.
Not sure what you mean by mapping reference.
I am attempting to create a column graph in a shiny app dynamically.
Bar charts — geom_bar • ggplot2 (tidyverse.org)

By map, i refer to cartography.
Your use of library(sf) and the use of st_read() to access data point to cartography.
In the example where you make the bar chart , you dont show how the data relates to the connection.

I assume you succesfully made a_mvmt_prop some other way (that you didnt share?)

Please ignore, as you can see the data type out of that function is a standard data frame, it can read both Postgresql and PostGIS tables.
All code is displayed, the title says 'RShiny: Update ggplot geom_col() graph...'
No mapping is mentioned or used, I am simply attempting to render a ggplot2 geom_col() graph with a updateSelectizeInput function in Shiny - unless you are implying this might be tripping up Shiny in some way.
Will test without sf library.

Removing sf library and just using RPostgres's dbGetQuery();
a_mvmt_prop <- dbGetQuery(conn, "SELECT * FROM public.weekly_mvmt_proportions;")
did not fix the missing graph / shiny not rendering ggplot2 issue

For some reason I am able to get it to work with :-

library(dplyr)
library(ggplot2)
library(RPostgres)
library(shiny)

server <- function(input, output, session) {

  conn <- dbConnect(drv = RPostgres::Postgres(),
                    dbname="xxx",
                    user = "xxx",
                    password="xxx",
                    host = "xxx")

  a_mvmt_prop <- dbGetQuery(conn, "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({
    numericInput("sel_dca",
                 h3("Type in From DCA ID"),
                 value = 366)
  })

  #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("Select DCA ID to show all Movement Proportions"),
  numericInput("sel_dca", label = "Enter DCA ID for movement proportions", value =366),
  plotOutput("plot")
)

shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.