首页 > 解决方案 > UseMethod中的错误(“激活”)没有适用于“激活”的方法应用于“函数”类的对象 - R

问题描述

我正在尝试从空间网络重现此代码(https://r-spatial.org/r/2019/09/26/spatial-networks.html)并出现错误

'''

图 <- 图 %>% 激活(边) %>% 变异(长度 = st_length(几何))

UseMethod(“activate”)中的错误:没有适用于“activate”的方法应用于类“function”的对象'''

完整代码如下:

muenster <- opq(bbox =  c(7.61, 51.954, 7.636, 51.968)) %>% 
  add_osm_feature(key = 'highway') %>% 
  osmdata_sf() %>% 
  osm_poly2line()


muenster_center <- muenster$osm_lines %>% 
  select(highway)

function(x, directed = TRUE) {
  
  edges <- x %>%
    mutate(edgeID = c(1:n()))
  
  nodes <- edges %>%
    st_coordinates() %>%
    as_tibble() %>%
    rename(edgeID = L1) %>%
    group_by(edgeID) %>%
    slice(c(1, n())) %>%
    ungroup() %>%
    mutate(start_end = rep(c('start', 'end'), times = n()/2)) %>%
    mutate(xy = paste(.$X, .$Y)) %>% 
    mutate(nodeID = group_indices(., factor(xy, levels = unique(xy)))) %>%
    select(-xy)
  
  source_nodes <- nodes %>%
    filter(start_end == 'start') %>%
    pull(nodeID)
  
  target_nodes <- nodes %>%
    filter(start_end == 'end') %>%
    pull(nodeID)
  
  edges = edges %>%
    mutate(from = source_nodes, to = target_nodes)
  
  nodes <- nodes %>%
    distinct(nodeID, .keep_all = TRUE) %>%
    select(-c(edgeID, start_end)) %>%
    st_as_sf(coords = c('X', 'Y')) %>%
    st_set_crs(st_crs(edges))
  
  tbl_graph(nodes = nodes, edges = as_tibble(edges), directed = directed)
  
}

graph <- graph %>%
  activate(edges) %>%
  mutate(length = st_length(geometry))




标签: rdplyrspatial

解决方案


推荐阅读