更新:在下面添加了代码修正和注释,我的弹出窗口正在工作.
这里有一个闪闪发亮的初学者,我有一个慢而闪亮的传单应用程序,所以我一直在使用profvis来寻找瓶颈。使用readOGR加载shapefile数据是主要问题。所以我做了一个改变--使用read_sf--事情要快得多。我所有的点和多边形都显示得很好,但是我的弹出窗口现在不起作用了,我不知道会发生什么。
预期的结果:从readOGR到read_sf的在使用数据填充弹出窗口方面没有任何区别。
结果:标签工作正常,但是弹出窗口根本没有出现.
下面是该应用程序的简化版本。
ui <- fluidPage(
fluidRow(
column(3,
"",
tags$head(
tags$style(type='text/css',
".nav-tabs {font-size: 10px} ")),
tabsetPanel(id='lefttabsetPanel',selected='placestab',
tabPanel(value="placestab",title='PLACES',
tags$iframe(name="myiframe2",seamless="seamless",src="http://45.56.98.26:8080/exist/rest/db/madrid/xml/tds-placeography.xml",style='height:95vh; width:25vw')
)
))
,
column(9,
"",
tabsetPanel(id='my_tabsetPanel',
tabPanel('Global Map',
withSpinner(leafletOutput(outputId="mymap",height = "95vh"),color="#cd0000",type = 5)
)
)
)
)
)
server <- function(input,output, session){
# Core wrapping function
wrap.it <- function(x, len)
{
sapply(x, function(y) paste(strwrap(y, len),
collapse = "\n"),
USE.NAMES = FALSE)
}
### MAP 1
output$mymap <- renderLeaflet({
m <- leaflet() %>%
addMapPane("toplayer", zIndex=420) %>% addMapPane("layer2",zIndex=410)%>%
setView(lng=-3.6898447, lat=40.4142174, zoom=3 ) %>%
addTiles(options = providerTileOptions(noWrap = TRUE), group="Open") %>%
addCircleMarkers(data = placeography,options = pathOptions(pane = "toplayer"),label=placeography$placename, fillColor="white",stroke = 2, weight=3, color="black",fillOpacity = 1,opacity=1,radius =3,group = "Puntos de interés",
# THIS IS WHAT'S BREAKING WITH read_sf
popup = mapply(function(x, y) {
HTML(sprintf("<div class='leaflet-popup-scrolled' style='font-size:10px;max-width:200px;max-height:150px; '><b><a href='http://45.56.98.26:8080/exist/rest/db/madrid/xml/tds-placeography.xml#%s' target='myiframe2'>%s</a></b></div>", htmlEscape(x), y))},
placeography$placeref,placeography$placename, SIMPLIFY = F))%>%
addLayersControl(baseGroups = c("Open"), overlayGroups = c("Puntos de interés"),position = c("topright"),options = layersControlOptions(collapsed = FALSE))
})
}
library(shiny)
library(leaflet)
library(rgdal)
library(htmltools)
library(tigris)
library(data.table)
library(rmapshaper)
library(shinycssloaders)
library(sf)
#POPUPS WORKED FINE WITH READOGR
#placeography <- readOGR("shapefiles/places_points.shp")
#POPUPS NOT WORKING WITH READ_SF
#placeography <- read_sf("shapefiles/places_points.shp",quiet=TRUE)
#MOST POPUPS WORKING WITH THIS READ_SF
placeography <- read_sf("shapefiles/places_points.shp",quiet=TRUE, as_tibble = FALSE,stringsAsFactors=TRUE)
shapefiles (places_points)在这里:http://45.56.98.26/shapefiles/
更新:I通过“使用stringsAsFactors =TRUE”来解决这个问题(上面的例子也适用):
placeography <- read_sf("shapefiles/places_points.shp",quiet=TRUE,as_tibble = FALSE,stringsAsFactors = TRUE)
不幸的是,我的一个标签(不包括在上面的示例中--参见下面)也使用了geojoin--它需要额外的一步来修复:
placeographyareas<-read_sf("shapefiles/places_areas.shp",quiet=TRUE,as_tibble = FALSE,stringsAsFactors = FALSE)
histpeople<- read.csv("http://45.56.98.26/tds-data/readingmadrid-people-places-hist.csv",header=TRUE,stringsAsFactors = FALSE)
placeographyareashistpeople<- geo_join(placeographyareas,histpeople,"placeref","placeref", how = "left")
#FIX: CONVERT TO FACTOR AFTER JOIN
placeographyareashistpeople$placeref <- as.factor(placeographyareashistpeople$placeref)
我在geojoin上收到警告:
警告:列placeref
连接因子和字符向量,胁迫到字符向量
我的弹出式也没出现。改变组织人的"stringsAsFactors=TRUE“也不起作用。仍然希望更好地理解sf_read和readOGR之间的区别,因此我可以更好地解决这个问题。谢谢!
发布于 2020-02-08 21:14:52
以防其他人在从readOGR切换到read_sf时遇到这种问题.我无意中找到了一个解决这个弹出式问题的过程,通过消除,并更新了上述问题的评论,以解释我在哪里作出的改变。
对于大多数弹出窗口,只要在read_sf中添加“read_sf = TRUE”就可以解决问题(read_sf的默认设置与readOGR相反)。在我的更复杂的弹出窗口中,它是进一步geojoin的产物,我需要在连接之后将placeref列更改为一个因子:
as.factor(placeographyareashistpeople$placeref).
有关工作代码,请参阅上文。
https://stackoverflow.com/questions/60126856
复制相似问题