Skip to content

Commit 62e496d

Browse files
committed
tidyselect fix
1 parent f0b1339 commit 62e496d

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ importFrom(dplyr,if_else)
148148
importFrom(dplyr,lag)
149149
importFrom(dplyr,mutate)
150150
importFrom(dplyr,near)
151+
importFrom(dplyr,pick)
151152
importFrom(dplyr,pull)
152153
importFrom(dplyr,relocate)
153154
importFrom(dplyr,rename)

R/revision_analysis.R

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#' @export
5555
#' @importFrom cli cli_inform cli_abort cli_li
5656
#' @importFrom rlang list2 syms
57-
#' @importFrom dplyr mutate group_by arrange filter if_any all_of across pull
57+
#' @importFrom dplyr mutate group_by arrange filter if_any all_of across pull pick
5858
#' everything ungroup summarize if_else %>%
5959
revision_summary <- function(epi_arch,
6060
...,
@@ -66,10 +66,10 @@ revision_summary <- function(epi_arch,
6666
rel_spread_threshold = 0.1,
6767
abs_spread_threshold = NULL,
6868
compactify_tol = .Machine$double.eps^0.5) {
69-
arg <- enquos(...)
69+
arg <- names(eval_select(rlang::expr(c(...)), allow_rename = FALSE, data = epi_arch$DT))
7070
if (length(arg) == 0) {
7171
first_non_key <- !(names(epi_arch$DT) %in% c(key_colnames(epi_arch), "version"))
72-
arg <- syms(names(epi_arch$DT)[first_non_key][1])
72+
arg <- names(epi_arch$DT)[first_non_key][1]
7373
} else if (length(arg) > 1) {
7474
cli_abort("Not currently implementing more than one column at a time. Run each separately")
7575
}
@@ -90,27 +90,28 @@ revision_summary <- function(epi_arch,
9090

9191
revision_behavior <-
9292
epi_arch$DT %>%
93-
select(c(geo_value, time_value, all_of(keys), version, !!arg[[1]]))
93+
select(c(geo_value, time_value, all_of(keys), version, !!arg))
9494
if (drop_nas) {
9595
# if we're dropping NA's, we should recompactify
9696
revision_behavior <-
9797
revision_behavior %>%
98-
filter(!is.na(!!arg[[1]])) %>%
98+
filter(!is.na(c_across(!!arg))) %>%
9999
arrange(across(c(geo_value, time_value, all_of(keys), version))) %>% # need to sort before compactifying
100100
compactify_tibble(c(keys, version), compactify_tol)
101101
} else {
102102
revision_behavior <- epi_arch$DT
103103
}
104-
revision_behavior <- revision_behavior %>%
104+
revision_behavior <-
105+
revision_behavior %>%
105106
mutate(lag = as.integer(version) - as.integer(time_value)) %>% # nolint: object_usage_linter
106107
group_by(across(all_of(keys))) %>% # group by all the keys
107108
summarize(
108109
n_revisions = dplyr::n() - 1,
109110
min_lag = min(lag), # nolint: object_usage_linter
110111
max_lag = max(lag), # nolint: object_usage_linter
111-
spread = spread_vec(!!arg[[1]]),
112-
rel_spread = (spread) / max_no_na(!!arg[[1]]), # nolint: object_usage_linter
113-
time_to = time_within_x_latest(lag, !!arg[[1]], prop = within_latest), # nolint: object_usage_linter
112+
spread = spread_vec(pick(!!arg)),
113+
rel_spread = spread / max_no_na(pick(!!arg)), # nolint: object_usage_linter
114+
time_to = time_within_x_latest(lag, pick(!!arg), prop = within_latest), # nolint: object_usage_linter
114115
.groups = "drop"
115116
) %>%
116117
mutate(
@@ -125,7 +126,9 @@ revision_summary <- function(epi_arch,
125126
cli_inform("Min lag (time to first version):")
126127
difftime_summary(revision_behavior$min_lag) %>% print()
127128
if (!drop_nas) {
128-
total_na <- nrow(epi_arch$DT %>% filter(is.na(!!arg[[1]]))) # nolint: object_usage_linter
129+
total_na <- epi_arch$DT %>%
130+
filter(is.na(c_across(!!arg))) %>% # nolint: object_usage_linter
131+
nrow()
129132
cli_inform("Fraction of all versions that are `NA`:")
130133
cli_li(num_percent(total_na, nrow(epi_arch$DT)))
131134
}
@@ -171,9 +174,12 @@ revision_summary <- function(epi_arch,
171174
return(revision_behavior)
172175
}
173176

177+
#' pull the value from lags when values starts indefinitely being within prop of it's last value.
178+
#' @param values this should be a 1 column tibble. errors may occur otherwise
174179
#' @keywords internal
175180
time_within_x_latest <- function(lags, values, prop = .2) {
176-
latest_value <- values[length(values)]
181+
values <- values[[1]]
182+
latest_value <- values[[length(values)]]
177183
close_enough <- abs(values - latest_value) < prop * latest_value
178184
# we want to ignore any stretches where it's close, but goes farther away later
179185
return(get_last_run(close_enough, lags))

man/time_within_x_latest.Rd

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-revision-latency-functions.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ test_that("revision_summary works for a dummy dataset", {
3636
})
3737
test_that("tidyselect is functional", {
3838
expect_no_error(revision_summary(dummy_ex, value))
39+
expect_no_error(revision_summary(dummy_ex, starts_with("val")))
3940
})
4041
test_that("revision_summary works for various timetypes", {})

0 commit comments

Comments
 (0)