Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach htmlwidget binding dependencies to leafletOutput() #821

Merged
merged 1 commit into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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