diff --git a/NEWS.md b/NEWS.md index eed9d83..0591529 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ * Improvements to printing (#166, thanks @szimmer) * Fix for situations where filtering a calibrated/PPS survey design when filtering 0 rows (#159, @bschneidr) * Fix for anonymous functions in `rename_with()` (#172, thanks @josiahg2) +* Fix for chaining filter & group_by in database backed surveys (#173, thanks @jamgreen) # srvyr 1.2.0 * `survey_prop()` now uses proportions as the default, which should confidence interval improve coverage, but does mean results may slightly change (#141, #142, thanks @szimmer) diff --git a/R/lazy_tbl.r b/R/lazy_tbl.r index d80780e..c43ee48 100644 --- a/R/lazy_tbl.r +++ b/R/lazy_tbl.r @@ -61,8 +61,9 @@ localize_lazy_svy <- function(svy, dots = NULL) { dplyr::select(svy$variables, !!!rlang::syms(vars_to_collect)) ) + class(svy) <- setdiff(class(svy), "tbl_lazy_svy") if (needs_subset) { - svy <- subset_svy_vars(svy, as.logical(svy$variables$`__SRVYR_SUBSET_VAR__`)) + svy <- srvyr::filter(svy, as.logical(`__SRVYR_SUBSET_VAR__`)) } svy } diff --git a/tests/testthat/test_database.R b/tests/testthat/test_database.R index fbb7ab4..15c6a8f 100644 --- a/tests/testthat/test_database.R +++ b/tests/testthat/test_database.R @@ -13,6 +13,7 @@ if (suppressPackageStartupMessages(require(dbplyr))) { # replace its successor # has_monetdb <- suppressWarnings(suppressPackageStartupMessages(require(MonetDBLite))) data(api) + data(scd, package = "survey") # dbs_to_run <- c("RSQLite", "MonetDBLite") dbs_to_run <- "RSQLite" @@ -25,15 +26,7 @@ if (suppressPackageStartupMessages(require(dbplyr))) { db_avail <- TRUE } else if (db == "RSQLite" && !has_rsqlite){ db_avail <- FALSE - } #else if (db == "MonetDBLite" && has_monetdb) { - # con <- DBI::dbConnect(MonetDBLite::MonetDBLite(), path = ":memory:") - # cleaned <- dplyr::select(apistrat, -full) - # names(cleaned) <- gsub("\\.", "", names(cleaned)) - # apistrat_db <- copy_to(con, cleaned) - # db_avail <- TRUE - # } else if (db == "MonetDBLite" && !has_monetdb) { - # db_avail <- FALSE - # } + } test_that(paste0("DB backed survey tests - ", db), { skip_if_not(db_avail) @@ -86,6 +79,19 @@ if (suppressPackageStartupMessages(require(dbplyr))) { summarize(api99 = survey_mean(api99)) ) + # Can filter then group_by and summarize + expect_df_equal( + suppressWarnings(dstrata %>% + filter(stype == "E") %>% + group_by(both) %>% + summarize(api99 = survey_mean(api99))), + local_dstrata %>% + filter(stype == "E") %>% + group_by(both) %>% + summarize(api99 = survey_mean(api99)) %>% + mutate(both = as.character(both)) + ) + # Can mutate and summarize expect_df_equal( suppressWarnings(dstrata %>%