Skip to content

Commit

Permalink
add example with Shiny gadget for Leaflet.Draw; cc @tim-salabim
Browse files Browse the repository at this point in the history
  • Loading branch information
timelyportfolio committed Jan 10, 2017
1 parent b4bc9d9 commit c5e27b4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions inst/examples/gadget_draw.R
@@ -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

4 comments on commit c5e27b4

@timelyportfolio
Copy link
Owner Author

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

leaflet() %>% addGeoJSON(drawn) %>% addTiles()

@tim-salabim
Copy link

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. the sf_intersect from the earlier example via shiny::observeEvent(input$intersect, { .....). Is that correct?

@bhaskarvk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @tim-salabim correct.

@timelyportfolio
Copy link
Owner Author

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

Please sign in to comment.