R/闪亮:从readOGR更改为read\u sf,闪亮的传单弹出窗口中断

R/闪亮:从readOGR更改为read\u sf,闪亮的传单弹出窗口中断,r,shiny,leaflet,R,Shiny,Leaflet,更新:下面添加了代码修复和注释,我的弹出窗口正在运行 这里是闪亮初学者,我有一个缓慢闪亮的传单应用程序,所以我一直在使用profvis查找瓶颈。使用readOGR加载shapefile数据是主要问题。所以我做了一个改变——使用read_sf——事情变得越来越快。我所有的点和多边形都显示得很好,但是我的弹出窗口现在不工作,我不知道会发生什么 预期结果:从readOGR更改为read\u sf不会对用数据填充弹出窗口产生任何影响 结果:标签工作正常,但弹出窗口根本不显示 这是该应用程序的精简版 ui

更新:下面添加了代码修复和注释,我的弹出窗口正在运行

这里是闪亮初学者,我有一个缓慢闪亮的传单应用程序,所以我一直在使用profvis查找瓶颈。使用readOGR加载shapefile数据是主要问题。所以我做了一个改变——使用read_sf——事情变得越来越快。我所有的点和多边形都显示得很好,但是我的弹出窗口现在不工作,我不知道会发生什么

预期结果:从readOGR更改为read\u 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)
              )

  )
)
  )
)
ui%
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”,
#这就是read_sf的突破之处
popup=mapply(函数(x,y){
HTML(sprintf(“,htmlEscape(x),y))},
placeography$placeref,placeography$placename,SIMPLIFY=F))%>%
addLayersControl(基本组=c(“打开”),覆盖组=c(“内部冲刺”),位置=c(“右上方”),选项=layersControlOptions(折叠=假))
})
}
库(闪亮)
图书馆(单张)
图书馆(rgdal)
图书馆(htmltools)
图书馆(底格里斯)
库(数据表)
图书馆(rmapshaper)
图书馆(shinycssloaders)
图书馆(sf)
#弹出窗口与READOGR配合得很好

#placeography以防其他人在从readOGR切换到read\u sf时遇到此类问题。。。。通过消除过程,我笨拙地找到了这个弹出问题的解决方案,并用注释更新了上面的问题,以解释我在哪里做了更改

对于我的大多数弹出窗口,只需添加“stringsAsFactors=TRUE”来读取sf就解决了问题(read sf的默认值与readOGR相反)。在我的更复杂的弹出窗口是进一步geojoin的产物的情况下,我需要将placeref列更改回join之后的一个因子:

as.factor(placeographyareashistpeople$placeref)


工作代码见上文。

更改histpeople的“stringsAsFactors=TRUE”时是否会出现其他类型的错误?是的,我多次收到相同的警告。但是我想我找到了解决办法。现在,我将其设置为StringsAsAffactors=FALSE,进行连接,然后在连接后转换为一个因子:as.factor(placeographyareashistpeople$placeref),砰的一声,我的弹出窗口开始工作!
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)

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)