将传单图像保存在R中
您好,我正在尝试将传单图像保存在R Shining应用程序中。并且可以在添加简单标记时执行此操作。然而,最近我一直试图通过添加循环来概括这段代码,并发现一些问题,想知道是否有其他人可以提供帮助。参见下面的可复制示例将传单图像保存在R中,r,shiny,leaflet,R,Shiny,Leaflet,您好,我正在尝试将传单图像保存在R Shining应用程序中。并且可以在添加简单标记时执行此操作。然而,最近我一直试图通过添加循环来概括这段代码,并发现一些问题,想知道是否有其他人可以提供帮助。参见下面的可复制示例 Poly = data.frame(Strat = c("A","A","A","A","A","B","B","B","B","B"), long = c(174.5012, 174.5026, 174.5026, 174.5014,174.5012,174.5012 ,1
Poly = data.frame(Strat = c("A","A","A","A","A","B","B","B","B","B"), long = c(174.5012, 174.5026, 174.5026, 174.5014,174.5012,174.5012 ,174.5020, 174.5020,174.5012,174.5012),lat = c(-35.84014, -35.84018, -35.84137,-35.84138,-35.84014,-35.84014,-35.84014,-35.84197,-35.84197,-35.84014))
Points = data.frame(long = c(174.5014 ,174.5017, 174.5021, 174.5023, 174.5020, 174.5017 ,174.5021 ,174.5017, 174.5021, 174.5019), lat = c(-35.84187, -35.84165, -35.84220 ,-35.84121, -35.84133, -35.84034, -35.84082, -35.84101, -35.84112, -35.84084))
library('leaflet')
library('shiny')
library('webshot')
library('htmlwidgets')
##### My take on Example 2
ui <- fluidPage(
sidebarPanel(
checkboxInput('returnpdf', 'output pdf?', FALSE),
conditionalPanel(
condition = "input.returnpdf == true",
downloadLink('pdflink')
)
),
mainPanel(leafletOutput("map"))
)
server = function(input, output){
mymap <- reactive({
leaflet() %>% addTiles()%>%
clearShapes() %>%
clearMarkers() %>%
fitBounds(lng1 = 174.5042, lat1= -35.83814,lng2= 174.5001, lat2 = -35.8424)
})
output$map <- renderLeaflet({
mymap()
})
myfun <- function(map) {
print("adding points")
map %>% clearShapes() %>%
clearControls() %>%
clearMarkers() %>%
addCircles(lng = Points$long, lat = Points$lat, color = "blue",fillOpacity = 1,radius = 1)
}
AddStrataPoly <- function(map) {
print("adding polygons")
for(i in 1:length(unique(Poly$Strat))) {
map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1))
}
}
observe({
leafletProxy("map") %>% myfun() %>% AddStrataPoly()
})
newmap <- reactive({
mymap() %>% myfun() %>% AddStrataPoly()
})
output$pdflink <- downloadHandler(
filename = 'plot.pdf',
content = function(file) {
owd <- setwd(tempdir())
on.exit(setwd(owd))
saveWidget(newmap(), "temp.html", selfcontained = FALSE)
}
)
}
尽管在线查看器工作正常,但问题似乎在于downloadhandler。我已在函数AddStrataPoly()
中将问题隔离到循环中。并将AddStrataPoly()
函数替换为
AddStrataPoly <- function(map) {
print("adding polygons")
#for(i in 1:length(unique(Poly$Strat))) {
i = 1
map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1))
#}
}
AddStrataPoly%addPolygons(lng=Poly[Poly$Strat==unique(Poly$Strat)[i],]$long,lat=Poly[Poly$Strat==unique(Poly$Strat)[i],]$lat,layerId=unique(Poly$Strat)[i],颜色='gray60',options=list(fillOpacity=0.1))
#}
}
但很明显,它只绘制了一个多边形。有人能帮我把这段代码推广一下吗?这样我就可以画很多多边形了
非常感谢
C在
AddStrataPoly
函数中,如果要按顺序添加多边形,则需要将经过贴图的贴图替换为多边形:
试试这个:
AddStrataPoly <- function(map) {
print("adding polygons")
for(i in 1:length(unique(Poly$Strat))) {
map <- map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1))
}
map
}
selfcontained=TRUE
将在文件中包含所有依赖项,以便用户可以按原样打开它。我还将目标文件更改为文件
,因为函数希望将内容写入其中。很乐意提供帮助。如果这个答案解决了你的问题,请点击勾号将其标记为接受。抱歉,我刚刚意识到,在我使用这个网站的整个时间里,我一直在投票,而不是接受答案。
AddStrataPoly <- function(map) {
print("adding polygons")
for(i in 1:length(unique(Poly$Strat))) {
map <- map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1))
}
map
}
output$pdflink <- downloadHandler(
filename = 'temp.html',
content = function(file) {
saveWidget(newmap(), file, selfcontained = TRUE)
}
)