egor icon indicating copy to clipboard operation
egor copied to clipboard

egor() goes astray if alters tibble has a column `x`

Open mbojan opened this issue 7 months ago • 0 comments

Two identical egor() calls differing in naming the attribute:

library(egor)

# Attribute is named "a"
egor(
  egos = tribble(
    ~ egoID, ~ a,
    1, 0,
    2, 0
  ),
  alters = tribble(
    ~ egoID, ~ alterID, ~ a,
    1, 1, 0,
    1, 2, 1,
    2, 1, 0,
    2, 2, 0
  ),
  aaties = tribble(
    ~ egoID, ~ Source, ~ Target,
    1, 1, 2,
    2, 1, 2
  ),
  ID.vars = list(
    ego = "egoID",
    alter = "alterID",
    source = "Source",
    target = "Target"
  )
)
#> # EGO data (active): 2 × 2
#>   .egoID     a
#> *  <dbl> <dbl>
#> 1      1     0
#> 2      2     0
#> # ALTER data: 4 × 3
#>   .altID .egoID     a
#> *  <dbl>  <dbl> <dbl>
#> 1      1      1     0
#> 2      2      1     1
#> 3      1      2     0
#> # ℹ 1 more row
#> # AATIE data: 2 × 3
#>   .egoID .srcID .tgtID
#> *  <dbl>  <dbl>  <dbl>
#> 1      1      1      2
#> 2      2      1      2


# Alter attribute is named "x"
egor(
  egos = tribble(
    ~ egoID, ~ x,
    1, 0,
    2, 0
  ),
  alters = tribble(
    ~ egoID, ~ alterID, ~ x,
    1, 1, 0,
    1, 2, 1,
    2, 1, 0,
    2, 2, 0
  ),
  aaties = tribble(
    ~ egoID, ~ Source, ~ Target,
    1, 1, 2,
    2, 1, 2
  ),
  ID.vars = list(
    ego = "egoID",
    alter = "alterID",
    source = "Source",
    target = "Target"
  )
)
#> Error: There is at least one alter referenced in the `alter-alter` data that is not listed in the `alter` data. Errors were found for egos: 1 2

It's a double edge of tidy/lazy evaluation... Evaluation of an expression such as function(x) filter(alters, !!e == x) evaluates x to alters$x if present rather than the function argument. This happens here:

https://github.com/tilltnet/egor/blob/44d87a00c3fbff8a06bbd3f1ccb337fa0eb07f24/R/egor.R#L201-L210

mbojan avatar Jul 18 '24 15:07 mbojan