rigraph icon indicating copy to clipboard operation
rigraph copied to clipboard

Edge sequence indexing

Open hecking opened this issue 10 years ago • 4 comments

Hello, today I discovered a strange behaviour when iterating over an edge sequence. My example code:

sapply(1:length(E(g)), function(i) {

  print(E(g)[1]) # This works as expected
  print(E(g)[i]) # Here I get an Error in eval(expr, envir, enclos) : object 'X' not found

})

I know that there have been some changes in the handling of edge and vertex sequences with the latest igraph version. However, this behaviour seems to be a bit strange.

hecking avatar Mar 04 '16 07:03 hecking

Traceback corresponding to the error:

> traceback()
10: eval(x$expr, data, x$env)
9: eval(x$expr, data, x$env)
8: FUN(X[[i]], ...)
7: lapply(x, lazy_eval, data = data)
6: lazy_eval(args, data = c(attrs, .inc = .inc, inc = inc, adj = adj,
       .from = .from, from = from, .to = .to, to = to, .igraph.from = list(.Call(C_R_igraph_mybracket,
           graph, 3L)[as.numeric(x)]), .igraph.to = list(.Call(C_R_igraph_mybracket,
           graph, 4L)[as.numeric(x)]), .igraph.graph = list(graph),
       `%--%` = `%--%`, `%->%` = `%->%`, `%<-%` = `%<-%`, .env = env,
       .data = list(attrs)))
5: `[.igraph.es`(E(g), i) at #1
4: E(g)[i] at #1
3: FUN(X[[i]], ...)
2: lapply(X = X, FUN = FUN, ...)
1: sapply(1:length(E(g)), function(i) {
       E(g)[i]
   })

ntamas avatar Oct 29 '22 19:10 ntamas

🤯

library(igraph, warn.conflicts = FALSE)

g <- make_ring(2)

lapply(1, function(i) {
  E(g)[1]
})
#> [[1]]
#> + 1/1 edge from e0777c8:
#> [1] 1--2

lapply(1, function(i) {
  E(g)[i]
})
#> Error in eval(x$expr, data, x$env): object 'X' not found

purrr::map(1, function(i) {
  E(g)[i]
})
#> Error in `purrr::map()`:
#> ℹ In index: 1.
#> Caused by error:
#> ! object '.x' not found
#> Backtrace:
#>      ▆
#>   1. ├─purrr::map(...)
#>   2. │ └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>   3. │   ├─purrr:::with_indexed_errors(...)
#>   4. │   │ └─base::withCallingHandlers(...)
#>   5. │   ├─purrr:::call_with_cleanup(...)
#>   6. │   └─global .f(.x[[i]], ...)
#>   7. │     ├─E(g)[i]
#>   8. │     └─igraph:::`[.igraph.es`(E(g), i)
#>   9. │       └─igraph:::lazy_eval(...)
#>  10. │         └─base::lapply(x, lazy_eval, data = data)
#>  11. │           └─igraph (local) FUN(X[[i]], ...)
#>  12. │             └─base::eval(x$expr, data, x$env)
#>  13. │               └─base::eval(x$expr, data, x$env)
#>  14. └─base::.handleSimpleError(...)
#>  15.   └─purrr (local) h(simpleError(msg, call))
#>  16.     └─cli::cli_abort(...)
#>  17.       └─rlang::abort(...)

Created on 2023-03-31 with reprex v2.0.2

krlmlr avatar Mar 31 '23 17:03 krlmlr

Also:

library(igraph, warn.conflicts = FALSE)

g <- make_ring(2)

fun <- function(i) {
  E(g)[i]
}

fun(1)
#> + 1/1 edge from 1691242:
#> [1] 1--2

Created on 2023-03-31 with reprex v2.0.2

I wonder what's specific about the mapping functions that makes the other examples fail.

krlmlr avatar Mar 31 '23 17:03 krlmlr

It's likely related to #233, too.

krlmlr avatar Mar 31 '23 17:03 krlmlr