The function deparses the supplied function to find only the variables used.

subset_data(func, db, ...)

Arguments

func

A user supplied function

db

A tibble or data.frame

...

Other variables in `db` to keep that are not used in the supplied function. Must be supplied as character strings.

Examples

ll <- function(param) {
  V <- list(
    alt1 = asc_1 + b_1 * variable_1,
    alt2 = asc_2 + b_2 * var_2
  )
}

db <- tibble::tibble(
  variable_1 = runif(10),
  var_2 = runif(10),
  id = seq(1, 10, 1),
  ct = rep(1:2, 5),
  income = sample(100:1000, 10)
)

subset_data(ll, db, c("id", "ct"))
#> # A tibble: 10 × 4
#>    variable_1  var_2    id    ct
#>         <dbl>  <dbl> <dbl> <int>
#>  1     0.569  0.394      1     1
#>  2     0.776  0.0850     2     2
#>  3     0.360  0.889      3     1
#>  4     0.191  0.939      4     2
#>  5     0.949  0.293      5     1
#>  6     0.645  0.335      6     2
#>  7     0.750  0.419      7     1
#>  8     0.673  0.486      8     2
#>  9     0.0377 0.540      9     1
#> 10     0.0711 0.177     10     2
subset_data(ll, db, "id", "ct")
#> # A tibble: 10 × 4
#>    variable_1  var_2    id    ct
#>         <dbl>  <dbl> <dbl> <int>
#>  1     0.569  0.394      1     1
#>  2     0.776  0.0850     2     2
#>  3     0.360  0.889      3     1
#>  4     0.191  0.939      4     2
#>  5     0.949  0.293      5     1
#>  6     0.645  0.335      6     2
#>  7     0.750  0.419      7     1
#>  8     0.673  0.486      8     2
#>  9     0.0377 0.540      9     1
#> 10     0.0711 0.177     10     2
subset_data(ll, db, c("id", "ct"), "income")
#> # A tibble: 10 × 5
#>    variable_1  var_2    id    ct income
#>         <dbl>  <dbl> <dbl> <int>  <int>
#>  1     0.569  0.394      1     1    991
#>  2     0.776  0.0850     2     2    761
#>  3     0.360  0.889      3     1    453
#>  4     0.191  0.939      4     2    299
#>  5     0.949  0.293      5     1    593
#>  6     0.645  0.335      6     2    580
#>  7     0.750  0.419      7     1    399
#>  8     0.673  0.486      8     2    401
#>  9     0.0377 0.540      9     1    646
#> 10     0.0711 0.177     10     2    316