Skip to content

Commit

Permalink
Attach htmlwidget binding dependencies to leafletOutput() (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Sep 27, 2022
1 parent 6d0db38 commit 7b416d4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 52 deletions.
65 changes: 65 additions & 0 deletions R/dependencies.R
@@ -1,3 +1,68 @@
# N.B. These are all the dependencies required to define leaflet's htmlwidget
# binding. We've intentionally avoided htmlwidgets' yaml approach to defining
# these since jQuery (in particular) wants to be defined with R code and must be
# provided first. For static rendering, it's sufficient to provide these to
# htmlwidgets::createWidget(dependencies = ...); however, when we dynamically
# render (via renderLeaflet()) the output container (i.e., leafletOutput()) also
# needs these dependencies attached (without them, the output binding won't be
# registered in the time when shiny binds to the DOM). Typically, we don't need
# to do this since htmlwidgets will automatically attach dependencies defined in
# yaml to the output container (which we cannot do anymore).
leafletBindingDependencies <- function() {
list(
jquerylib::jquery_core(3),
htmltools::htmlDependency(
name = "leaflet",
version = "1.3.1",
package = "leaflet",
src = "htmlwidgets/lib/leaflet",
script = "leaflet.js",
stylesheet = "leaflet.css"
),
htmltools::htmlDependency(
name = "leafletfix",
version = "1.0.0",
package = "leaflet",
src = "htmlwidgets/lib/leafletfix",
stylesheet = "leafletfix.css"
),
htmltools::htmlDependency(
name = "proj4",
version = "2.6.2",
package = "leaflet",
src = "htmlwidgets/plugins/Proj4Leaflet",
script = "proj4.min.js",
all_files = FALSE
),
htmltools::htmlDependency(
name = "Proj4Leaflet",
version = "1.0.1",
package = "leaflet",
src = "htmlwidgets/plugins/Proj4Leaflet",
script = "proj4leaflet.js",
all_files = FALSE
),
htmltools::htmlDependency(
name = "rstudio_leaflet",
version = "1.3.1",
package = "leaflet",
src = "htmlwidgets/lib/rstudio_leaflet",
stylesheet = "rstudio_leaflet.css"
),
# Include the rstudio leaflet binding last
# https://github.com/ramnathv/htmlwidgets/blob/7b9c1ea3d9fbf4736d84f1fd1178fce0af29f8e3/R/utils.R#L59-L68
htmltools::htmlDependency(
name = "leaflet-binding",
version = get_package_version("leaflet"),
src = "htmlwidgets/assets",
package = "leaflet",
script = "leaflet.js",
all_files = FALSE
)
)
}


#' Various leaflet dependency functions for use in downstream packages
#' @examples \dontrun{
#' addBootStrap <- function(map) {
Expand Down
52 changes: 1 addition & 51 deletions R/leaflet.R
Expand Up @@ -89,57 +89,7 @@ leaflet <- function(data = NULL, width = NULL, height = NULL,
widget
},
elementId = elementId,
dependencies = list(
jquerylib::jquery_core(3),
htmltools::htmlDependency(
name = "leaflet",
version = "1.3.1",
package = "leaflet",
src = "htmlwidgets/lib/leaflet",
script = "leaflet.js",
stylesheet = "leaflet.css"
),
htmltools::htmlDependency(
name = "leafletfix",
version = "1.0.0",
package = "leaflet",
src = "htmlwidgets/lib/leafletfix",
stylesheet = "leafletfix.css"
),
htmltools::htmlDependency(
name = "proj4",
version = "2.6.2",
package = "leaflet",
src = "htmlwidgets/plugins/Proj4Leaflet",
script = "proj4.min.js",
all_files = FALSE
),
htmltools::htmlDependency(
name = "Proj4Leaflet",
version = "1.0.1",
package = "leaflet",
src = "htmlwidgets/plugins/Proj4Leaflet",
script = "proj4leaflet.js",
all_files = FALSE
),
htmltools::htmlDependency(
name = "rstudio_leaflet",
version = "1.3.1",
package = "leaflet",
src = "htmlwidgets/lib/rstudio_leaflet",
stylesheet = "rstudio_leaflet.css"
),
# Include the rstudio leaflet binding last
# https://github.com/ramnathv/htmlwidgets/blob/7b9c1ea3d9fbf4736d84f1fd1178fce0af29f8e3/R/utils.R#L59-L68
htmltools::htmlDependency(
name = "leaflet-binding",
version = get_package_version("leaflet"),
src = "htmlwidgets/assets",
package = "leaflet",
script = "leaflet.js",
all_files = FALSE
)
)
dependencies = leafletBindingDependencies()
)

if (crosstalk::is.SharedData(data)) {
Expand Down
6 changes: 5 additions & 1 deletion R/shiny.R
Expand Up @@ -19,7 +19,11 @@
#'
#' \donttest{if (interactive()) app}
leafletOutput <- function(outputId, width = "100%", height = 400) {
htmlwidgets::shinyWidgetOutput(outputId, "leaflet", width, height, "leaflet")
htmltools::attachDependencies(
htmlwidgets::shinyWidgetOutput(outputId, "leaflet", width, height, "leaflet"),
leafletBindingDependencies(),
append = TRUE
)
}

# use expr description from htmlwidgets to avoid bad inherit params code
Expand Down

0 comments on commit 7b416d4

Please sign in to comment.