Skip to content

Make epi_slide_opt work with !!varname, maybe !!varnames, maybe !!!varnames #562

@brookslogan

Description

@brookslogan
library(epiprocess)
#> Registered S3 method overwritten by 'tsibble':
#>   method               from 
#>   as_tibble.grouped_df dplyr
#> 
#> Attaching package: 'epiprocess'
#> The following object is masked from 'package:stats':
#> 
#>     filter
varname <- "case_rate"
varnames <- c("case_rate", "death_rate")
covid_case_death_rates_extended %>% epi_slide_sum(!!varname, .window_size=7)
#> Error in !varname: invalid argument type
covid_case_death_rates_extended %>% epi_slide_sum(!!varnames, .window_size=7)
#> Error in !varnames: invalid argument type
covid_case_death_rates_extended %>% epi_slide_sum(!!varnames, .window_size=7)
#> Error in !varnames: invalid argument type

Created on 2024-11-03 with reprex v2.1.1

We probably don't need the if branch here and can probably just use the else branch code in any situation.

  if (inherits(quo_get_expr(enquo(.col_names)), "character")) {
    pos <- eval_select(dplyr::all_of(.col_names), data = .x, allow_rename = FALSE)
  } else {
    pos <- eval_select(enquo(.col_names), data = .x, allow_rename = FALSE)
  }
  • !!varnames probably will work, but I'm not sure if this is intended.
  • !!! may or may not work; it may be more "proper" for it not to work, as it looks like splatting multiple arguments into the function, while our .col_names is just a single arg slot. (If we look at the similar across function, it will balk with a confusing message if we attempt to use !!!, because it is actually splatting multiple fn args. But I don't think we have fn arg splatting here so it might actually work in our case.)

Workaround is to use manually use all_of(varname) or all_of(varnames) instead of something with !! or !!!.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions