Click on map marker to show ggplot line chart



I am new to R shiny. Trying to show a line chart upon clicking onto the marker on the map. However, the id does not seem to work. Please assist. Any help would be appreciated.

library(shiny)    # for shiny apps
library(leaflet)  # renderLeaflet function

server = function(input, output) {

Cookedfood_R <- readRDS("~/hawkermaster.rds")
line <- readRDS("~/line.rds")
#Reshape the data for ggplot
traff2 <- melt(line,id=c("TYPE","newname"), = "Year")

#Remove the X in the Year column and convert it to number
traff2$Year <- as.numeric(gsub(pattern="X",replacement = "",x = as.character(traff2$Year)))

# create a reactive value that will store the click position
data_of_click <- reactiveValues(clickedMarker=NULL)

#getColor <- function(Cookedfood_R) { 
#  sapply(Cookedfood_R$TYPE, function(TYPE) { 
#    if(TYPE == 1) {"blue"} 
#    else {"orange"} }) 
icons <- awesomeIcons( 
  icon = 'ion-close', 
  iconColor = 'black', 
  library = 'ion', 
  markerColor = getColor(Cookedfood_R) 

  output$map = renderLeaflet({
    leaflet() %>% addTiles() %>%
      addMarkers(data = Cookedfood_R,  
                 lat = ~ LATITUDE,  
                 lng = ~ LONGITUDE, 
                 icon = icons, 
                 popup = paste(Cookedfood_R$HAWKER, "<br>",
                               "Total no. of stalls:", Cookedfood_R$total, "<br>",
                               "Avg bid $:", trunc(Cookedfood_R$avgp,"<br>")))})

# store the click
  data_of_click$clickedMarker <- input$map_marker_click

# Make a line chart depending of the selected point

    facet_grid(facets = newname~., scales = "free_y")+
      geom_line(aes(Year, value, color = TYPE))+theme_bw()+
     geom_point(aes(shape=TYPE, size=1))

ui <- fluidPage(
  column(8,leafletOutput("map", height="700px")),
  column(4,br(),br(),br(),br(),plotOutput("plot", height="400px")),

shinyApp(ui = ui, server = server)


Hi @joyce1! Welcome!

It's not possible for a helper to run this code as-is, because it depends on data files that the helper won't have available. I'm afraid that makes it difficult for anybody to understand exactly what your problem is.

The code also isn't formatted correctly, which makes it not-very-fun to read (especially the case for shiny code, where there's lots of indentation!). It looks like maybe when you pasted the code in, you formatted it with the little double-quote button (which formats the selected text as a block quote)? What you wanted to use was the little </> button (which formats the selected text as code). Can you please edit your original post to try the paste again, this time with the correct formatting?

Even better, take a look at our Shiny Reprex and Debugging Guide, which talks about how to make a small reproducible example that shows the problem you're having. Supplying a reproducible example will make it much more likely that someone will be able to help!

This seems like a somewhat separate question. We encourage people to ask separate questions in separate topics. Once you get the basic app working, if you still aren't sure how to do this part, that would be a good time to start a new topic (linking to this one), with this follow-up question.