r - 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))
解决方案
推荐阅读
- java - RestTemplate 有时会给出 500 内部服务器错误
- c# - 如何在 MVC 核心 c# 中使用 SaveOnServerPath Rotativa?
- sql - psql 的 \dt PATTERN 是如何工作的?
- javascript - 在javascript中将值乘以连续子字符串的简单方法
- node.js - 承诺待定的空气表
- python - 如何通过 Colab 在我的 Google Drive 中正确加载文件(包括模型)
- html - 如何制作具有多个子菜单的可悬停下拉导航菜单?
- c# - 如何查找在 C# .NET Core 中实现嵌套接口的所有类(类型)?
- email - Net::SMTPAuthenticationError (535-5.7.8 用户名和密码不被接受。了解更多信息): with devise mailer
- react-native - 反应原生 JSON 解析