Skip to content

Commit

Permalink
lint: increase lintr happiness
Browse files Browse the repository at this point in the history
  • Loading branch information
dshemetov committed Mar 7, 2024
1 parent 68c027b commit 0c8144e
Show file tree
Hide file tree
Showing 31 changed files with 443 additions and 284 deletions.
13 changes: 7 additions & 6 deletions R/archive.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# We use special features of data.table's `[`. The data.table package has a
# compatibility feature that disables some/all of these features if it thinks we
# might expect `data.frame`-compatible behavior instead. We can signal that we
# want the special behavior via `.datatable.aware = TRUE` or by importing any
# want the special behavior via `.datatable_aware = TRUE` or by importing any
# `data.table` package member. Do both to prevent surprises if we decide to use
# `data.table::` everywhere and not importing things.
.datatable.aware <- TRUE
.datatable_aware <- TRUE

#' Validate a version bound arg
#'
Expand Down Expand Up @@ -79,6 +79,7 @@ max_version_with_row_in <- function(x) {
version_bound <- max(version_col)
}
}
version_bound
}

#' Get the next possible value greater than `x` of the same type
Expand Down Expand Up @@ -343,7 +344,7 @@ epi_archive <-
# then the call to as.data.table() will fail to set keys, so we
# need to check this, then do it manually if needed
key_vars <- c("geo_value", "time_value", other_keys, "version")
DT <- as.data.table(x, key = key_vars)
DT <- as.data.table(x, key = key_vars) # nolint: object_name_linter
if (!identical(key_vars, key(DT))) setkeyv(DT, cols = key_vars)

maybe_first_duplicate_key_row_index <- anyDuplicated(DT, by = key(DT))
Expand Down Expand Up @@ -381,7 +382,7 @@ epi_archive <-
# Runs compactify on data frame
if (is.null(compactify) || compactify == TRUE) {
elim <- keep_locf(DT)
DT <- rm_locf(DT)
DT <- rm_locf(DT) # nolint: object_name_linter
} else {
# Create empty data frame for nrow(elim) to be 0
elim <- tibble::tibble()
Expand Down Expand Up @@ -543,7 +544,7 @@ epi_archive <-
validate_version_bound(fill_versions_end, self$DT, na_ok = FALSE)
how <- arg_match(how)
if (self$versions_end < fill_versions_end) {
new_DT <- switch(how,
new_DT <- switch(how, # nolint: object_name_linter
"na" = {
# old DT + a version consisting of all NA observations
# immediately after the last currently/actually-observed
Expand All @@ -567,7 +568,7 @@ epi_archive <-
if (identical(address(self$DT), address(nonversion_key_vals_ever_recorded))) {
nonversion_key_vals_ever_recorded <- copy(nonversion_key_vals_ever_recorded)
}
next_version_DT <- nonversion_key_vals_ever_recorded[
next_version_DT <- nonversion_key_vals_ever_recorded[ # nolint: object_name_linter
, version := next_version_tag
][
# this makes the class of these columns logical (`NA` is a
Expand Down
10 changes: 5 additions & 5 deletions R/autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ autoplot.epi_df <- function(
dplyr::mutate(
.colours = switch(.color_by,
all_keys = interaction(!!!all_keys, sep = "/"),
geo_value = geo_value,
geo_value = .data$geo_value,
other_keys = interaction(!!!other_keys, sep = "/"),
all = interaction(!!!all_avail, sep = "/"),
NULL
),
.facets = switch(.facet_by,
all_keys = interaction(!!!all_keys, sep = "/"),
geo_value = as.factor(geo_value),
geo_value = as.factor(.data$geo_value),
other_keys = interaction(!!!other_keys, sep = "/"),
all = interaction(!!!all_avail, sep = "/"),
NULL
Expand All @@ -130,10 +130,10 @@ autoplot.epi_df <- function(
n_facets <- nlevels(object$.facets)
if (n_facets > .max_facets) {
top_n <- levels(as.factor(object$.facets))[seq_len(.max_facets)]
object <- dplyr::filter(object, .facets %in% top_n) %>%
dplyr::mutate(.facets = droplevels(.facets))
object <- dplyr::filter(object, .data$.facets %in% top_n) %>%
dplyr::mutate(.facets = droplevels(.data$.facets))
if (".colours" %in% names(object)) {
object <- dplyr::mutate(object, .colours = droplevels(.colours))
object <- dplyr::mutate(object, .colours = droplevels(.data$.colours))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion R/correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#' cor_by = geo_value,
#' dt1 = -2
#' )
epi_cor <- function(x, var1, var2, dt1 = 0, dt2 = 0, shift_by = geo_value,
epi_cor <- function(x, var1, var2, dt1 = 0, dt2 = 0, shift_by = geo_value, # nolint: object_usage_linter
cor_by = geo_value, use = "na.or.complete",
method = c("pearson", "kendall", "spearman")) {
assert_class(x, "epi_df")
Expand Down
105 changes: 74 additions & 31 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
#' COVID-19 cases, daily}
#' }
#' @source This object contains a modified part of the
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University}
#' as \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished in the COVIDcast Epidata API}.
#' This data set is licensed under the terms of the
#' \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons Attribution 4.0 International license}
#' by the Johns Hopkins University on behalf of its Center for Systems Science
#' in Engineering. Copyright Johns Hopkins University 2020.
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository
#' by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins
#' University} as
#' \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished
#' in the COVIDcast Epidata API}. This data set is licensed under the terms of
#' the \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons
#' Attribution 4.0 International license} by the Johns Hopkins University on
#' behalf of its Center for Systems Science in Engineering. Copyright Johns
#' Hopkins University 2020.
#'
#' Modifications:
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From
Expand Down Expand Up @@ -54,19 +57,34 @@
#' \item{geo_value}{the geographic value associated with each row of measurements.}
#' \item{time_value}{the time value associated with each row of measurements.}
#' \item{version}{the time value specifying the version for each row of measurements. }
#' \item{percent_cli}{percentage of doctor’s visits with CLI (COVID-like illness) computed from medical insurance claims}
#' \item{case_rate_7d_av}{7-day average signal of number of new confirmed deaths due to COVID-19 per 100,000 population, daily}
#' \item{percent_cli}{percentage of doctor’s visits with CLI (COVID-like
#' illness) computed from medical insurance claims}
#' \item{case_rate_7d_av}{7-day average signal of number of new confirmed
#' deaths due to COVID-19 per 100,000 population, daily}
#' }
#' @source
#' This object contains a modified part of the \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University} as \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished in the COVIDcast Epidata API}. This data set is licensed under the terms of the
#' \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons Attribution 4.0 International license}
#' by Johns Hopkins University on behalf of its Center for Systems Science in Engineering.
#' Copyright Johns Hopkins University 2020.
#' This object contains a modified part of the
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by
#' the Center for Systems Science and Engineering (CSSE) at Johns Hopkins
#' University} as
#' \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished
#' in the COVIDcast Epidata API}. This data set is licensed under the terms of
#' the \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons
#' Attribution 4.0 International license} by Johns Hopkins University on behalf
#' of its Center for Systems Science in Engineering. Copyright Johns Hopkins
#' University 2020.
#'
#' Modifications:
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/doctor-visits.html}{From the COVIDcast Doctor Visits API}: The signal `percent_cli` is taken directly from the API without changes.
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From the COVIDcast Epidata API}: `case_rate_7d_av` signal was computed by Delphi from the original JHU-CSSE data by calculating moving averages of the preceding 7 days, so the signal for June 7 is the average of the underlying data for June 1 through 7, inclusive.
#' * Furthermore, the data is a subset of the full dataset, the signal names slightly altered, and formatted into a tibble.
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/doctor-visits.html}{From
#' the COVIDcast Doctor Visits API}: The signal `percent_cli` is taken
#' directly from the API without changes.
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From
#' the COVIDcast Epidata API}: `case_rate_7d_av` signal was computed by Delphi
#' from the original JHU-CSSE data by calculating moving averages of the
#' preceding 7 days, so the signal for June 7 is the average of the underlying
#' data for June 1 through 7, inclusive.
#' * Furthermore, the data is a subset of the full dataset, the signal names
#' slightly altered, and formatted into a tibble.
#'
#' @export
"archive_cases_dv_subset"
Expand Down Expand Up @@ -128,19 +146,19 @@ some_package_is_being_unregistered <- function(parent_n = 0L) {
#'
#' @noRd
delayed_assign_with_unregister_awareness <- function(x, value,
eval.env = rlang::caller_env(),
assign.env = rlang::caller_env()) {
value_quosure <- rlang::as_quosure(rlang::enexpr(value), eval.env)
eval_env = rlang::caller_env(),
assign_env = rlang::caller_env()) {
value_quosure <- rlang::as_quosure(rlang::enexpr(value), eval_env)
this_env <- environment()
delayedAssign(x, eval.env = this_env, assign.env = assign.env, value = {
delayedAssign(x, eval.env = this_env, assign.env = assign_env, value = {
if (some_package_is_being_unregistered()) {
withCallingHandlers(
# `rlang::eval_tidy(value_quosure)` is shorter and would sort of work,
# but doesn't give the same `ls`, `rm`, and top-level `<-` behavior as
# we'd have with `delayedAssign`; it doesn't seem to actually evaluate
# quosure's expr in the quosure's env. Using `rlang::eval_bare` instead
# seems to do the trick. (We also could have just used a `value_expr`
# and `eval.env` together rather than introducing `value_quosure` at
# and `eval_env` together rather than introducing `value_quosure` at
# all.)
rlang::eval_bare(rlang::quo_get_expr(value_quosure), rlang::quo_get_env(value_quosure)),
error = function(err) {
Expand Down Expand Up @@ -193,7 +211,10 @@ delayed_assign_with_unregister_awareness <- function(x, value,
# binding may have been created with the same name as the package promise, and
# this binding will stick around even when the package is reloaded, and will
# need to be `rm`-d to easily access the refreshed package promise.
delayed_assign_with_unregister_awareness("archive_cases_dv_subset", as_epi_archive(archive_cases_dv_subset_dt, compactify = FALSE))
delayed_assign_with_unregister_awareness(
"archive_cases_dv_subset",
as_epi_archive(archive_cases_dv_subset_dt, compactify = FALSE)
)

#' Subset of JHU daily cases from California and Florida
#'
Expand All @@ -210,15 +231,24 @@ delayed_assign_with_unregister_awareness("archive_cases_dv_subset", as_epi_archi
#' \item{time_value}{the time value associated with each row of measurements.}
#' \item{cases}{Number of new confirmed COVID-19 cases, daily}
#' }
#' @source This object contains a modified part of the \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University} as \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished in the COVIDcast Epidata API}. This data set is licensed under the terms of the
#' \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons Attribution 4.0 International license}
#' by the Johns Hopkins University on behalf of its Center for Systems Science in Engineering.
#' Copyright Johns Hopkins University 2020.
#' @source This object contains a modified part of the
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by
#' the Center for Systems Science and Engineering (CSSE) at Johns Hopkins
#' University} as
#' \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished
#' in the COVIDcast Epidata API}. This data set is licensed under the terms of
#' the \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons
#' Attribution 4.0 International license} by the Johns Hopkins University on
#' behalf of its Center for Systems Science in Engineering. Copyright Johns
#' Hopkins University 2020.
#'
#' Modifications:
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From the COVIDcast Epidata API}:
#' These signals are taken directly from the JHU CSSE \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 GitHub repository} without changes.
#' * Furthermore, the data has been limited to a very small number of rows, the signal names slightly altered, and formatted into a tibble.
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From
#' the COVIDcast Epidata API}: These signals are taken directly from the JHU
#' CSSE \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 GitHub
#' repository} without changes.
#' * Furthermore, the data has been limited to a very small number of rows, the
#' signal names slightly altered, and formatted into a tibble.
"incidence_num_outlier_example"

#' Subset of JHU daily cases from counties in Massachusetts and Vermont
Expand All @@ -237,12 +267,25 @@ delayed_assign_with_unregister_awareness("archive_cases_dv_subset", as_epi_archi
#' \item{county_name}{the name of the county}
#' \item{state_name}{the full name of the state}
#' }
#' @source This object contains a modified part of the \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University} as \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished in the COVIDcast Epidata API}. This data set is licensed under the terms of the
#' @source This object contains a modified part of the
#' \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 Data Repository by
#' the Center for Systems Science and Engineering (CSSE) at Johns Hopkins
#' University} as
#' \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{republished
#' in the COVIDcast Epidata API}. This data set is licensed under the terms of
#' the
#' \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons Attribution 4.0 International license}
#' by the Johns Hopkins University on behalf of its Center for Systems Science in Engineering.
#' Copyright Johns Hopkins University 2020.
#'
#' Modifications:
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From the COVIDcast Epidata API}: These signals are taken directly from the JHU CSSE \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 GitHub repository} without changes. The 7-day average signals are computed by Delphi by calculating moving averages of the preceding 7 days, so the signal for June 7 is the average of the underlying data for June 1 through 7, inclusive.
#' * Furthermore, the data has been limited to a very small number of rows, the signal names slightly altered, and formatted into a tibble.
#' * \href{https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html}{From
#' the COVIDcast Epidata API}: These signals are taken directly from the JHU
#' CSSE \href{https://github.com/CSSEGISandData/COVID-19}{COVID-19 GitHub
#' repository} without changes. The 7-day average signals are computed by
#' Delphi by calculating moving averages of the preceding 7 days, so the
#' signal for June 7 is the average of the underlying data for June 1 through
#' 7, inclusive.
#' * Furthermore, the data has been limited to a very small number of rows, the
#' signal names slightly altered, and formatted into a tibble.
"jhu_csse_county_level_subset"
16 changes: 8 additions & 8 deletions R/epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,20 @@ new_epi_df <- function(x = tibble::tibble(), geo_type, time_type, as_of,
# If as_of is missing, then try to guess it
if (missing(as_of)) {
# First check the metadata for an as_of field
if ("metadata" %in% names(attributes(x)) &&
"as_of" %in% names(attributes(x)$metadata)) {
if (
"metadata" %in% names(attributes(x)) &&
"as_of" %in% names(attributes(x)$metadata)
) {
as_of <- attributes(x)$metadata$as_of
}

# Next check for as_of, issue, or version columns
else if ("as_of" %in% names(x)) {
} else if ("as_of" %in% names(x)) {
# Next check for as_of, issue, or version columns
as_of <- max(x$as_of)
} else if ("issue" %in% names(x)) {
as_of <- max(x$issue)
} else if ("version" %in% names(x)) {
as_of <- max(x$version)
} # If we got here then we failed
else {
} else {
# If we got here then we failed
as_of <- Sys.time()
} # Use the current day-time
}
Expand Down
Loading

0 comments on commit 0c8144e

Please sign in to comment.