以 shiny 格式保存 Tmap 图
Saving a Tmap plot in shiny
我想在 Shiny 中添加一个功能供用户导出地图,由 Tmap 制作。我知道 tmap 将地图转换为传单,但它不适用于 mapview::mapshot,因为许多答案都给出了在 Shiny 中保存 Leaflet 地图的答案。
None 以下作品:
map_expr <- reactive({
tm_shape(water1) +
tm_fill(col = "darkblue") +
tm_shape(county_outlines) +
tm_borders(col = "black") +
tm_shape(herd_summary) +
tm_symbols(col = "green", size = "REACTORS", scale = 0.15, alpha = 0.7) +
tm_shape(water1) +
tm_fill(col = "darkblue")
})
observe({
output$map <- renderTmap({
map_expr()
})
})
output$downloadData <- downloadHandler(
filename = "test.png",
content = function(file) {
# mapshot(map_expr(), file = file, cliprect = "viewport")
# tmap_save(map_expr(), file = file)
tmapProxy("map", session, {}) %>%
mapview::mapshot(file = file)
}
)
经过反复试验,我最终让它工作,并在此处包含一个可重现的示例,如下所示:
library(shiny)
library(tmap)
library(mapview)
data("World")
pal <- leaflet::colorBin(palette = grDevices::heat.colors(20), domain = World$HPI)
ui <- fluidPage(
titlePanel("Tmap Example"),
sidebarLayout(
sidebarPanel(
downloadButton("downloadData")
),
mainPanel(
tmapOutput("map")
)
)
)
server <- function(input, output) {
map_expr <- reactive({
tm_shape(World) +
tm_polygons("HPI")
})
output$map <- renderTmap({
map_expr()
})
output$downloadData <- downloadHandler(
filename = "test.png",
content = function(file) {
mapview::mapshot(tmap_leaflet(map_expr()), file = file)
}
)
}
shinyApp(ui = ui, server = server)
因此必须将 tmap 表达式保存到一个对象中(我将其保存到一个名为 map_expr 的反应对象中,因此在代码中的其他地方调用该对象时必须包含括号)。
使用 mapview 包中的函数 mapshot,您可以保存传单对象,tmap 中有一个名为 tamp_leaflet 的函数,可以将 tmap 对象转换为传单对象。
首先要在应用程序外部对其进行测试,以确保 mapshot 正常工作。为了让它工作,我必须安装包 webshot 并且必须安装 phantomJS,这可以通过以下功能完成:
webshot::install_phantomjs()
希望对您有所帮助!
library(tmap)
data("World")
# pal accepts a character vector of colours
tm_shape(World) +
tm_polygons("HPI", pal = c("#DC0032","#FFF5AC","#00B0F0"))
# You can also store the palette in pal,
# and then call pal(3) to return three colours
pal = grDevices::colorRampPalette(c("#DC0032","#FFF5AC","#00B0F0"))
tm_shape(World) +
tm_polygons("HPI", pal = pal(3))
使用tmap_save函数
output$downloadData <- downloadHandler(
filename = "example.png",
content = function(file) {
tmap_save(map_expr(), file)
}
我想在 Shiny 中添加一个功能供用户导出地图,由 Tmap 制作。我知道 tmap 将地图转换为传单,但它不适用于 mapview::mapshot,因为许多答案都给出了在 Shiny 中保存 Leaflet 地图的答案。
None 以下作品:
map_expr <- reactive({
tm_shape(water1) +
tm_fill(col = "darkblue") +
tm_shape(county_outlines) +
tm_borders(col = "black") +
tm_shape(herd_summary) +
tm_symbols(col = "green", size = "REACTORS", scale = 0.15, alpha = 0.7) +
tm_shape(water1) +
tm_fill(col = "darkblue")
})
observe({
output$map <- renderTmap({
map_expr()
})
})
output$downloadData <- downloadHandler(
filename = "test.png",
content = function(file) {
# mapshot(map_expr(), file = file, cliprect = "viewport")
# tmap_save(map_expr(), file = file)
tmapProxy("map", session, {}) %>%
mapview::mapshot(file = file)
}
)
经过反复试验,我最终让它工作,并在此处包含一个可重现的示例,如下所示:
library(shiny)
library(tmap)
library(mapview)
data("World")
pal <- leaflet::colorBin(palette = grDevices::heat.colors(20), domain = World$HPI)
ui <- fluidPage(
titlePanel("Tmap Example"),
sidebarLayout(
sidebarPanel(
downloadButton("downloadData")
),
mainPanel(
tmapOutput("map")
)
)
)
server <- function(input, output) {
map_expr <- reactive({
tm_shape(World) +
tm_polygons("HPI")
})
output$map <- renderTmap({
map_expr()
})
output$downloadData <- downloadHandler(
filename = "test.png",
content = function(file) {
mapview::mapshot(tmap_leaflet(map_expr()), file = file)
}
)
}
shinyApp(ui = ui, server = server)
因此必须将 tmap 表达式保存到一个对象中(我将其保存到一个名为 map_expr 的反应对象中,因此在代码中的其他地方调用该对象时必须包含括号)。
使用 mapview 包中的函数 mapshot,您可以保存传单对象,tmap 中有一个名为 tamp_leaflet 的函数,可以将 tmap 对象转换为传单对象。
首先要在应用程序外部对其进行测试,以确保 mapshot 正常工作。为了让它工作,我必须安装包 webshot 并且必须安装 phantomJS,这可以通过以下功能完成: webshot::install_phantomjs()
希望对您有所帮助!
library(tmap)
data("World")
# pal accepts a character vector of colours
tm_shape(World) +
tm_polygons("HPI", pal = c("#DC0032","#FFF5AC","#00B0F0"))
# You can also store the palette in pal,
# and then call pal(3) to return three colours
pal = grDevices::colorRampPalette(c("#DC0032","#FFF5AC","#00B0F0"))
tm_shape(World) +
tm_polygons("HPI", pal = pal(3))
使用tmap_save函数
output$downloadData <- downloadHandler(
filename = "example.png",
content = function(file) {
tmap_save(map_expr(), file)
}