Skip to content

Commit

Permalink
shiny working
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jul 16, 2024
1 parent d245f0b commit f9d7fac
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 65 deletions.
2 changes: 1 addition & 1 deletion R/onLoad.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# envir = environment()
.onLoad = function(...) {
options(tmap.style = "white", tmap.mode = "view", tmap.design.mode = FALSE,
options(tmap.style = "white", tmap.mode = "plot", tmap.design.mode = FALSE,
tmap.devel.mode = FALSE)
assign("tmapOptions", .defaultTmapOptions, envir = .TMAP)

Expand Down
2 changes: 2 additions & 0 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#' @param show show the map
#' @param vp viewport (for `"plot"` mode)
#' @param knit A logical, should knit?
#' @param in.shiny A logical, is the map drawn in **shiny**?
#' @param proxy A logical, if `in.shiny`, is \code{\link{tmapProxy}} used?
#' @param options A vector of options
#' @param ... not used
#' @export
Expand Down
6 changes: 3 additions & 3 deletions R/shiny.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Wrapper functions for using tmap in shiny
#' Wrapper functions for using **tmap** in **shiny**
#'
#' Use \code{tmapOutput} to create a UI element, and \code{renderTmap} to render the tmap map. To update the map (more specifically, to add and remove layers) use \code{tmapProxy}. Adding layers is as usual, removing layers can be done with the function \code{tm_remove_layer}.
#' Use \code{tmapOutput} to create a UI element, and \code{renderTmap} to render the tmap map. To update the map in \code{view} mode, use \code{tmapProxy}. Adding layers is as usual via the map layer functions like \code{\link{tm_polygons}}. Removing layers can be done , removing with the function \code{tm_remove_layer}.
#'
#' Two features from tmap are not (yet) supported in Shiny: small multiples (facets) and colored backgrounds (argument bg.color of \code{\link{tm_layout}}). Workarounds for small multiples: create multiple independent maps or specify as.layers = TRUE in \code{\link{tm_facets}}.
#'
Expand All @@ -13,7 +13,7 @@
#' @param 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)
#' @param session the Shiny session object to which the map belongs; usually the default value will suffice
#' @param x the tmap object that specifies the added and removed layers.
#' @param zindex the z index of the pane in which the layer is contained that is going to be removed. It is recommended to specify the zindex for this layer when creating the map (inside \code{renderTmap}).
#' @param zindex the z index of the pane in which the layer is contained that is going to be removed. It is recommended to specify the `zindex` for this layer when creating the map (inside \code{renderTmap}).
#' @param execOnResize If `TRUE` (default), when the plot is resized, the map is regenerated. When set to `FALSE` the map is rescaled: the aspect ratio is kept, but the layout will be less desirable.
#' @importFrom htmlwidgets shinyWidgetOutput
#' @rdname renderTmap
Expand Down
1 change: 1 addition & 0 deletions R/step1_rearrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ step1_rearrange = function(tmel) {
}
} else {
.TMAP$start_pane_id = 400
.TMAP$pane_ids = NULL
}

# create groups, for each group: tms (tmap shape), tmls (tmap layers), tmf (tmap facets)
Expand Down
7 changes: 2 additions & 5 deletions R/tmapLeafletInit.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ tmapLeafletInit = function(o, return.asp = FALSE, vp, prx, lf = NULL, ...) {

leaflet_opts$attributionControl = TRUE

print("init")


if (!.TMAP$proxy) {
.TMAP_LEAFLET$layerIds = list()
.TMAP_LEAFLET$layerIds = NULL
}


Expand Down Expand Up @@ -55,7 +52,7 @@ tmapLeafletInit = function(o, return.asp = FALSE, vp, prx, lf = NULL, ...) {
id = which(Lnames == pane_name(z))[1]
if (!is.na(id)) {
tp = Ltypes[id]
if (tp %in% c("polygons", "symbols")) {
if (tp %in% c("polygons", "symbols", "text", "lines")) {
L2 = c(L2, list(list(name = Lnames[id], type = tp, Lid = Lids[[id]])))
}
}
Expand Down
4 changes: 1 addition & 3 deletions R/tmapLeafletRun.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ tmapLeafletRun = function(o, q, show, knit, args) {
lfs2 = lapply(lfs, function(lfsi) {
x = if (o$nrows == 1 && o$ncols == 1) {
lf = lfsi[[1]]
print("run")
# proxy remove
if (!is.null(.TMAP_LEAFLET$layerIds2)) {
L = .TMAP_LEAFLET$layerIds
po(.TMAP_LEAFLET$layerIds2)
for (L2 in .TMAP_LEAFLET$layerIds2) {
if (L2$type == "raster") {
lf = leaflet::removeImage(lf, L2$Lid)
} else if (L2$type %in% c("symbols")) {
} else if (L2$type %in% c("symbols", "text")) {
lf = leaflet::removeMarker(lf, L2$Lid)
} else {
lf = leaflet::removeShape(lf, L2$Lid)
Expand Down
9 changes: 4 additions & 5 deletions R/tmapLeaflet_layers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
submit_labels = function(labels, cls, pane, group) {
print("submit")

layerIds = get("layerIds", envir = .TMAP_LEAFLET)

if (length(layerIds)) {
Expand Down Expand Up @@ -69,7 +67,6 @@ tmapLeafletPolygons = function(shpTM, dt, pdt, popup.format, hdt, idt, gp, bbx,

if (is.null(idt)) idt = submit_labels(dt$tmapID__, "polygons", pane, group)


if (o$use.WebGL) {
shp2 = sf::st_sf(id = seq_along(shp), geom = shp)
shp3 = suppressWarnings(sf::st_cast(shp2, "POLYGON"))
Expand Down Expand Up @@ -127,15 +124,17 @@ tmapLeafletLines = function(shpTM, dt, pdt, popup.format, hdt, idt, gp, bbx, fac
gp = rescale_gp(gp, o$scale_down)

opt = leaflet::pathOptions(interactive = TRUE, pane = pane)
if (is.null(idt)) idt = submit_labels(dt$tmapID__, "lines", pane, group)

if (o$use.WebGL) {
shp2 = sf::st_sf(id = seq_along(shp), geom = shp)
shp3 = suppressWarnings(sf::st_cast(shp2, "LINESTRING"))
gp3 = lapply(gp, function(gpi) {if (length(gpi) == 1) gpi else gpi[shp3$id]})
lf %>%
leafgl::addGlPolylines(data = shp3, color = gp3$col, opacity = gp3$col_alpha, weight = gp3$lwd, pane = pane, group = group) %>%
leafgl::addGlPolylines(data = shp3, color = gp3$col, opacity = gp3$col_alpha, weight = gp3$lwd, pane = pane, group = group, layerId = idt) %>%
assign_lf(facet_row, facet_col, facet_page)
} else {

lf %>%
leaflet::addPolylines(data = shp, layerId = idt, label = hdt, color = gp$col, opacity = gp$col_alpha, weight = gp$lwd, group = group, options = opt, dashArray = lty2dash(gp$lty), popup = popups) %>%
assign_lf(facet_row, facet_col, facet_page)
Expand Down Expand Up @@ -411,7 +410,7 @@ tmapLeafletText = function(shpTM, dt, gp, bbx, facet_row, facet_col, facet_page,
lf = lf %>% addLabelOnlyMarkers(lng = coords[i,1], lat = coords[i,2],
label=text[i],
group=group,
layerId = idt,
layerId = idt[i],
labelOptions = labelOptions(noHide = TRUE,
textOnly = TRUE,
pane = pane,
Expand Down
50 changes: 2 additions & 48 deletions examples/tmapOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,63 +27,17 @@ if (interactive() && require("shiny")) {
),
server <- function(input, output, session) {
output$map <- renderTmap({
tm_shape(World[1:3,], id = "iso_a3") +
tm_shape(World, id = "iso_a3") +
tm_polygons(world_vars[1], zindex = 401)
})
observe({
var <- input$var
tmapProxy("map", session, {
tm_remove_layer(401) +
tm_shape(World[1:3,], id = "iso_a3") +
tm_shape(World, id = "iso_a3") +
tm_polygons(var, zindex = 401)
})
})
},options = list(launch.browser=TRUE)
)

shinyApp(
ui = fluidPage(
tmapOutput("map", height = "600px"),
selectInput("var", "Variable", world_vars)
),
server <- function(input, output, session) {
output$map <- renderTmap({
tm_shape(World[1:3,], id = "iso_a3") +
tm_symbols(fill = world_vars[1], zindex = 401)
})
observe({
var <- input$var
tmapProxy("map", session, {
tm_remove_layer(401) +
tm_shape(World[1:3,], id = "iso_a3") +
tm_symbols(fill = var, zindex = 401)
})
})
},options = list(launch.browser=TRUE)
)

shinyApp(
ui = fluidPage(
tmapOutput("map", height = "600px"),
selectInput("var", "Variable", world_vars)
),
server <- function(input, output, session) {
output$map <- renderTmap({
tm_shape(World[1:3,], id = "iso_a3") +
tm_text(world_vars[1], zindex = 401)
})
observe({
var <- input$var
tmapProxy("map", session, {
tm_remove_layer(401) +
tm_shape(World[1:3,], id = "iso_a3") +
tm_text(var, zindex = 401)
})
})
},options = list(launch.browser=TRUE)
)

}



6 changes: 6 additions & 0 deletions man/print.tmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions man/renderTmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/tmap_leaflet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9d7fac

Please sign in to comment.