Learn R Programming

tmap (version 2.3-1)

renderTmap: Wrapper functions for using tmap in shiny

Description

Use tmapOutput to create a UI element, and renderTmap to render the tmap map. To update the map (more specifically, to add and remove layers) use tmapProxy. Adding layers is as usual, removing layers can be done with the function tm_remove_layer.

Usage

renderTmap(expr, env = parent.frame(), quoted = FALSE)

tmapOutput(outputId, width = "100%", height = 400)

tmapProxy(mapId, session = shiny::getDefaultReactiveDomain(), x)

tm_remove_layer(zindex)

Arguments

expr

A tmap object. A tmap object is created with qtm or by stacking tmap-elements.

env

The environment in which to evaluate expr

quoted

Is expr a quoted expression (with quote())? This is useful if you want to save an expression in a variable

outputId

Output variable to read from

width, height

the width and height of the map

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)

session

the Shiny session object to which the map belongs; usually the default value will suffice

x

the tmap object that specifies the added and removed layers.

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 renderTmap).

Details

Two features from tmap are not (yet) supported in Shiny: small multiples (facets) and colored backgrounds (argument bg.color of tm_layout). Workarounds for small multiples: create multiple independent maps or specify as.layers = TRUE in tm_facets.

Examples

Run this code
# NOT RUN {
if (require("shiny")) {
	
	
	data(World)
	world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
	
	
	ui <- fluidPage(
		tmapOutput("map"),
		selectInput("var", "Variable", world_vars)
	)
	
	server <- function(input, output, session) {
		output$map <- renderTmap({
			tm_shape(World) +
				tm_polygons(world_vars[1], zindex = 401)
		})
		
		observe({
			var <- input$var
			tmapProxy("map", session, {
				tm_remove_layer(401) +
				tm_shape(World) +
					tm_polygons(var, zindex = 401)
			})
		})
	}	
	
	
	app <- shinyApp(ui, server)
	if (interactive()) app
}
# }

Run the code above in your browser using DataLab