-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathleafletProxy.Rd
75 lines (65 loc) · 2.48 KB
/
leafletProxy.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{leafletProxy}
\alias{leafletProxy}
\title{Send commands to a Leaflet instance in a Shiny app}
\usage{
leafletProxy(
mapId,
session = shiny::getDefaultReactiveDomain(),
data = NULL,
deferUntilFlush = TRUE
)
}
\arguments{
\item{mapId}{single-element character vector indicating the output ID of the
map to modify (if invoked from a Shiny module, the namespace will be added
automatically)}
\item{session}{the Shiny session object to which the map belongs; usually the
default value will suffice}
\item{data}{a data object; see Details under the \code{\link[=leaflet]{leaflet()}} help
topic}
\item{deferUntilFlush}{indicates whether actions performed against this
instance should be carried out right away, or whether they should be held
until after the next time all of the outputs are updated; defaults to
\code{TRUE}}
}
\description{
Creates a map-like object that can be used to customize and control a map
that has already been rendered. For use in Shiny apps and Shiny docs only.
}
\details{
Normally, you create a Leaflet map using \code{\link[=leaflet]{leaflet()}}.
This creates an in-memory representation of a map that you can customize
using functions like \code{\link[=addPolygons]{addPolygons()}} and \code{\link[=setView]{setView()}}.
Such a map can be printed at the R console, included in an R Markdown
document, or rendered as a Shiny output.
In the case of Shiny, you may want to further customize a map, even after it
is rendered to an output. At this point, the in-memory representation of the
map is long gone, and the user's web browser has already realized the Leaflet
map instance.
This is where \code{leafletProxy()} comes in. It returns an object that can
stand in for the usual Leaflet map object. The usual map functions like
\code{\link[=addPolygons]{addPolygons()}} and \code{\link[=setView]{setView()}} can be called, and
instead of customizing an in-memory representation, these commands will
execute on the live Leaflet map instance.
}
\examples{
library(shiny)
ui <- fluidPage(
leafletOutput("map1")
)
map <- leaflet() \%>\% addCircleMarkers(
lng = runif(10),
lat = runif(10),
layerId = paste0("marker", 1:10))
server <- function(input, output, session) {
output$map1 <- renderLeaflet(map)
observeEvent(input$map1_marker_click, {
leafletProxy("map1", session) \%>\%
removeMarker(input$map1_marker_click$id)
})
}
app <- shinyApp(ui, server)
\donttest{if (interactive()) app}
}