How to store each click on a different object? leaflet shiny

library(shiny)
library(leaflet)


output$map <- renderLeaflet({
    m<-leaflet() %>%
      addProviderTiles(providers$OpenStreetMap,
                       options = providerTileOptions(noWrap = TRUE))%>%
      setView(lng =-73.958091, lat =40.643767, zoom= 12)

  observe({  click = input$map_click
  if(is.null(click))
    return()
  else
    click= data.frame(click[1:2])
# if the object v does not exist store the click information in the object "v"
  if(exists("v")==F){
   leafletProxy("map") %>%
     addMarkers(data = click) ->>v
   v<<-data.frame(v$x$limits)}
#if object v exists store click information on object "w"
if(exists("v")==T) {leafletProxy("map") %>%
      addMarkers(data = click) ->>w}
   w<<-data.frame(w$x$limits)

This code creates a marker each time the map is clicked, but how do I store the information for each click in a different variable? I need to store the latitude and longitude of 1 click on one object and another click on another object

> w
        lat       lng
1 40.643767 -73.958091
2 40.643767 -73.958091
> v
data frame with 0 columns and 0 rows

the w object stores the information I want but the v doesn't.. Why does it happen?

You shouldn't use <<- with shiny.
Create a reactive variable to store the information in, maybe a list.

clicklist <- reactiveVal(list()) # empty list
observeEvent(input$map_click, {
  click <- input$map_click
  temp <- clicklist() # get the list of past clicks 
  temp[[length(temp)+1]] <- click # add this click to the list
  clicklist(temp) # save the list

Thanks for the answer, the information was stored in the clicklist object, but how do I access each one individually? when i try clicklist [1] it gives the error "object of type 'closure' is not subsettable". I tried to use clicklist () [1] and clicklist () [2] but only the last click is returned to me.

>clicklist
[1]reactiveVal: [1] "40.742610, -73.434242" "40.730612, -73.935268" 
> class(clicklist)
[1] "reactiveVal" "reactive"   

Address1<-clicklist()[1]       # error, selected only the last click
  Address2<-clicklist()[2]     # error, selected only the last click
print(Address1)
[[1]]
[[1]]$lat
[1] 40.730612

[[1]]$lng
[1] -73.935268

>print(Address2)
[[1]]
[[1]]$lat
[1] 40.730612

[[1]]$lng
[1] -73.935268

I want to implement two map click events in my map, map_marker_click, and map_click. When I click on a marker on the map(using map_marker_click), I want the event to filter other related charts based on the id of the clicked marker. Using the map_click event by clicking the base map, I want the event to reset the map and graphs to default (with all the markers and charts). With my current code, I can only be able to click on a marker and only that marker is displayed on the map with the corresponding chart.

#reactive event for map click
  map_click<-reactive({
    clk=input$maps2_marker_click
    if(is.null(clk$id)){
       return(final_data)   
    }
      else{
        rev_filter<-final_data %>% filter(Kiosk_Id_number==clk$id)
        return(rev_filter)
            }
  }
  )


   #color function for waterpoint types
  pal=colorFactor(c("sienna2","green3","cornflowerblue"),domain = final_data$Water_point_type)

  #interactive map with click event
  output$maps2<-renderLeaflet(
    leaflet() %>%
      addCircleMarkers(
        data = map_click(),
        fillColor = ~pal(Water_point_type),
        fillOpacity = 1.0,
        radius = 8,
        stroke = FALSE,
        lat = ~latitude,
        lng = ~longitude,
        layerId = ~Kiosk_Id_number
  ) #end renderleaflet


  #facet plots 
  #plots are filtered based on the map click
  output$facet_plot<-renderPlotly(
    ggplotly(
      ggplot(map_click(),aes(x=Day,y=Daily_revenue,group=Kiosk_Id_number))+
        geom_col(aes(fill=Water_point_type))+
        scale_fill_manual(values = c("sienna2","cornflowerblue")
        )+
        geom_line(aes(y=Daily_Average),linetype="dashed", size=0.4,color="gray0")+
        facet_wrap(~Kiosk_Id_number,scales = "free_y",ncol = 2)+
        labs(
          y="Daily Average Revenue (GHC)")+
        scale_x_discrete(breaks=c("05-Jun-19","31-Jul-19",
                                  "27-Sep-19","28-Nov-19","31-Dec-19",
                                  "31-Jan-20","28-Feb-20","26-Mar-20"))+
        scale_y_continuous(breaks = my_breaks)+
        theme(axis.text.x = element_text(angle = 90,size = 7),
              axis.title = element_text(size=9),
              axis.text = element_text(face = "bold"),
              axis.title.x = element_blank(),
              axis.text.y = element_text(size=6,face="bold"),
              strip.text = element_text(face="bold"),
              strip.background = element_blank(),
              strip.text.x = element_text(face="bold.italic",colour ="gray0",size=8,margin=margin(0,0,0,0,"cm")),
              legend.title = element_blank(),
              #legend.position = "bottom",
              panel.grid = element_blank(),
              panel.spacing = unit(2, "lines")       
        )  
    ) 
  )#end renderplot

Yes you access the value of a reactiveVal with brackets. clicklist is the reactive object itself, and clicklist() returns its value, the list you are storing i it. You read the documentation right?

To access elements of a list in R you use double brackets [[]]

So

clicklist()[[1]]
clicklist()[[2]]
...

Use an observeEvent() for each of the events you want to observe (map_click and map_marker_click).

I tried this and returned the error: ":Error in [[: subscript out of bounds", It seems that an object of the reactive class does not behave like a list

I would appreciate a reproducible example of how to implement the map_click and map_marker_click using observe event for both the map and the graphs

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