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.424 0.380      1     1
#>  2      0.653 0.630      2     2
#>  3      0.904 0.614      3     1
#>  4      0.426 0.702      4     2
#>  5      0.268 0.493      5     1
#>  6      0.114 0.0726     6     2
#>  7      0.675 0.0822     7     1
#>  8      0.526 0.509      8     2
#>  9      0.121 0.381      9     1
#> 10      0.184 0.158     10     2
subset_data(ll, db, "id", "ct")
#> # A tibble: 10 × 4
#>    variable_1  var_2    id    ct
#>         <dbl>  <dbl> <dbl> <int>
#>  1      0.424 0.380      1     1
#>  2      0.653 0.630      2     2
#>  3      0.904 0.614      3     1
#>  4      0.426 0.702      4     2
#>  5      0.268 0.493      5     1
#>  6      0.114 0.0726     6     2
#>  7      0.675 0.0822     7     1
#>  8      0.526 0.509      8     2
#>  9      0.121 0.381      9     1
#> 10      0.184 0.158     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.424 0.380      1     1    772
#>  2      0.653 0.630      2     2    764
#>  3      0.904 0.614      3     1    385
#>  4      0.426 0.702      4     2    956
#>  5      0.268 0.493      5     1    215
#>  6      0.114 0.0726     6     2    955
#>  7      0.675 0.0822     7     1    861
#>  8      0.526 0.509      8     2    112
#>  9      0.121 0.381      9     1    278
#> 10      0.184 0.158     10     2    528