【问题标题】:R/Shiny: Changed from readOGR to read_sf and shiny leaflet popups brokeR/Shiny:从 readOGR 更改为 read_sf,闪亮的传单弹出窗口坏了
【发布时间】: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) 并且,砰,我的弹出窗口有效!

标签: r shiny leaflet


【解决方案1】:

以防万一其他人在从 readOGR 切换到 read_sf 时遇到此类问题...。我通过消除过程偶然发现了此弹出问题的解决方案,并用 cmets 更新了上面的问题以解释我在哪里做了变化。

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

as.factor(placeographyareashistpeople$placeref)。

工作代码见上文。

【讨论】:

    猜你喜欢
    • 2017-02-27
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    相关资源
    最近更新 更多