recognizing contexts in which `rlang::exec` fails and how to make it work

Although rlang::exec works without any issues for most functions, I keep coming across some functions where it haphazardly fails, and I am not sure about:

  • how to premeditate these failures
  • how to get it to work in such contexts

For example, here are two functions I have run into in the wild where this happens:

# setup
set.seed(123)
library(WRS2)
library(rlang)
library(metafor)
#> Loading required package: Matrix
#> Loading 'metafor' package (version 2.4-0). For an overview 
#> and introduction to the package please type: help(metafor).

# example-1 -------------------------------------------

dat <- escalc(measure = "RR", ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg)

# works
rma(yi, vi, data = dat)
#> 
#> Random-Effects Model (k = 13; tau^2 estimator: REML)
#> 
#> tau^2 (estimated amount of total heterogeneity): 0.3132 (SE = 0.1664)
#> tau (square root of estimated tau^2 value):      0.5597
#> I^2 (total heterogeneity / total variability):   92.22%
#> H^2 (total variability / sampling variability):  12.86
#> 
#> Test for Heterogeneity:
#> Q(df = 12) = 152.2330, p-val < .0001
#> 
#> Model Results:
#> 
#> estimate      se     zval    pval    ci.lb    ci.ub 
#>  -0.7145  0.1798  -3.9744  <.0001  -1.0669  -0.3622  *** 
#> 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# doesn't work
rlang::exec(.fn = metafor::rma, yi, vi, data = dat)
#> Error in rlang::exec(.fn = metafor::rma, yi, vi, data = dat): object 'yi' not found

# example-2 -------------------------------------------

# works
WRS2::rmmcp(
  y = WineTasting$Taste,
  groups = WineTasting$Wine,
  blocks = WineTasting$Taster
)
#> Call:
#> WRS2::rmmcp(y = WineTasting$Taste, groups = WineTasting$Wine, 
#>     blocks = WineTasting$Taster)
#> 
#>                    psihat ci.lower ci.upper p.value p.crit   sig
#> Wine A vs. Wine B 0.02143 -0.02164  0.06449 0.19500 0.0500 FALSE
#> Wine A vs. Wine C 0.11429  0.02148  0.20710 0.00492 0.0169  TRUE
#> Wine B vs. Wine C 0.08214  0.00891  0.15538 0.00878 0.0250  TRUE

# doesn't work
rlang::exec(
  .fn = WRS2::rmmcp,
  y = WineTasting$Taste,
  groups = WineTasting$Wine,
  blocks = WineTasting$Taster
)
#> Error in names(x) <- value: 'names' attribute [18] must be the same length as the vector [3]

Created on 2021-01-20 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.