1

我正在尝试构建一个闪亮的应用程序,其中包含一个显示移动路径的传单地图,可以使用包含几何数据的数据集中的另外两个列进行双向过滤。

为此,我正在尝试使用 selectizeGroupUI(shinyWidgets 包),它允许双向/相互依赖的过滤。

但是,当我运行代码时,出现以下错误:

“警告:polygonData.default 中的错误:不知道如何从 data.frame 类的对象中获取路径数据”

我有一种感觉,这是因为传单地图中的映射路径(线串)数据要求底层数据集是 sf 对象,而 selectizeGroupUI 将 sf 对象转换为 data.table(?),因此出现错误消息。

这得到了以下事实的支持:当我将数据集从 sf 对象转换为 data.table 并尝试将路径绘制为单独的 A 和 B 坐标(没有连接线)时,整个过程完美运行。

知道是否存在解决方法吗?

任何帮助将不胜感激,请感谢!

一个代表:


library(tidyverse)
library(sf)
library(shiny)
library(shinyWidgets)

# generate the table with geometry data

geo_data <- structure(list(idx = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                           start_lat = c(33.40693,33.64672, 33.57127, 33.42848, 33.54936, 33.53418, 33.60399, 33.49554,33.5056, 33.61696),
                           start_long = c(-112.0298, -111.9255, -112.049,-112.0998, -112.0912, -112.0911, -111.9273, -111.9687, -112.0563, -111.9866),
                           end_lat = c(33.40687, 33.64776, 33.57125, 33.42853,33.54893, 33.53488, 33.60401, 33.49647, 33.5056, 33.61654),
                           end_long = c(-112.0343,-111.9303, -112.0481, -112.0993, -112.0912, -112.0911, -111.931,-111.9711, -112.0541, -111.986)),
                      row.names = c(NA, -10L), spec = structure(list(cols = list(idx = structure(list(), class = c("collector_double","collector")),
                                                                                 start_lat = structure(list(), class = c("collector_double", "collector")),
                                                                                 start_long = structure(list(), class = c("collector_double", "collector")),
                                                                                 end_lat = structure(list(), class = c("collector_double", "collector")),
                                                                                 end_long = structure(list(), class = c("collector_double","collector"))),
                                                                     default = structure(list(), class = c("collector_guess","collector")), delim = ","),
                                                                class = "col_spec"),class = c("data.table","data.frame"))


geo_data<- setDT(geo_data)

geo_data <- geo_data[
  , {
    geometry <- sf::st_linestring(x = matrix(c(start_lat, start_long, end_long, end_long), ncol = 2, byrow = T))
    geometry <- sf::st_sfc(geometry)
    geometry <- sf::st_sf(geometry = geometry)
  }
  , by = idx
  ]



# generate the table with columns to filter the geometry data, join with geometry data and convert to sf

table <- structure(list(idx = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                        column1 = c("A", "A", "A", "B", "B", "B", "C", "C", "C", "C"),
                        column2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), row.names = c(NA, -10L),
                   class = c("tbl_df","tbl", "data.frame")) %>%
  left_join(x = ., y = geo_data, by = "idx", keep = FALSE)


sf <- sf::st_as_sf(table)


# Shiny

ui <- fluidPage(
  fluidRow(
    column(
      width = 10, offset = 1,
      tags$h3("Filter data with selectize group"),
      panel(
        selectizeGroupUI(
          id = "my-filters",
          params = list(
            column1 = list(inputId = "column1", title = "column1:"),
            column2 = list(inputId = "column2", title = "column2:")
          )
        ), status = "primary"
      ),
      leafletOutput(outputId = "map")
    )
  )
)

server <- function(input, output, session) {
  
  res_mod <- callModule(
    module = selectizeGroupServer,
    id = "my-filters",
    data = sf,
    vars = c("column1", "column2"))
  
  
  
  output$map <- renderLeaflet({
    leaflet() %>%
    addPolylines(data = res_mod())
  })
  
}



shinyApp(ui, server)




4

1 回答 1

1

调用时res_mod(),它会返回一个 data.frame,但您可以st_as_sf()像任何其他具有geometry列的数据框对象一样再次将其强制返回。

    output$map <- renderLeaflet({
        leaflet() %>%
            addPolylines(data = st_as_sf(res_mod()))
    })

之后output$map再次开始工作。

于 2021-11-30T21:02:53.197 回答