From 551efef5dc971a3f4c4c0ce76a7e18cef478fdea Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 3 Aug 2022 14:51:58 -0700 Subject: [PATCH 1/3] Addressed issue #104 from epipredict & simplified code --- R/methods-epi_df.R | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/R/methods-epi_df.R b/R/methods-epi_df.R index 454e067c..55f346b0 100644 --- a/R/methods-epi_df.R +++ b/R/methods-epi_df.R @@ -63,59 +63,32 @@ summary.epi_df = function(object, ...) { dplyr::summarize(mean(.data$num))))) } -#' @export `[.epi_df` <- function(x, i, j, drop = FALSE) { res <- NextMethod() if (!is.data.frame(res)) return(res) - i_arg <- substitute(i) - j_arg <- substitute(j) - if (missing(i)) { i <- NULL i_arg <- NULL - } else if (is.null(i)) { - i <- integer() - } + } if (missing(j)) { j <- NULL j_arg <- NULL - } else if (is.null(j)) { - j <- integer() - } - - # Ignore drop as an argument for counting - n_real_args <- nargs() - !missing(drop) - - # Case when the number of args (excluding drop) is not 3 or more - if (n_real_args <= 2L) { - j <- i - i <- NULL - j_arg <- i_arg - i_arg <- NULL - } + } cn <- names(res) nr <- vctrs::vec_size(x) not_epi_df <- (!("time_value" %in% cn) || !("geo_value" %in% cn) || vctrs::vec_size(res) > nr || any(i > nr)) - - if (not_epi_df) return(tibble::as_tibble(res)) - # Case when i is numeric and there are duplicate values in it - if (is.numeric(i) && vctrs::vec_duplicate_any(i) > 0) - return(tibble::as_tibble(res)) + if (not_epi_df) return(tibble::as_tibble(res)) - att_x = attr(x, "metadata") - new_epi_df(tibble::as_tibble(res), - geo_type = att_x$geo_type, - time_type = att_x$time_type, - as_of = att_x$as_of, - additional_metadata = - att_x[!(names(att_x) %in% c("geo_type", "time_type", "as_of"))]) + # Use reclass as safeguard (in case class & metadata are dropped) + reclass(res, attr(x, "metadata")) } + #' `dplyr` verbs #' #' `dplyr` verbs for `epi_df` objects, preserving class and attributes. From a508f6864d18841f2418414bd6b4321b5aaa3cd8 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 3 Aug 2022 14:52:38 -0700 Subject: [PATCH 2/3] Removed extra line --- R/methods-epi_df.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/methods-epi_df.R b/R/methods-epi_df.R index 55f346b0..2bbfdf54 100644 --- a/R/methods-epi_df.R +++ b/R/methods-epi_df.R @@ -88,7 +88,6 @@ summary.epi_df = function(object, ...) { reclass(res, attr(x, "metadata")) } - #' `dplyr` verbs #' #' `dplyr` verbs for `epi_df` objects, preserving class and attributes. From d1b23ce2da144b6dfd63941932071ee17f98ad16 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 3 Aug 2022 15:15:21 -0700 Subject: [PATCH 3/3] devtools::document() --- R/methods-epi_df.R | 1 + man/as_epi_archive.Rd | 12 ++++-------- man/as_epi_df.Rd | 10 +++++----- man/epi_archive.Rd | 42 +++++++++++++++++++++--------------------- man/epi_slide.Rd | 12 ++++-------- man/epix_as_of.Rd | 12 ++++-------- man/epix_slide.Rd | 12 ++++-------- 7 files changed, 43 insertions(+), 58 deletions(-) diff --git a/R/methods-epi_df.R b/R/methods-epi_df.R index 2bbfdf54..b0f7207d 100644 --- a/R/methods-epi_df.R +++ b/R/methods-epi_df.R @@ -63,6 +63,7 @@ summary.epi_df = function(object, ...) { dplyr::summarize(mean(.data$num))))) } +#' @export `[.epi_df` <- function(x, i, j, drop = FALSE) { res <- NextMethod() diff --git a/man/as_epi_archive.Rd b/man/as_epi_archive.Rd index a98798cc..d13ba4d6 100644 --- a/man/as_epi_archive.Rd +++ b/man/as_epi_archive.Rd @@ -91,15 +91,11 @@ examples. } \details{ This simply a wrapper around the \code{new()} method of the \code{epi_archive} -class, so for example: - -\if{html}{\out{
}}\preformatted{x <- as_epi_archive(df, geo_type = "state", time_type = "day") -}\if{html}{\out{
}} - -would be equivalent to: +class, so for example:\preformatted{x <- as_epi_archive(df, geo_type = "state", time_type = "day") +} -\if{html}{\out{
}}\preformatted{x <- epi_archive$new(df, geo_type = "state", time_type = "day") -}\if{html}{\out{
}} +would be equivalent to:\preformatted{x <- epi_archive$new(df, geo_type = "state", time_type = "day") +} } \examples{ # Simple ex. with necessary keys diff --git a/man/as_epi_df.Rd b/man/as_epi_df.Rd index b5df1302..5d1b1335 100644 --- a/man/as_epi_df.Rd +++ b/man/as_epi_df.Rd @@ -51,9 +51,9 @@ examples. } \section{Methods (by class)}{ \itemize{ -\item \code{as_epi_df(epi_df)}: Simply returns the \code{epi_df} object unchanged. +\item \code{epi_df}: Simply returns the \code{epi_df} object unchanged. -\item \code{as_epi_df(tbl_df)}: The input tibble \code{x} must contain the columns +\item \code{tbl_df}: The input tibble \code{x} must contain the columns \code{geo_value} and \code{time_value}. All other columns will be preserved as is, and treated as measured variables. If \code{as_of} is missing, then the function will try to guess it from an \code{as_of}, \code{issue}, or \code{version} column of \code{x} @@ -61,14 +61,14 @@ will try to guess it from an \code{as_of}, \code{issue}, or \code{version} colum (stored in its attributes); if this fails, then the current day-time will be used. -\item \code{as_epi_df(data.frame)}: Works analogously to \code{as_epi_df.tbl_df()}. +\item \code{data.frame}: Works analogously to \code{as_epi_df.tbl_df()}. -\item \code{as_epi_df(tbl_ts)}: Works analogously to \code{as_epi_df.tbl_df()}, except that +\item \code{tbl_ts}: Works analogously to \code{as_epi_df.tbl_df()}, except that the \code{tbl_ts} class is dropped, and any key variables (other than "geo_value") are added to the metadata of the returned object, under the \code{other_keys} field. - }} + \examples{ # Convert a `tsibble` that has county code as an extra key # Notice that county code should be a character string to preserve any leading zeroes diff --git a/man/epi_archive.Rd b/man/epi_archive.Rd index 026f27e1..0b198eab 100644 --- a/man/epi_archive.Rd +++ b/man/epi_archive.Rd @@ -114,18 +114,18 @@ toy_epi_archive \section{Methods}{ \subsection{Public methods}{ \itemize{ -\item \href{#method-epi_archive-new}{\code{epi_archive$new()}} -\item \href{#method-epi_archive-print}{\code{epi_archive$print()}} -\item \href{#method-epi_archive-as_of}{\code{epi_archive$as_of()}} -\item \href{#method-epi_archive-fill_through_version}{\code{epi_archive$fill_through_version()}} -\item \href{#method-epi_archive-merge}{\code{epi_archive$merge()}} -\item \href{#method-epi_archive-slide}{\code{epi_archive$slide()}} -\item \href{#method-epi_archive-clone}{\code{epi_archive$clone()}} +\item \href{#method-new}{\code{epi_archive$new()}} +\item \href{#method-print}{\code{epi_archive$print()}} +\item \href{#method-as_of}{\code{epi_archive$as_of()}} +\item \href{#method-fill_through_version}{\code{epi_archive$fill_through_version()}} +\item \href{#method-merge}{\code{epi_archive$merge()}} +\item \href{#method-slide}{\code{epi_archive$slide()}} +\item \href{#method-clone}{\code{epi_archive$clone()}} } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-epi_archive-new}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-new}{}}} \subsection{Method \code{new()}}{ Creates a new \code{epi_archive} object. \subsection{Usage}{ @@ -195,8 +195,8 @@ An \code{epi_archive} object. } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-epi_archive-print}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-print}{}}} \subsection{Method \code{print()}}{ \subsection{Usage}{ \if{html}{\out{
}}\preformatted{epi_archive$print()}\if{html}{\out{
}} @@ -204,8 +204,8 @@ An \code{epi_archive} object. } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-epi_archive-as_of}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-as_of}{}}} \subsection{Method \code{as_of()}}{ Generates a snapshot in \code{epi_df} format as of a given version. See the documentation for the wrapper function \code{\link[=epix_as_of]{epix_as_of()}} for details. @@ -215,8 +215,8 @@ See the documentation for the wrapper function \code{\link[=epix_as_of]{epix_as_ } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-epi_archive-fill_through_version}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-fill_through_version}{}}} \subsection{Method \code{fill_through_version()}}{ Fill in unobserved history using requested scheme by mutating \code{self} and potentially reseating its fields. See @@ -237,8 +237,8 @@ version, which doesn't mutate the input archive but might alias its fields. } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-epi_archive-merge}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-merge}{}}} \subsection{Method \code{merge()}}{ Merges another \code{epi_archive} with the current one, mutating the current one by reseating its \code{DT} and several other fields, but avoiding @@ -267,8 +267,8 @@ does not alias either archive's \code{DT}. } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-epi_archive-slide}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-slide}{}}} \subsection{Method \code{slide()}}{ Slides a given function over variables in an \code{epi_archive} object. See the documentation for the wrapper function \code{\link[=epix_slide]{epix_slide()}} for @@ -290,8 +290,8 @@ details. } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-epi_archive-clone}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-clone}{}}} \subsection{Method \code{clone()}}{ The objects of this class are cloneable with this method. \subsection{Usage}{ diff --git a/man/epi_slide.Rd b/man/epi_slide.Rd index 1b620b69..c64bbce1 100644 --- a/man/epi_slide.Rd +++ b/man/epi_slide.Rd @@ -107,16 +107,12 @@ incomplete windows) is therefore left up to the user, either through the specified function or formula \code{f}, or through post-processing. If \code{f} is missing, then an expression for tidy evaluation can be specified, -for example, as in: - -\if{html}{\out{
}}\preformatted{epi_slide(x, cases_7dav = mean(cases), n = 7) -}\if{html}{\out{
}} - -which would be equivalent to: +for example, as in:\preformatted{epi_slide(x, cases_7dav = mean(cases), n = 7) +} -\if{html}{\out{
}}\preformatted{epi_slide(x, function(x, ...) mean(x$cases), n = 7, +which would be equivalent to:\preformatted{epi_slide(x, function(x, ...) mean(x$cases), n = 7, new_col_name = "cases_7dav") -}\if{html}{\out{
}} +} Thus, to be clear, when the computation is specified via an expression for tidy evaluation (first example, above), then the name for the new column is diff --git a/man/epix_as_of.Rd b/man/epix_as_of.Rd index 4053cd28..6dc72a44 100644 --- a/man/epix_as_of.Rd +++ b/man/epix_as_of.Rd @@ -29,15 +29,11 @@ examples. } \details{ This is simply a wrapper around the \code{as_of()} method of the -\code{epi_archive} class, so if \code{x} is an \code{epi_archive} object, then: - -\if{html}{\out{
}}\preformatted{epix_as_of(x, max_version = v) -}\if{html}{\out{
}} - -is equivalent to: +\code{epi_archive} class, so if \code{x} is an \code{epi_archive} object, then:\preformatted{epix_as_of(x, max_version = v) +} -\if{html}{\out{
}}\preformatted{x$as_of(max_version = v) -}\if{html}{\out{
}} +is equivalent to:\preformatted{x$as_of(max_version = v) +} } \examples{ # warning message of data latency shown diff --git a/man/epix_slide.Rd b/man/epix_slide.Rd index 79e9c1c3..2acae1a1 100644 --- a/man/epix_slide.Rd +++ b/man/epix_slide.Rd @@ -115,15 +115,11 @@ should never be used in place of \code{epi_slide()}, and only used when version-aware sliding is necessary (as it its purpose). Finally, this is simply a wrapper around the \code{slide()} method of the -\code{epi_archive} class, so if \code{x} is an \code{epi_archive} object, then: - -\if{html}{\out{
}}\preformatted{epix_slide(x, new_var = comp(old_var), n = 120) -}\if{html}{\out{
}} - -is equivalent to: +\code{epi_archive} class, so if \code{x} is an \code{epi_archive} object, then:\preformatted{epix_slide(x, new_var = comp(old_var), n = 120) +} -\if{html}{\out{
}}\preformatted{x$slide(x, new_var = comp(old_var), n = 120) -}\if{html}{\out{
}} +is equivalent to:\preformatted{x$slide(x, new_var = comp(old_var), n = 120) +} } \examples{ # these dates are reference time points for the 3 day average sliding window