Skip to content

Commit ff68c45

Browse files
authored
Merge pull request #232 from cmu-delphi/ndefries/class-print
Add print methods for rest of custom classes
2 parents 6e9f899 + 1b2f1bb commit ff68c45

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
S3method(as_tibble,covidcast_data_signal_list)
44
S3method(as_tibble,covidcast_data_source_list)
5+
S3method(print,EpiRange)
6+
S3method(print,EpidataFieldInfo)
57
S3method(print,covidcast_data_signal)
68
S3method(print,covidcast_data_signal_list)
79
S3method(print,covidcast_data_source)
810
S3method(print,covidcast_epidata)
911
S3method(print,epidata_call)
12+
S3method(print,fetch_args)
1013
export(avail_endpoints)
1114
export(cache_info)
1215
export(clear_cache)

R/epidatacall.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ request_arguments <- function(epidata_call, format_type, fields = NULL) {
121121

122122
#' @export
123123
print.epidata_call <- function(x, ...) {
124-
stopifnot(inherits(x, "epidata_call"))
125124
cli::cli_h1("<epidata_call> object:")
126125
cli::cli_bullets(c(
127126
"*" = "Pipe this object into `fetch()` to actually fetch the data",
@@ -198,6 +197,13 @@ fetch_args_list <- function(
198197
)
199198
}
200199

200+
#' @export
201+
print.fetch_args <- function(x, ...) {
202+
cli::cli_h1("<fetch_args> object:")
203+
# Print all non-class fields.
204+
cli::cli_dl(x[attr(x, "names")])
205+
}
206+
201207
#' Fetches the data
202208
#'
203209
#' @details

R/model.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ epirange <- function(from, to) {
6161
structure(list(from = from, to = to), class = "EpiRange")
6262
}
6363

64+
#' @export
65+
print.EpiRange <- function(x, ...) {
66+
if (nchar(x$from) == 8) {
67+
date_type <- "Days" # nolint: object_usage_linter
68+
x$from <- as.Date(as.character(x$from), "%Y%m%d")
69+
x$to <- as.Date(as.character(x$to), "%Y%m%d")
70+
} else if (nchar(x$from) == 6) {
71+
date_type <- "Epiweeks" # nolint: object_usage_linter
72+
x$from <- paste0(
73+
substr(x$from, 1, 4), "w", substr(x$from, 5, 6)
74+
)
75+
x$to <- paste0(
76+
substr(x$to, 1, 4), "w", substr(x$to, 5, 6)
77+
)
78+
}
79+
80+
cli::cli_h1("<EpiRange> object:")
81+
cli::cli_bullets(
82+
"{date_type} from {x$from} to {x$to}"
83+
)
84+
}
6485

6586
#' Timeset formats for specifying dates
6687
#'
@@ -86,7 +107,6 @@ epirange <- function(from, to) {
86107
#' @name timeset
87108
NULL
88109

89-
90110
create_epidata_field_info <- function(name,
91111
type,
92112
description = "",
@@ -117,6 +137,13 @@ create_epidata_field_info <- function(name,
117137
)
118138
}
119139

140+
#' @export
141+
print.EpidataFieldInfo <- function(x, ...) {
142+
cli::cli_h1("<EpidataFieldInfo> object:")
143+
# Print all non-class fields.
144+
cli::cli_dl(x[attr(x, "names")])
145+
}
146+
120147
parse_value <- function(info, value, disable_date_parsing = FALSE) {
121148
stopifnot(inherits(info, "EpidataFieldInfo"))
122149

0 commit comments

Comments
 (0)