ropensci / stplanr

Sustainable transport planning with R
https://docs.ropensci.org/stplanr
Other
416 stars 66 forks source link

bug in the rnet_merge function when defining the funs #533

Closed wangzhao0217 closed 10 months ago

wangzhao0217 commented 10 months ago

when set funs <- Null or via

name_list <- names(rnet_y)
# Initialize an empty list
funs <- list()
# Loop through each name and assign it a function based on specific conditions
for (name in name_list) {
  if (name == "geometry") {
    next  # Skip the current iteration
  } else if (name %in% c("Gradient", "Quietness")) {
    funs[[name]] <- mean
  } else {
    funs[[name]] <- sum
  }
}

This error will pop up

rnet_merged = rnet_merge(rnet_x, rnet_y, dist = 20, segment_length = 10, fun$ st_as_s2(): dropping Z and/or M coordinate st_as_s2(): dropping Z and/or M coordinate Joining with by = join_by(identifier) |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=06m 37s New names: • all_fastest_bicycle_ebike -> all_fastest_bicycle_ebike...3all_fastest_bicycle_go_dutch -> all_fastest_bicycle_go_dutch...4all_fastest_bicycle_ebike -> all_fastest_bicycle_ebike...5all_fastest_bicycle_go_dutch -> all_fastest_bicycle_go_dutch...6all_quietest_bicycle_ebike -> all_quietest_bicycle_ebike...8all_quietest_bicycle_go_dutch -> all_quietest_bicycle_go_dutch...9all_quietest_bicycle_ebike -> all_quietest_bicycle_ebike...10all_quietest_bicycle_go_dutch -> all_quietest_bicycle_go_dutch...11commute_fastest_bicycle_ebike -> commute_fastest_bicycle_ebike...13commute_fastest_bicycle_go_dutch -> commute_fastest_bicycle_go_dutch...14commute_fastest_bicycle_ebike -> commute_fastest_bicycle_ebike...15commute_fastest_bicycle_go_dutch -> commute_fastest_bicycle_go_dutch...16commute_quietest_bicycle_ebike -> commute_quietest_bicycle_ebike...18commute_quietest_bicycle_go_dutch -> commute_quietest_bicycle_go_dutch...19commute_quietest_bicycle_ebike -> commute_quietest_bicycle_ebike...20commute_quietest_bicycle_go_dutch -> commute_quietest_bicycle_go_dutch...21primary_fastest_bicycle_ebike -> primary_fastest_bicycle_ebike...23primary_fastest_bicycle_go_dutch -> primary_fastest_bicycle_go_dutch...24primary_fastest_bicycle_ebike -> primary_fastest_bicycle_ebike...25primary_fastest_bicycle_go_dutch -> primary_fastest_bicycle_go_dutch...26primary_quietest_bicycle_ebike -> primary_quietest_bicycle_ebike...28primary_quietest_bicycle_go_dutch -> primary_quietest_bicycle_go_dutch...29primary_quietest_bicycle_ebike -> primary_quietest_bicycle_ebike...30primary_quietest_bicycle_go_dutch -> primary_quietest_bicycle_go_dutch...31secondary_fastest_bicycle_ebike -> secondary_fastest_bicycle_ebike...33secondary_fastest_bicycle_go_dutch -> secondary_fastest_bicycle_go_dutch...34secondary_fastest_bicycle_ebike -> secondary_fastest_bicycle_ebike...35secondary_fastest_bicycle_go_dutch -> secondary_fastest_bicycle_go_dutch...36secondary_quietest_bicycle_ebike -> secondary_quietest_bicycle_ebike...38secondary_quietest_bicycle_go_dutch -> secondary_quietest_bicycle_go_dutch...39secondary_quietest_bicycle_ebike -> secondary_quietest_bicycle_ebike...40secondary_quietest_bicycle_go_dutch -> secondary_quietest_bicycle_go_dutch...41 Joining with by = join_by(identifier) st_as_s2(): dropping Z and/or M coordinate Error in structure(): ! Assigned data value must be compatible with existing data. ✖ Existing data has 5168 rows. ✖ Assigned data has 0 rows. ℹ Only vectors of size 1 are recycled. Caused by error in vectbl_recycle_rhs_rows(): ! Can't recycle input of size 0 to size 5168.

when setting funs as funs = list( all_fastest_bicycle = sum,

all_fastest_bicycle_ebike = sum,

# all_fastest_bicycle_go_dutch = sum, 
all_quietest_bicycle = sum, 
# all_quietest_bicycle_ebike = sum, 
# all_quietest_bicycle_go_dutch = sum, 
commute_fastest_bicycle = sum, 
# commute_fastest_bicycle_ebike = sum, 
# commute_fastest_bicycle_go_dutch = sum, 
commute_quietest_bicycle = sum, 
# commute_quietest_bicycle_ebike = sum, 
# commute_quietest_bicycle_go_dutch = sum, 
primary_fastest_bicycle = sum, 
# primary_fastest_bicycle_ebike = sum, 
# primary_fastest_bicycle_go_dutch = sum, 
primary_quietest_bicycle = sum, 
# primary_quietest_bicycle_ebike = sum, 
# primary_quietest_bicycle_go_dutch = sum, 
secondary_fastest_bicycle = sum, 
# secondary_fastest_bicycle_ebike = sum, 
# secondary_fastest_bicycle_go_dutch = sum, 
secondary_quietest_bicycle = sum, 
# secondary_quietest_bicycle_ebike = sum, 
# secondary_quietest_bicycle_go_dutch = sum, 
Gradient = mean, 
Quietness = mean

) The rnet_merge function is executing successfully, and the uncommented columns in rnet_merged are displaying the expected results. However, an issue persists where the commented-out columns (such as all_fastest_bicycle_ebike, all_quietest_bicycle_ebike, etc.) are appearing in rnet_merged with inaccurate results.

Error stems from code:

  if (identical(fn, sum) && sum_flows) {
    res = rnetj_df %>%
      dplyr::group_by_at(1) %>%
      dplyr::summarise(dplyr::across(dplyr::matches(nm), function(x) sum(x * length_y)))
  } else {
    res = rnetj_df %>%
      dplyr::group_by_at(1) %>%
      dplyr::summarise(dplyr::across(dplyr::matches(nm), fn))
  }
Robinlovelace commented 10 months ago

Hey @wangzhao0217 I'm going to get a cup of tea and then will head up to double team this with you.

wangzhao0217 commented 10 months ago

Hey @wangzhao0217 I'm going to get a cup of tea and then will head up to double team this with you.

Hi Robin, pls have a look at the pull request, Does that make sense to you change dplyr::matches to dplyr::all_of

Robinlovelace commented 10 months ago

Thanks Zhao and great fix!