forked from rstudio/leaflet
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example with Shiny gadget for Leaflet.Draw; cc @tim-salabim
- Loading branch information
1 parent
b4bc9d9
commit c5e27b4
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# start toward a Shiny gadget for Leaflet and Leaflet.Draw | ||
# still missing many features but hopefully serves | ||
# as proof of concept | ||
|
||
#' Leaflet Draw Shiny Gadget | ||
#' | ||
#' @param lf leaflet map currently with \code{addDrawToolbar} already | ||
#' added. | ||
#' @param width,height valid \code{CSS} size for the gadget | ||
|
||
leafdraw_gadget <- function(lf = NULL, height = NULL, width = NULL) { | ||
# modeled after chemdoodle gadget | ||
# https://github.com/zachcp/chemdoodle/blob/master/R/chemdoodle_sketcher_gadgets.R | ||
stopifnot(requireNamespace("miniUI"), requireNamespace("shiny")) | ||
ui <- miniUI::miniPage( | ||
miniUI::miniContentPanel(lf, height=NULL, width=NULL), | ||
|
||
miniUI::gadgetTitleBar("Draw Something", right = miniUI::miniTitleBarButton("done", "Done", primary = TRUE)) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
shiny::observeEvent(input$done, { shiny::stopApp(input$undefined_draw_new_feature) }) | ||
shiny::observeEvent(input$cancel, { shiny::stopApp (NULL) }) | ||
} | ||
|
||
shiny::runGadget( | ||
ui, | ||
server, | ||
viewer = shiny::dialogViewer("View and Edit Data"), | ||
stopOnCancel = FALSE | ||
) | ||
} | ||
|
||
|
||
# example use | ||
library(leaflet) | ||
library(leaflet.extras) | ||
library(mapview) | ||
|
||
lf <- mapview(breweries91)@map %>% | ||
addTiles() %>% | ||
addDrawToolbar() | ||
|
||
drawn <- leafdraw_gadget(lf) | ||
drawn |
c5e27b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To extend the example, we can
c5e27b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! This is what I was looking for! I was not aware of
miniUI
, this is great for our needs.My head is already scheming out all the possibilities. I am not experienced with shiny, but if I am not mistaken we can use the
shiny::observeEvent(input$done, { .....
events to provide the respective functionalities we want (e.g. thesf_intersect
from the earlier example viashiny::observeEvent(input$intersect, { .....
). Is that correct?c5e27b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @tim-salabim correct.
c5e27b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more complicated example just added that will collect all drawn and edited shapes 4c75faf