title: How to connect leaflet map clicks (events) with plot creation in a shiny app
body: <p>Hello I am creating an environmental shiny app in which I want to use a leaflet map to create some simple plots based on openair package(https://rpubs.com/NateByers/Openair).</p>
  
  <p>Aq_measurements() general form</p>
  
  <p><code>AQ&lt;- (aq_measurements(country = “country”, city = “city”, location = “location”, parameter = “pollutant choice”, date_from = “YYYdateY-MM-DD”, date_to = “YYYY-MM-DD”)</code>. </p>
  
  <p>All parameters available in locations dataframe.</p>
  
  <p>worldmet() general form</p>
  
  <pre><code>met &lt;- importNOAA(code = "12345-12345", year = YYYYY:YYYY)
  </code></pre>
  
  <p>NOAA Code available in locations dataframe</p>
  
  <p>Below I create a sample of my initial data frame:</p>
  
  <pre><code>location = c("100 ail","16th and Whitmore","40AB01 - ANTWERPEN") 
  lastUpdated = c("2018-02-01 09:30:00", "2018-02-01 03:00:00", "2017-03-07 10:00:00") 
  firstUpdated = c("2015-09-01 00:00:00","2016-03-06 19:00:00","2016-11-22 15:00:00")
  pm25=c("FALSE","FALSE","FALSE")
  pm10=c("TRUE","FALSE","FALSE")
  no2=c("TRUE","FALSE","FALSE")
  latitude=c(47.932907,41.322470,36.809700)
  longitude=c(106.92139000,-95.93799000
  ,-107.65170000)
  
  df = data.frame(location, lastUpdated, firstUpdated,latitude,longitude,pm25,pm10,no2)
  </code></pre>
  
  <p>As a general idea I want to be able to click on a certain location in the map based on this dataframe. Then I have one <code>selectInput()</code> and 2 <code>dateInput()</code>. The 2 <code>dateInput()</code> should take as inputs the <code>df$firstUpdated</code> and <code>df$lastUpdated</code> respectively. Then the <code>selectInput()</code> should take as inputs the pollutants that exist in the <code>df</code> based on "TRUE"/"FALSE" value. And then the plots should be created. All of these should be triggered by clicking on the map.</p>
  
  <p>Up to now I was not able to achieve this so in order to help you understand I connected the <code>selectInput()</code> and the <code>dateInput()</code> with <code>input$loc</code> which is a <code>selectIpnut()</code> with locations in the first tab as I will not need this when I find the solution.</p>
  
  <pre><code>library(shiny)
  library(leaflet)
  library(plotly)
  library(shinythemes)
  library(htmltools)
  library(DT)
  library(utilr)
  library(openair)
  library(plotly)
  library(dplyr)
  library(ggplot2)
  library(gissr)
  library(ropenaq)
  library(worldmet)
  
  # Define UI for application that draws a histogram
     ui = navbarPage("ROPENAQ",
             tabPanel("CREATE DATAFRAME",
                      sidebarLayout(
  
                        # Sidebar panel for inputs ----
                        sidebarPanel(
                          wellPanel(
                            uiOutput("loc"),
                            helpText("Choose a Location to create the dataframe.")
                          )
                          ),
                        mainPanel(
  
                        )
                      )
             ),
             tabPanel("LEAFLET MAP",
                      leafletOutput("map"),
                      wellPanel(
                        uiOutput("dt"),
                        uiOutput("dt2"),
                        helpText("Choose a start and end date for the dataframe creation. Select up to 2 dates")
                      ),
                      "Select your Pollutant",
                      uiOutput("pollutant"),                     
  
                      helpText("While all pollutants are listed here, not all pollutants are measured at all locations and all times.  
                               Results may not be available; this will be corrected in further revisions of the app.  Please refer to the measurement availability 
                               in the 'popup' on the map."),
  
                      hr(),
                      fluidRow(column(8, plotOutput("tim")),
                               column(4,plotOutput("polv"))),
                      hr(),
  
                      fluidRow(column(4, plotOutput("win")),
                               column(8,plotOutput("cal"))),
                      hr(),
                      fluidRow(column(12, plotOutput("ser"))
                               )
             )
  
  
  )
  
  #server.r
  
  # load data
  # veh_data_full &lt;- readRDS("veh_data_full.RDS")
  # veh_data_time_var_type &lt;- readRDS("veh_data_time_var_type.RDS")
  df$location &lt;- gsub( " " , "+" , df$location)
  server = function(input, output, session) {
  
      output$pollutant&lt;-renderUI({
        selectInput("pollutant", label = h4("Choose Pollutant"), 
                    choices = colnames(df[,6:8]), 
                    selected = 1)
      })
  
  
      #Stores the value of the pollutant selection to pass to openAQ request      
  
      ###################################
     #output$OALpollutant &lt;- renderUI({OALpollutant})
  
  
      ##################################
      # create the map, using dataframe 'locations' which is polled daily (using ropenaq)
      #MOD TO CONSIDER: addd all available measurements to the popup - true/false for each pollutant, and dates of operation.
  
  
      output$map &lt;- renderLeaflet({
        leaflet(subset(df,(df[,input$pollutant]=="TRUE")))%&gt;% addTiles() %&gt;%
          addMarkers(lng = subset(df,(df[,input$pollutant]=="TRUE"))$longitude, lat = subset(df,(df[,input$pollutant]=="TRUE"))$latitude,
                     popup = paste("Location:", subset(df,(df[,input$pollutant]=="TRUE"))$location, "&lt;br&gt;",
                                   "Pollutant:", input$pollutant, "&lt;br&gt;",
                                   "First Update:", subset(df,(df[,input$pollutant]=="TRUE"))$firstUpdated, "&lt;br&gt;",
                                   "Last Update:", subset(df,(df[,input$pollutant]=="TRUE"))$lastUpdated
                                   ))
      })
      #Process Tab
     OAL_site &lt;- reactive({
          req(input$map_marker_click)
          location %&gt;%
              filter(latitude == input$map_marker_click$lat,
                     longitude == input$map_marker_click$lng)
  
  ###########
          #call Functions for data retrieval and processing.  Might be best to put all data request
          #functions into a seperate single function.  Need to:
          # call importNOAA() to retrieve meteorology data into temporary data frame
          # call aq_measurements() to retrieve air quality into a temporary data frame
          # merge meteorology and air quality datasets into one working dataset for computations; temporary
          # meteorology and air quality datasets to be removed.
          # call openAir() functions to create plots from merged file.  Pass output to a dashboard to assemble 
          # into appealing output.
          # produce output, either as direct download, or as an emailable PDF.
          # delete all temporary files and reset for next run.
      })
     #fun 
  
     output$loc&lt;-renderUI({
       selectInput("loc", label = h4("Choose location"),
                   choices = df$location ,selected = 1
       )
     })
  
     output$dt&lt;-renderUI({
  
                   dateInput('date',
                             label = 'First Available Date',
                             value = subset(df$firstUpdated,(df[,1]==input$loc))
                   )           
  
  
     })
     output$dt2&lt;-renderUI({
  
                   dateInput('date2',
                             label = 'Last available Date',
                             value = subset(df$lastUpdated,(df[,1]==input$loc))
                   )            
  
  
     })
  
     rt&lt;-reactive({
  
  
       AQ&lt;- aq_measurements(location = input$loc, date_from = input$dt,date_to = input$dt2,parameter = input$pollutant)
       met &lt;- importNOAA(year = 2014:2018)
       colnames(AQ)[9] &lt;- "date"
       merged&lt;-merge(AQ, met, by="date")
       # date output -- reports user-selected state &amp; stop dates in UI
       merged$location &lt;- gsub( " " , "+" , merged$location)
  
       merged
  
  
     })
     #DT  
  
       output$tim = renderPlot({
         timeVariation(rt(), pollutant = "value")
       })
  
  
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>The part of my code that I believe input$MAPID_click should be applied is:    </p>
  
  <pre><code>output$map &lt;- renderLeaflet({
        leaflet(subset(locations,(locations[,input$pollutant]=="TRUE")))%&gt;% addTiles() %&gt;%
          addMarkers(lng = subset(locations,(locations[,input$pollutant]=="TRUE"))$longitude, lat = subset(locations,(locations[,input$pollutant]=="TRUE"))$latitude,
                     popup = paste("Location:", subset(locations,(locations[,input$pollutant]=="TRUE"))$location, "&lt;br&gt;",
                                   "Pollutant:", input$pollutant, "&lt;br&gt;",
                                   "First Update:", subset(locations,(locations[,input$pollutant]=="TRUE"))$firstUpdated, "&lt;br&gt;",
                                   "Last Update:", subset(locations,(locations[,input$pollutant]=="TRUE"))$lastUpdated
                     ))
      })  
  
    output$dt&lt;-renderUI({
  
                   dateInput('date',
                             label = 'First Available Date',
                             value = subset(locations$firstUpdated,(locations[,1]==input$loc))
                   )           
  
  
     })
     output$dt2&lt;-renderUI({
  
                   dateInput('date2',
                             label = 'Last available Date',
                             value = subset(locations$lastUpdated,(locations[,1]==input$loc))
                   )            
  
  
     })
  
  
     rt&lt;-reactive({
  
  
  
       AQ&lt;- aq_measurements(location = input$loc, date_from = input$dt,date_to = input$dt2)
       met &lt;- importNOAA(year = 2014:2018)
       colnames(AQ)[9] &lt;- "date"
       merged&lt;-merge(AQ, met, by="date")
       # date output -- reports user-selected state &amp; stop dates in UI
       merged$location &lt;- gsub( " " , "+" , merged$location)
  
       merged
  
  
     })
     #DT  
  
  
  
  
  
  
  
  
  
  
       output$tim = renderPlot({
         timeVariation(rt(), pollutant = "value")
       })         
  </code></pre>