2

Is there a function to find the input handlers for a package, generally? There are several special input handlers for leaflet, e.g. input$mymap_shape_mouseover that are not anywhere listed in the R documentation. Really, I just want to be able to grab the coordinates of a flat png heatmap that I'm using with leaflet and reformat them to grab the coordinates in the matrix I've previously plotted.

library(shiny)
library(leaflet)
library(leaflet.extras)
library(mapview)
library(foreach)
server <- function(input, output, session) {
  points <- eventReactive(input$recalc, {
    cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
  }, ignoreNULL = FALSE)
  output$mymap <- renderLeaflet({
    bounds <- c(0, 0, 14400, 14400)
    leaflet(options = leafletOptions(
      crs = leafletCRS(crsClass = "L.CRS.Simple"),
      minZoom = -5,
      maxZoom = 5)) %>%
      fitBounds(bounds[1], bounds[2], bounds[3], bounds[4]) %>%
      htmlwidgets::onRender("
                            function(el, t) {
                            var myMap = this;
                            var bounds = myMap.getBounds();
                            var image = new L.ImageOverlay(
                            'https://github.com/theaidenlab/juicebox/wiki/images/domains_peaks.png',
                            bounds);
                            image.addTo(myMap);
                            }") %>%
    addMeasure()  %>%
      addMiniMap( toggleDisplay = TRUE,
                  position = "bottomleft") %>% addDrawToolbar() %>% addFullscreenControl() %>% 
     addMouseCoordinates(style="basic")
  })
2

1 Answer 1

1

Discovering Leaflet Input Events

For those interested in leaflet input events - such as input$MAPID_center - please thank @blondclover. They recommended a nifty trick to print out all input events:

  1. Create the output$outputID in the UI using verbatimTextOutput; and
  2. Store the results of renderPrint({reactiveValuesToList(input)}) in the output$outputID object in server.

As the complexity of your leaflet map grows, knowing which leaflet input events are available to use will help you customize your map.

SS of Leaflet Input Events

# load necessary packages
library( shiny )
library( leaflet )
library( mapview )

ui <- fluidPage(
  leafletOutput( outputId = "map"),
  downloadButton( outputId = "dl"),
  h2("List of Input Events"),
  verbatimTextOutput( outputId = "text")
)

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

  # print list of input events
  output$text <-
    renderPrint({reactiveValuesToList(input)})

  # Create foundational leaflet map
  # and store it as a reactive expression
  foundational.map <- reactive({

    leaflet() %>% # create a leaflet map widget

      addTiles( urlTemplate = "https://{s}.tile.openstreetmap.se/hydda/base/{z}/{x}/{y}.png" ) # specify provider tile and type

  }) # end of foundational.map()

  # render foundational leaflet map
  output$map <- leaflet::renderLeaflet({

    # call reactive map
    foundational.map()

  }) # end of render leaflet

  # store the current user-created version
  # of the Leaflet map for download in 
  # a reactive expression
  user.created.map <- reactive({

    # call the foundational Leaflet map
    foundational.map() %>%

      # store the view based on UI
      setView( lng = input$map_center$lng
               ,  lat = input$map_center$lat
               , zoom = input$map_zoom
      )

  }) # end of creating user.created.map()



  # create the output file name
  # and specify how the download button will take
  # a screenshot - using the mapview::mapshot() function
  # and save as a PDF
  output$dl <- downloadHandler(
    filename = paste0( Sys.Date()
                       , "_customLeafletmap"
                       , ".pdf"
    )

    , content = function(file) {
      mapshot( x = user.created.map()
               , file = file
               , cliprect = "viewport" # the clipping rectangle matches the height & width from the viewing port
               , selfcontained = FALSE # when this was not specified, the function for produced a PDF of two pages: one of the leaflet map, the other a blank page.
      )
    } # end of content() function
  ) # end of downloadHandler() function

} # end of server

# run the Shiny app
shinyApp(ui = ui, server = server)

# end of script #
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.