首页 > 解决方案 > R/Shiny:从 readOGR 更改为 read_sf,闪亮的传单弹出窗口坏了

问题描述

更新:在下面添加了代码修复和评论,我的弹出窗口正在工作......

这里闪亮的初学者,我有一个缓慢闪亮的传单应用程序,所以我一直在使用 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/

更新: 我能够通过“使用 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 之间的区别,以便更好地解决这个问题。谢谢!

标签: rshinyleaflet

解决方案


以防万一其他人在从 readOGR 切换到 read_sf 时遇到此类问题......

对于我的大多数弹出窗口,只需将“stringsAsFactors = TRUE”添加到 read_sf 即可解决问题(read_sf 的默认值与 readOGR 相反)。在我的更复杂的弹出窗口是进一步地理连接的产物的情况下,我需要在连接后将 placeref 列更改回一个因子:

as.factor(placeographyareashistpeople$placeref)。

有关工作代码,请参见上文。


推荐阅读