Leaflet R闪亮的应用程序,传单地图不以选定点为中心

Leaflet R闪亮的应用程序,传单地图不以选定点为中心,leaflet,zooming,center,Leaflet,Zooming,Center,我在R Shining应用程序中有一张传单地图,地图不会居中并重新聚焦于选定的位置。令人沮丧的是,这适用于人口普查数据质心,但不适用于我的数据 我有下面的代码,如果我使用人口普查中的一些虚拟数据,它可以工作,但是当我使用我自己的数据(可在Github上获得)时,它不会工作。我怀疑我的数据有什么问题,但我似乎无法理解它可能是什么 #Load libraries ########################################## library(shiny) librar

我在R Shining应用程序中有一张传单地图,地图不会居中并重新聚焦于选定的位置。令人沮丧的是,这适用于人口普查数据质心,但不适用于我的数据

我有下面的代码,如果我使用人口普查中的一些虚拟数据,它可以工作,但是当我使用我自己的数据(可在Github上获得)时,它不会工作。我怀疑我的数据有什么问题,但我似乎无法理解它可能是什么


#Load libraries
##########################################
library(shiny)    
library(shinyWidgets)
library(tigris)
library(leaflet)
library(rgeos)
library(rgdal)


#Get data from here - https://github.com/JoshRoll/ODOT-Projects/blob/master/Bend_Spatial_Data_2018.gdb.zip

#Count Location spatial information
##############
#Define the location where you unzipped the downloaded file
fgdb <-     "Bend_Spatial_Data_2018.gdb"
# Read the feature class
Count_Location_Info_Sp <-  readOGR(dsn=fgdb,layer= "MMCountLocations")

# Load data- Use census to use as proper spatial transformation from x/y to lat/long (Uses tigris package)
States_Sp <- states( year = "2010")
#Reproject
Count_Location_Info_Sp <-  spTransform(Count_Location_Info_Sp, CRS(proj4string( States_Sp)))  

#Create a data frame from spatial data
Data.. <- Count_Location_Info_Sp@data

#Set up User Interface
######################
ui <- fluidPage(
  titlePanel("LOcation Selector Test"),
  tabsetPanel(
    #Daily Counts Panel
    ##############
    #Hourly Counts Panel
    #######################
    tabPanel("Tab 1",
             #Call plot 
             fluidRow(
               column(3,
                      uiOutput("Location_Selector"))),
             #Location Details 
             fluidRow( 
               column(6,
                      #h4("Selected Location"),
                      leafletOutput("map_plot",height = 500))
               #Close row
             )
             #Close panel
    )
    #Close setPanel
  )
  #Page end   
)

#Set up Server
#---------------------------
server <- shinyServer(function(session,input,output){
  #Location selector
  observe({
    output$Location_Selector <- renderUI({
      selectInput(inputId = "Location_Selector",
                  label = "Select Location", multiple = FALSE,
                  choices = as.character(unique(Data..$Sub_Location_Id)),
                  selected =  unique(Data..$Sub_Location_Id)[1])
    })
  })
  #Set up starting leaflet
  ###############
  output$map_plot <- renderLeaflet({
    leaflet(Count_Location_Info_Sp) %>%
      addTiles() %>%
      addCircles(color = "black" )
    })
  #Set up proxy leaflet for updated selector
  ####################
  observe({
    dat <-  Count_Location_Info_Sp[Count_Location_Info_Sp@data$Sub_Location_Id%in%input$Location_Selector,]
    lat <-  coordinates( dat)[,1]
    long <-  coordinates(dat)[,2]
    leafletProxy("map_plot") %>% 
      clearShapes() %>%
      addTiles() %>%
      addCircles(data =dat ,color = "black" ) %>%
      setView(lng = long, lat = lat, zoom = 14)
   #Close leaflet proxy observe
  })


})
#Run App
shinyApp(ui,server)


#加载库
##########################################
图书馆(闪亮)
图书馆(shinyWidgets)
图书馆(底格里斯)
图书馆(单张)
图书馆(rgeos)
图书馆(rgdal)
#从这里获取数据-https://github.com/JoshRoll/ODOT-Projects/blob/master/Bend_Spatial_Data_2018.gdb.zip
#统计位置空间信息
##############
#定义解压下载文件的位置
fgdb简单的功能(
sf
)包更容易使用(在我看来),并且比使用
sp
功能更丰富。我会这样做的

我所做的只是改变了使用sf包读取数据的方式。我们将其转换为标准坐标参考系(
crs
)。数据集正在使用不同的坐标参照系

最后,在
sf
中,您不需要索引到
@data
。您可以将数据帧
Count\u Location\u Info\u Sp
视为常规的旧数据帧(尽管有一些附加功能)

#加载库
##########################################
图书馆(闪亮)
图书馆(shinyWidgets)
图书馆(底格里斯)
图书馆(单张)
图书馆(rgeos)
图书馆(地球圈)
图书馆(sf)
#从这里获取数据-https://github.com/JoshRoll/ODOT-Projects/blob/master/Bend_Spatial_Data_2018.gdb.zip
#统计位置空间信息
##############
#定义解压下载文件的位置

fgdb请发布一个最小可复制的示例。上述代码应在从嵌入链接下载空间文件后生效。我试着用美国人口普查数据做一个例子,使用底格里斯软件包在飞行中下载,结果发现问题消失了。然后,我将我的数据移植到我的工作人口普查数据示例中,结果发现重新缩放问题又出现了。我在这里希望避免发疯。请让我知道上面的代码/数据是否存在问题。这是可行的,但我不确定为什么,因为数据本质上是相同的,只是不同的类,空间点数据帧与sf对象?当我用人口普查数据模拟这一点时,它起了作用,那是美国各州的空间点数据框架(质心),所以你知道为什么它现在会起作用吗?我认为坐标参考框架存在问题。尝试将上一代码的crs转换为WGS84。还有
Count\u Location\u Info_Sp@data$Sub_Location_Id
是一个因子,而不是字符向量。感谢Sada93,我确保重新投影到您建议的投影中,并将因子变量转换为字符,但它仍然不能像您建议的那样工作。这似乎不是投影或因子问题,因为我使用了示例中使用的原始项目文件(来自普查),并且没有将Sub_Location_Id转换为字符,而是使用了st_read()和st_transform(),并且工作正常,非常奇怪。当您使用我的代码运行我的问题时,您是否能够复制它。我很高兴实现您的解决方案,但现在我很好奇为什么使用readOGR加载数据会导致这种情况?
#Load libraries
##########################################
library(shiny)    
library(shinyWidgets)
library(tigris)
library(leaflet)
library(rgeos)
library(geosphere)
library(sf)


#Get data from here - https://github.com/JoshRoll/ODOT-Projects/blob/master/Bend_Spatial_Data_2018.gdb.zip

#Count Location spatial information
##############
#Define the location where you unzipped the downloaded file
fgdb <-     "~/Downloads/Bend_Spatial_Data_2018.gdb"
# Read the feature class
Count_Location_Info_Sp <-  st_read(dsn=fgdb,layer= "MMCountLocations",stringsAsFactors = FALSE)
Count_Location_Info_Sp <- st_transform(Count_Location_Info_Sp, crs = "+proj=longlat +datum=WGS84")


#Set up User Interface
######################
ui <- fluidPage(
  titlePanel("LOcation Selector Test"),
  tabsetPanel(
    #Daily Counts Panel
    ##############
    #Hourly Counts Panel
    #######################
    tabPanel("Tab 1",
             #Call plot 
             fluidRow(
               column(3,
                      uiOutput("Location_Selector"))),
             #Location Details 
             fluidRow( 
               column(6,
                      #h4("Selected Location"),
                      leafletOutput("map_plot",height = 500))
               #Close row
             )
             #Close panel
    )
    #Close setPanel
  )
  #Page end   
)

#Set up Server
#---------------------------
server <- shinyServer(function(session,input,output){
  #Location selector
  observe({
    output$Location_Selector <- renderUI({
      selectInput(inputId = "Location_Selector",
                  label = "Select Location", multiple = FALSE,
                  choices = as.character(unique(Data..$Sub_Location_Id)),
                  selected =  unique(Data..$Sub_Location_Id)[1])
    })
  })
  #Set up starting leaflet
  ###############
  output$map_plot <- renderLeaflet({
    leaflet(Count_Location_Info_Sp) %>%
      addTiles() %>%
      addCircles(color = "black" )
  })
  #Set up proxy leaflet for updated selector
  ####################
  observe({
    req(input$Location_Selector)

    dat <-  Count_Location_Info_Sp[Count_Location_Info_Sp$Sub_Location_Id %in% input$Location_Selector,]
    lat <-  st_coordinates(dat)[[2]]
    long <-  st_coordinates(dat)[[1]]
    leafletProxy("map_plot") %>%
      clearShapes() %>%
      addTiles() %>%
      addCircles(data =dat ,color = "black" ) %>%
      setView(lng = long, lat = lat, zoom = 14)
    #Close leaflet proxy observe
  })


})
#Run App
shinyApp(ui,server)