我创建了一个闪亮的仪表板来分析芝加哥的犯罪行为。因此,我还制作了芝加哥的传单地图,我们可以根据标记看到芝加哥哪个地区发生了多少犯罪事件。我有三个过滤器小部件(日期、犯罪类型和位置)。基于这 3 个小部件,用户可以随意使用,并且每次都会使用所选输入再次绘制地图。一切都很完美,我只是想添加一个“下载”部分,其中带有标记的当前绘图可以导出为不同的格式(png、pdf ...)。我的仪表板看起来像这样:
仪表板 - 地图页面 https://i.stack.imgur.com/CYylY.png
正如您所看到的,我想让用户选择他想要的导出格式。我尝试了一些代码,但没有成功。
My ui.R:
tabItem(tabName = "map",
fluidRow(
column(width = 9,
# Main plot
box(
title = "Chicago Map",
status = "danger",
solidHeader = TRUE,
width = NULL,
height = NULL,
#tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("map",height = "95vh")
)),
column(width = 3,
box(
title = "Date",
status = "primary",
solidHeader = TRUE,
width = NULL,
#height = 142,
sliderInput("mapYear", label = "Select Year", min = 2001, max = 2016, step = 1, sep = '', value = c(2001,2016))
),
box(
title = "Crime Type",
status = "primary",
solidHeader = TRUE,
width = NULL,
selectInput("mapCrimeType", label= "Select Crime Type", choices = unique(cc$Primary.Type), multiple = TRUE)
),
box(
title = "Location",
status = "primary",
solidHeader = TRUE,
width = NULL,
selectInput("mapLocation", label= "Select Location", choices = unique(cc$Location.Description), multiple = TRUE)
),
box(
title = "Download",
status = "success",
solidHeader = TRUE,
width = NULL,
radioButtons("mapFormat", "Document format", c("PNG"="png", "EPS"="eps", "PDF"="pdf"), inline = TRUE, selected = "png"),
downloadButton("mapDownload")
)
)
)
),
我的服务器是这样的:
### Draw Map ###
output$map = renderLeaflet({
m <- leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
setView(lng = -87.623177, lat = 41.881832, zoom=11)
})
reactMap = reactive({
cc %>%
filter(Primary.Type %in% input$mapCrimeType &
Location.Description %in% input$mapLocation &
Year2 %in% cbind(input$mapYear[1],input$mapYear[2]))
})
observe({
proxy = leafletProxy("map", data = reactMap()) %>%
clearMarkers() %>%
clearMarkerClusters() %>%
addCircleMarkers(clusterOptions = markerClusterOptions(),
lng =~ Longitude, lat =~ Latitude, radius = 5, group = 'Cluster',
popup =~ paste('<b><font color="Black">', 'Crime Information',
'</font></b><br/>', 'Crime Type:', Primary.Type,'<br/>',
'Date:', Date,'<br/>', #'Time:', Time,'<br/>',
'Location:', Location.Description,'<br/>', 'Block:', Block, '<br/>', 'Arrest:', Arrest, '<br/>'))
})
fnMap <- reactive({paste(input$mapCrimeType,input$mapLocation,sep = ".")})
doMap <- reactive({input$mapFormat})
# Download current plot
output$mapDownload <- downloadHandler(
filename = fnMap,
content = function(file) {
mapshot(m, file)
}
)
有人有任何想法或建议吗?我很乐意解决这个问题。非常感谢!
更新:有人知道如何解决我的代码吗?其他有类似问题的帖子对我不起作用。