egor icon indicating copy to clipboard operation
egor copied to clipboard

ego_design argument of egor is not lazy-evaluated

Open mbojan opened this issue 6 months ago • 1 comments

At this moment we write in the docs of egor() that

ego_design A list of arguments to srvyr::as_survey_design() specifying the sampling design for the egos. If formulas, they can refer to columns of egos.df. NULL means that no design is set.

alter_design A list of arguments specifying nomination information. Currently, the following elements are supported (...)

while arguments srvyr::as_survey_design() are in fact lazy-evaluated so e.g. accept unquoted variable names. For example for weights:

library(egor)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> Loading required package: tibble

a <- tribble(
  ~egoID, ~alterID,
  1, 1,
  1, 2,
  2, 1,
  2, 2
)

e <- tribble(
  ~egoID, ~ w,
  1, 1,
  2, 2
)


d1 <- srvyr::as_survey_design(e, weights = w) # lazy eval works
d2 <- srvyr::as_survey_design(e, weights = "w") # works too
identical(d1, d2)
#> [1] TRUE

# Works
egor(
  alters = a,
  egos = e,
  ego_design = list(weights = "w")
)
#> # EGO data with survey design (active): 2 × 2
#>   .egoID     w
#> * <chr>  <dbl>
#> 1 1          1
#> 2 2          2
#> # ALTER data: 4 × 2
#>   .altID .egoID
#> * <chr>  <chr> 
#> 1 1      1     
#> 2 2      1     
#> 3 1      2     
#> # ℹ 1 more row
#> # AATIE data: 0 × 3
#> # ℹ 3 variables: .egoID <chr>, .srcID <chr>, .tgtID <chr>

# Not
egor(
  alters = a,
  egos = e,
  ego_design = list(weights = w)
)
#> Error in eval(expr, envir, enclos): object 'w' not found

# Not work because expecting column name, not a vector of weights
wei <- e$w  
egor(
  alters = a,
  egos = e,
  ego_design = list(weights = wei)
)
#> Error in 1/as.matrix(weights): non-numeric argument to binary operator

I guess for now we can get away with more precise docs. In the long run we should probably pass it rlang-style...

mbojan avatar Aug 21 '24 17:08 mbojan