PHEindicatormethods icon indicating copy to clipboard operation
PHEindicatormethods copied to clipboard

Update phe_dsr so that it will work inside a function

Open Olivia-Box-Power opened this issue 2 years ago • 0 comments

Would it be possible to update phe_dsr so that it can be used within a function, with field names passed using {{ }}?

library("PHEindicatormethods")
library("tidyverse")




df <- data.frame(indicatorid = rep(c(1234, 5678, 91011, 121314), each = 19 * 2 * 5),
                 year = rep(2006:2010, each = 19 * 2),
                 sex = rep(rep(c("Male", "Female"), each = 19), 5),
                 ageband = rep(c(0,5,10,15,20,25,30,35,40,45,
                                 50,55,60,65,70,75,80,85,90), times = 10),
                 obs = sample(200, 19 * 2 * 5 * 4, replace = TRUE),
                 pop = sample(10000:20000, 19 * 2 * 5 * 4, replace = TRUE),
                 esp = rep(esp2013, times = 10))



phe_dsr_2 <- function(data,
                      count,
                      denominator,
                      ref_pop) {

data %>%
  group_by(indicatorid, year, sex) %>%
  phe_dsr(x = {{ count }},
          n = {{ denominator }},
          stdpop =  {{ ref_pop }},
          stdpoptype = "field")

}

phe_dsr_2(df, count =  obs, denominator = pop, ref_pop = esp)
#> Warning in if (deparse(substitute(stdpop)) %in% colnames(data)) {: the condition
#> has length > 1 and only the first element will be used
#> Error in phe_dsr(., x = {: stdpop is not a field name from data

Created on 2021-09-30 by the reprex package (v2.0.1)

Olivia-Box-Power avatar Sep 30 '21 10:09 Olivia-Box-Power