Skip to content

Commit 219c7a0

Browse files
committed
Fix/improve group_by, ungroup usage in most vignettes
* Add missing `group_by` before some `epix_slide`s that previously relied on a nontrivial default grouping, fixing vignette regression. * Be more consistent about `ungroup`ing after grouped operations that don't automatically ungroup. Try not to introduce regressions in (uncommon) code that takes a grouped object from an earlier computation and performs manipulations that actually do rely on it being grouped.
1 parent 86c7dab commit 219c7a0

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

vignettes/advanced.Rmd

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ df %>%
6262
df %>%
6363
mutate(version = time_value) %>%
6464
as_epi_archive() %>%
65-
epix_slide(x_2dav = mean(x), before = 1, ref_time_values = as.Date("2020-06-02"))
65+
group_by(geo_value) %>%
66+
epix_slide(x_2dav = mean(x), before = 1, ref_time_values = as.Date("2020-06-02")) %>%
67+
ungroup()
6668
6769
df %>%
6870
mutate(version = time_value) %>%
6971
as_epi_archive() %>%
70-
epix_slide(~ mean(.x$x), before = 1, ref_time_values = as.Date("2020-06-02"))
72+
group_by(geo_value) %>%
73+
epix_slide(~ mean(.x$x), before = 1, ref_time_values = as.Date("2020-06-02")) %>%
74+
ungroup()
7175
```
7276

7377
When the slide computation returns an atomic vector (rather than a single value)
@@ -102,7 +106,8 @@ object returned by `epi_slide()` has a list column containing the slide values.
102106
df2 <- df %>%
103107
group_by(geo_value) %>%
104108
epi_slide(a = data.frame(x_2dav = mean(x), x_2dma = mad(x)),
105-
before = 1, as_list_col = TRUE)
109+
before = 1, as_list_col = TRUE) %>%
110+
ungroup()
106111
107112
class(df2$a)
108113
length(df2$a)
@@ -120,7 +125,8 @@ slide computation (here `x_2dav` and `x_2dma`) separated by "_".
120125
df %>%
121126
group_by(geo_value) %>%
122127
epi_slide(a = data.frame(x_2dav = mean(x), x_2dma = mad(x)),
123-
before = 1, as_list_col = FALSE)
128+
before = 1, as_list_col = FALSE) %>%
129+
ungroup()
124130
```
125131

126132
We can use `names_sep = NULL` (which gets passed to `tidyr::unnest()`) to drop
@@ -130,7 +136,8 @@ the prefix associated with list column name, in naming the unnested columns.
130136
df %>%
131137
group_by(geo_value) %>%
132138
epi_slide(a = data.frame(x_2dav = mean(x), x_2dma = mad(x)),
133-
before = 1, as_list_col = FALSE, names_sep = NULL)
139+
before = 1, as_list_col = FALSE, names_sep = NULL) %>%
140+
ungroup()
134141
```
135142

136143
Furthermore, `epi_slide()` will recycle the single row data frame as needed in
@@ -152,9 +159,11 @@ df %>%
152159
df %>%
153160
mutate(version = time_value) %>%
154161
as_epi_archive() %>%
162+
group_by(geo_value) %>%
155163
epix_slide(a = data.frame(x_2dav = mean(x), x_2dma = mad(x)),
156164
ref_time_values = as.Date("2020-06-02"),
157-
before = 1, as_list_col = FALSE, names_sep = NULL)
165+
before = 1, as_list_col = FALSE, names_sep = NULL) %>%
166+
ungroup()
158167
```
159168

160169
## Multi-row outputs

vignettes/aggregation.Rmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ xt %>%
190190
as_epi_df() %>%
191191
group_by(geo_value) %>%
192192
epi_slide(cases_7dav = mean(cases), before = 6) %>%
193+
ungroup() %>%
193194
filter(geo_value == "Plymouth, MA",
194195
abs(time_value - as.Date("2021-07-01")) <= 3) %>%
195196
print(n = 7)
@@ -198,6 +199,7 @@ xt_filled %>%
198199
as_epi_df() %>%
199200
group_by(geo_value) %>%
200201
epi_slide(cases_7dav = mean(cases), before = 6) %>%
202+
ungroup() %>%
201203
filter(geo_value == "Plymouth, MA",
202204
abs(time_value - as.Date("2021-07-01")) <= 3) %>%
203205
print(n = 7)

vignettes/outliers.Rmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ x <- x %>%
117117
x = time_value, y = cases,
118118
methods = detection_methods,
119119
combiner = "median")) %>%
120+
ungroup() %>%
120121
unnest(outlier_info)
121122
122123
head(x)

0 commit comments

Comments
 (0)