Skip to content

Commit

Permalink
Fix #173
Browse files Browse the repository at this point in the history
  • Loading branch information
gergness committed Jul 14, 2024
1 parent f092175 commit 1b9505b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion R/lazy_tbl.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 15 additions & 9 deletions tests/testthat/test_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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 %>%
Expand Down

0 comments on commit 1b9505b

Please sign in to comment.