【发布时间】:2020-02-08 12:36:55
【问题描述】:
更新:在下面添加了代码修复和 cmets,我的弹出窗口正在工作...
这里是闪亮的初学者,我有一个缓慢闪亮的传单应用程序,所以我一直在使用 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)
shapefile(places_points)在这里:http://45.56.98.26/shapefiles/
更新: 通过“使用 stringsAsFactors = TRUE”,我能够中途解决这个问题(它适用于上面的示例):
placeography <- read_sf("shapefiles/places_points.shp",quiet=TRUE,as_tibble = FALSE,stringsAsFactors = TRUE)
不幸的是,我的一个标签(不包括在上面的示例中——见下文)也使用了地理连接——它需要一个额外的步骤来修复:
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加入因子和字符向量,强制转换为字符向量
而且我的弹出窗口没有出现。为 histpeople 更改“stringsAsFactors=TRUE”也不起作用。仍然希望能更好地理解 sf_read 和 readOGR 之间的区别,以便更好地解决这个问题。谢谢!
【问题讨论】:
-
在更改 histpeople 的“stringsAsFactors=TRUE”时是否会出现不同类型的错误?
-
是的,我多次收到相同的警告。但是...我想我设法找到了解决办法。我现在将其设置为 stringsAsFactors=FALSE,进行连接,然后在连接后转换为一个因子:as.factor(placeographyareashistpeople$placeref) 并且,砰,我的弹出窗口有效!