Skip to content

Commit f16eb60

Browse files
authored
Merge pull request #378 from cmu-delphi/ds/style
style: run package through styler
2 parents 71e11f7 + c02d201 commit f16eb60

30 files changed

+3534
-2868
lines changed

.git-blame-ignore-revs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c65876078a6f9525952b305eaea2fca003adf907

R/archive.R

+578-516
Large diffs are not rendered by default.

R/correlation.R

+62-47
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#' grouping by geo value, time value, or any other variables. See the
55
#' [correlation
66
#' vignette](https://cmu-delphi.github.io/epiprocess/articles/correlation.html)
7-
#' for examples.
7+
#' for examples.
88
#'
99
#' @param x The `epi_df` object under consideration.
1010
#' @param var1,var2 The variables in `x` to correlate.
11-
#' @param dt1,dt2 Time shifts to consider for the two variables, respectively,
11+
#' @param dt1,dt2 Time shifts to consider for the two variables, respectively,
1212
#' before computing correlations. Negative shifts translate into in a lag
1313
#' value and positive shifts into a lead value; for example, if `dt = -1`,
1414
#' then the new value on June 2 is the original value on June 1; if `dt = 1`,
@@ -34,51 +34,59 @@
3434
#' `method` (same as `cor()`).
3535
#'
3636
#' @return An tibble with the grouping columns first (`geo_value`, `time_value`,
37-
#' or possibly others), and then a column `cor`, which gives the correlation.
38-
#'
37+
#' or possibly others), and then a column `cor`, which gives the correlation.
38+
#'
3939
#' @importFrom stats cor
4040
#' @importFrom rlang .data !! !!! enquo syms
4141
#' @importFrom tidyselect eval_select
4242
#' @export
4343
#' @examples
44-
#'
44+
#'
4545
#' # linear association of case and death rates on any given day
46-
#' epi_cor(x = jhu_csse_daily_subset,
47-
#' var1 = case_rate_7d_av,
48-
#' var2 = death_rate_7d_av,
49-
#' cor_by = "time_value")
50-
#'
46+
#' epi_cor(
47+
#' x = jhu_csse_daily_subset,
48+
#' var1 = case_rate_7d_av,
49+
#' var2 = death_rate_7d_av,
50+
#' cor_by = "time_value"
51+
#' )
52+
#'
5153
#' # correlation of death rates and lagged case rates
52-
#' epi_cor(x = jhu_csse_daily_subset,
53-
#' var1 = case_rate_7d_av,
54-
#' var2 = death_rate_7d_av,
55-
#' cor_by = time_value,
56-
#' dt1 = -2)
57-
#'
58-
#' # correlation grouped by location
59-
#' epi_cor(x = jhu_csse_daily_subset,
60-
#' var1 = case_rate_7d_av,
61-
#' var2 = death_rate_7d_av,
62-
#' cor_by = geo_value)
63-
#'
54+
#' epi_cor(
55+
#' x = jhu_csse_daily_subset,
56+
#' var1 = case_rate_7d_av,
57+
#' var2 = death_rate_7d_av,
58+
#' cor_by = time_value,
59+
#' dt1 = -2
60+
#' )
61+
#'
62+
#' # correlation grouped by location
63+
#' epi_cor(
64+
#' x = jhu_csse_daily_subset,
65+
#' var1 = case_rate_7d_av,
66+
#' var2 = death_rate_7d_av,
67+
#' cor_by = geo_value
68+
#' )
69+
#'
6470
#' # correlation grouped by location and incorporates lagged cases rates
65-
#' epi_cor(x = jhu_csse_daily_subset,
66-
#' var1 = case_rate_7d_av,
67-
#' var2 = death_rate_7d_av,
68-
#' cor_by = geo_value,
69-
#' dt1 = -2)
70-
epi_cor = function(x, var1, var2, dt1 = 0, dt2 = 0, shift_by = geo_value,
71-
cor_by = geo_value, use = "na.or.complete",
72-
method = c("pearson", "kendall", "spearman")) {
71+
#' epi_cor(
72+
#' x = jhu_csse_daily_subset,
73+
#' var1 = case_rate_7d_av,
74+
#' var2 = death_rate_7d_av,
75+
#' cor_by = geo_value,
76+
#' dt1 = -2
77+
#' )
78+
epi_cor <- function(x, var1, var2, dt1 = 0, dt2 = 0, shift_by = geo_value,
79+
cor_by = geo_value, use = "na.or.complete",
80+
method = c("pearson", "kendall", "spearman")) {
7381
# Check we have an `epi_df` object
7482
if (!inherits(x, "epi_df")) Abort("`x` must be of class `epi_df`.")
7583

7684
# Check that we have variables to do computations on
7785
if (missing(var1)) Abort("`var1` must be specified.")
7886
if (missing(var2)) Abort("`var2` must be specified.")
79-
var1 = enquo(var1)
80-
var2 = enquo(var2)
81-
87+
var1 <- enquo(var1)
88+
var2 <- enquo(var2)
89+
8290
# Defuse grouping variables. This looks a bit more involved since we want to
8391
# accomodate the option of specifying multiple variables for each grouping.
8492
# Hence use the power of tidyselect::eval_select(), which can accomodate any
@@ -88,26 +96,33 @@ epi_cor = function(x, var1, var2, dt1 = 0, dt2 = 0, shift_by = geo_value,
8896
# * cor_by = c(a, b)
8997
# * cor_by = c("a", "b")
9098
# and so on, and similarly for shift_by. Note: make sure to follow with !!!
91-
cor_by = syms(names(eval_select(enquo(cor_by), x)))
92-
shift_by = syms(names(eval_select(enquo(shift_by), x)))
99+
cor_by <- syms(names(eval_select(enquo(cor_by), x)))
100+
shift_by <- syms(names(eval_select(enquo(shift_by), x)))
93101

94102
# Which method?
95-
method = match.arg(method)
103+
method <- match.arg(method)
96104

97105
# Perform time shifts, then compute appropriate correlations and return
98106
return(x %>%
99-
dplyr::group_by(!!!shift_by) %>%
100-
dplyr::arrange(.data$time_value) %>%
101-
dplyr::mutate(var1 = shift(!!var1, n = dt1),
102-
var2 = shift(!!var2, n = dt2)) %>%
103-
dplyr::ungroup() %>%
104-
dplyr::group_by(!!!cor_by) %>%
105-
dplyr::summarize(cor = cor(x = .data$var1, y = .data$var2,
106-
use = use, method = method)))
107+
dplyr::group_by(!!!shift_by) %>%
108+
dplyr::arrange(.data$time_value) %>%
109+
dplyr::mutate(
110+
var1 = shift(!!var1, n = dt1),
111+
var2 = shift(!!var2, n = dt2)
112+
) %>%
113+
dplyr::ungroup() %>%
114+
dplyr::group_by(!!!cor_by) %>%
115+
dplyr::summarize(cor = cor(
116+
x = .data$var1, y = .data$var2,
117+
use = use, method = method
118+
)))
107119
}
108120

109121
# Function to perform time shifts, lag or lead
110-
shift = function(var, n) {
111-
if (n < 0) return(dplyr::lag(var, -n))
112-
else return(dplyr::lead(var, n))
122+
shift <- function(var, n) {
123+
if (n < 0) {
124+
return(dplyr::lag(var, -n))
125+
} else {
126+
return(dplyr::lead(var, n))
127+
}
113128
}

R/data.R

+53-49
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
#' Subset of JHU daily state cases and deaths
1+
#' Subset of JHU daily state cases and deaths
22
#'
33
#' This data source of confirmed COVID-19 cases and deaths
44
#' is based on reports made available by the Center for
55
#' Systems Science and Engineering at Johns Hopkins University.
6-
#' This example data ranges from Mar 1, 2020 to Dec 31, 2021, and is limited to
6+
#' This example data ranges from Mar 1, 2020 to Dec 31, 2021, and is limited to
77
#' California, Florida, Texas, New York, Georgia, and Pennsylvania.
88
#'
99
#' @format A tibble with 4026 rows and 6 variables:
1010
#' \describe{
11-
#' \item{geo_value}{the geographic value associated with each row
11+
#' \item{geo_value}{the geographic value associated with each row
1212
#' of measurements.}
1313
#' \item{time_value}{the time value associated with each row of measurements.}
14-
#' \item{case_rate_7d_av}{7-day average signal of number of new
14+
#' \item{case_rate_7d_av}{7-day average signal of number of new
1515
#' confirmed COVID-19 cases per 100,000 population, daily}
16-
#' \item{death_rate_7d_av}{7-day average signal of number of new confirmed
16+
#' \item{death_rate_7d_av}{7-day average signal of number of new confirmed
1717
#' deaths due to COVID-19 per 100,000 population, daily}
1818
#' \item{cases}{Number of new confirmed COVID-19 cases, daily}
19-
#' \item{cases_7d_av}{7-day average signal of number of new confirmed
19+
#' \item{cases_7d_av}{7-day average signal of number of new confirmed
2020
#' COVID-19 cases, daily}
2121
#' }
22-
#' @source This object contains a modified part of the
22+
#' @source This object contains a modified part of the
2323
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University}
24-
#' as \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished in the COVIDcast Epidata API}.
24+
#' as \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished in the COVIDcast Epidata API}.
2525
#' This data set is licensed under the terms of the
2626
#' \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons Attribution 4.0 International license}
2727
#' by the Johns Hopkins University on behalf of its Center for Systems Science
2828
#' in Engineering. Copyright Johns Hopkins University 2020.
2929
#'
3030
#' Modifications:
31-
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From the COVIDcast Epidata API}:
32-
#' These signals are taken directly from the JHU CSSE
33-
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 GitHub repository}
34-
#' without changes. The 7-day average signals are computed by Delphi by
35-
#' calculating moving averages of the preceding 7 days, so the signal for
36-
#' June 7 is the average of the underlying data for June 1 through 7,
31+
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From the COVIDcast Epidata API}:
32+
#' These signals are taken directly from the JHU CSSE
33+
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 GitHub repository}
34+
#' without changes. The 7-day average signals are computed by Delphi by
35+
#' calculating moving averages of the preceding 7 days, so the signal for
36+
#' June 7 is the average of the underlying data for June 1 through 7,
3737
#' inclusive.
38-
#' * Furthermore, the data has been limited to a very small number of rows,
38+
#' * Furthermore, the data has been limited to a very small number of rows,
3939
#' the signal names slightly altered, and formatted into a tibble.
4040
"jhu_csse_daily_subset"
4141

@@ -46,7 +46,7 @@
4646
#' provided to us by health system partners, and also contains confirmed
4747
#' COVID-19 cases based on reports made available by the Center for
4848
#' Systems Science and Engineering at Johns Hopkins University.
49-
#' This example data ranges from June 1, 2020 to Dec 1, 2021, and
49+
#' This example data ranges from June 1, 2020 to Dec 1, 2021, and
5050
#' is also limited to California, Florida, Texas, and New York.
5151
#'
5252
#' @format An `epi_archive` data format. The data table DT has 129,638 rows and 5 columns:
@@ -88,23 +88,23 @@
8888
#' @return Boolean
8989
#'
9090
#' @noRd
91-
some_package_is_being_unregistered = function(parent_n = 0L) {
92-
calls = sys.calls()
91+
some_package_is_being_unregistered <- function(parent_n = 0L) {
92+
calls <- sys.calls()
9393
# `calls` will include the call to this function; strip out this call plus
9494
# `parent_n` additional requested calls to make it like we're reasoning about
9595
# the desired call. This could prevent potential false positives from
9696
# triggering if, in a later version, we decide to loosen the `call_name`
9797
# checks below to something that would be `TRUE` for the name of this function
9898
# or one of the undesired call ancestors.
99-
calls_to_inspect = utils::head(calls, n = -(parent_n + 1L))
99+
calls_to_inspect <- utils::head(calls, n = -(parent_n + 1L))
100100
# Note that `utils::head(sys.calls(), n=-1L)` isn't equivalent, due to lazy
101101
# argument evaluation. Note that copy-pasting the body of this function
102102
# without this `utils::head` operation isn't always equivalent to calling it;
103103
# e.g., within the `value` argument of a package-level `delayedAssign`,
104104
# `sys.calls()` will return `NULL` is some or all cases, including when its
105105
# evaluation has been triggered via `unregister`.
106-
simple_call_names = purrr::map_chr(calls_to_inspect, function(call) {
107-
maybe_simple_call_name = rlang::call_name(call)
106+
simple_call_names <- purrr::map_chr(calls_to_inspect, function(call) {
107+
maybe_simple_call_name <- rlang::call_name(call)
108108
if (is.null(maybe_simple_call_name)) NA_character_ else maybe_simple_call_name
109109
})
110110
# `pkgload::unregister` is an (the?) exported function that forces
@@ -127,11 +127,11 @@ some_package_is_being_unregistered = function(parent_n = 0L) {
127127
#' different than when using `delayedAssign` directly.
128128
#'
129129
#' @noRd
130-
delayed_assign_with_unregister_awareness = function(x, value,
131-
eval.env = rlang::caller_env(),
132-
assign.env = rlang::caller_env()) {
133-
value_quosure = rlang::as_quosure(rlang::enexpr(value), eval.env)
134-
this_env = environment()
130+
delayed_assign_with_unregister_awareness <- function(x, value,
131+
eval.env = rlang::caller_env(),
132+
assign.env = rlang::caller_env()) {
133+
value_quosure <- rlang::as_quosure(rlang::enexpr(value), eval.env)
134+
this_env <- environment()
135135
delayedAssign(x, eval.env = this_env, assign.env = assign.env, value = {
136136
if (some_package_is_being_unregistered()) {
137137
withCallingHandlers(
@@ -144,26 +144,30 @@ delayed_assign_with_unregister_awareness = function(x, value,
144144
# all.)
145145
rlang::eval_bare(rlang::quo_get_expr(value_quosure), rlang::quo_get_env(value_quosure)),
146146
error = function(err) {
147-
Abort(paste("An error was raised while attempting to evaluate a promise",
148-
"(prepared with `delayed_assign_with_unregister_awareness`)",
149-
"while an `unregister` or `unregister_namespace` call",
150-
"was being evaluated.",
151-
"This can happen, for example, when `devtools::load_all`",
152-
"reloads a package that contains a buggy promise,",
153-
"because reloading can cause old package-level promises to",
154-
"be forced via `pkgload::unregister` and",
155-
"`pkgload:::unregister_namespace`, due to",
156-
"https://github.com/r-lib/pkgload/pull/157.",
157-
"If this is the current situation, you might be able to",
158-
"be successfully reload the package again after",
159-
"`unloadNamespace`-ing it (but this situation will",
160-
"keep re-occurring every other `devtools::load`",
161-
"and every `devtools:document` until the bug or situation",
162-
"generating the promise's error has been resolved)."
163-
),
164-
class = "epiprocess__promise_evaluation_error_during_unregister",
165-
parent = err)
166-
})
147+
Abort(
148+
paste(
149+
"An error was raised while attempting to evaluate a promise",
150+
"(prepared with `delayed_assign_with_unregister_awareness`)",
151+
"while an `unregister` or `unregister_namespace` call",
152+
"was being evaluated.",
153+
"This can happen, for example, when `devtools::load_all`",
154+
"reloads a package that contains a buggy promise,",
155+
"because reloading can cause old package-level promises to",
156+
"be forced via `pkgload::unregister` and",
157+
"`pkgload:::unregister_namespace`, due to",
158+
"https://github.com/r-lib/pkgload/pull/157.",
159+
"If this is the current situation, you might be able to",
160+
"be successfully reload the package again after",
161+
"`unloadNamespace`-ing it (but this situation will",
162+
"keep re-occurring every other `devtools::load`",
163+
"and every `devtools:document` until the bug or situation",
164+
"generating the promise's error has been resolved)."
165+
),
166+
class = "epiprocess__promise_evaluation_error_during_unregister",
167+
parent = err
168+
)
169+
}
170+
)
167171
} else {
168172
rlang::eval_bare(rlang::quo_get_expr(value_quosure), rlang::quo_get_env(value_quosure))
169173
}
@@ -189,14 +193,14 @@ delayed_assign_with_unregister_awareness = function(x, value,
189193
# binding may have been created with the same name as the package promise, and
190194
# this binding will stick around even when the package is reloaded, and will
191195
# need to be `rm`-d to easily access the refreshed package promise.
192-
delayed_assign_with_unregister_awareness("archive_cases_dv_subset", as_epi_archive(archive_cases_dv_subset_dt, compactify=FALSE))
196+
delayed_assign_with_unregister_awareness("archive_cases_dv_subset", as_epi_archive(archive_cases_dv_subset_dt, compactify = FALSE))
193197

194198
#' Subset of JHU daily cases from California and Florida
195199
#'
196200
#' This data source of confirmed COVID-19 cases
197201
#' is based on reports made available by the Center for
198202
#' Systems Science and Engineering at Johns Hopkins University.
199-
#' This example data is a snapshot as of Oct 28, 2021 and captures the cases
203+
#' This example data is a snapshot as of Oct 28, 2021 and captures the cases
200204
#' from June 1, 2020 to May 31, 2021
201205
#' and is limited to California and Florida.
202206
#'
@@ -222,7 +226,7 @@ delayed_assign_with_unregister_awareness("archive_cases_dv_subset", as_epi_archi
222226
#' This data source of confirmed COVID-19 cases and deaths
223227
#' is based on reports made available by the Center for
224228
#' Systems Science and Engineering at Johns Hopkins University.
225-
#' This example data ranges from Mar 1, 2020 to Dec 31, 2021,
229+
#' This example data ranges from Mar 1, 2020 to Dec 31, 2021,
226230
#' and is limited to Massachusetts and Vermont.
227231
#'
228232
#' @format A tibble with 16,212 rows and 5 variables:

0 commit comments

Comments
 (0)