6
6
# '
7
7
# ' @details An `epi_signal` object is simply a tibble, with (at least) the
8
8
# ' following columns (with data types written in tibble notation):
9
- # ' * `value` <dbl>: the value of the signal
9
+ # ' * `value` <dbl> or <list> : the value of the signal
10
10
# ' * `geo_value` <int> or <str>: the associated geographic value
11
11
# ' * `time_value` <date>: the associated time value
12
- # ' * `issue` <date>: the time value at which the given signal value was issued
13
12
# '
14
13
# ' An `epi_signal` object also has a tibble `metadata` stored in its attributes,
15
14
# ' with (at least) the following columns:
16
15
# ' * `name` <str>: the name of the signal
17
16
# ' * `geo_type` <str>: the geographic resolution
18
17
# ' * `time_type` <str>: the temporal resolution
18
+ # ' * `issue` <date>: the time value at which the given data set was issued (this
19
+ # ' represents the maximum of issue dates of individual signal values in the
20
+ # ' data set)
19
21
# ' * `signal_type` <str>: the type of the signal value (optional)
20
22
# ' * `signal_unit` <str>: the units associated with the signal value (optional)
21
23
# '
51
53
# ' @param geo_type The geographic resolution. If missing, then it will be
52
54
# ' guessed from the geo values present.
53
55
# ' @param time_type The temporal resolution. If missing, then it will be guessed
54
- # ' from the time values present.
56
+ # ' from the time values present.
57
+ # ' @param issue Issue date to use for this data. If missing, then today's date
58
+ # ' will be used.
55
59
# ' @param signal_type The type of the signal value.
56
60
# ' @param signal_unit The units of the signal value.
57
- # ' @param issue Issue date to use for this data, if not present in `x`. If no
58
- # ' issue date is present in `x` and `issue` is missing, then today's date will
59
- # ' be used.
60
- # ' @param metadata List or tibble of additional metadata to attach to the
61
+ # ' @param metadata List or tibble of *additional* metadata to attach to the
61
62
# ' `epi_signal` object. All objects will have `geo_type`, `time_type`,
62
63
# ' `signal_type` (optional), and `signal_unit` (optional) entries included in
63
64
# ' their metadata, derived from the above arguments; any entries in the passed
@@ -78,13 +79,13 @@ as.epi_signal.epi_signal = function(x, ...) {
78
79
79
80
# ' @method as.epi_signal tibble
80
81
# ' @describeIn as.epi_signal The input tibble `x` must contain the columns
81
- # ' `value`, `geo_value`, and `time_value`. If an `issue` column is present in
82
- # ' `x`, it will be used as the issue date for each observation; if not, the
83
- # ' `issue` argument will be used. Other columns will be preserved as-is.
82
+ # ' `value`, `geo_value`, and `time_value`. All other columns will be preserved
83
+ # ' as-is.
84
84
# ' @importFrom rlang .data abort
85
85
# ' @export
86
- as.epi_signal.tibble = function (x , name , geo_type , time_type , signal_type ,
87
- signal_unit , issue , metadata = list (), ... ) {
86
+ as.epi_signal.tibble = function (x , name , geo_type , time_type , issue ,
87
+ signal_type , signal_unit , metadata = list (),
88
+ ... ) {
88
89
if (! (" value" %in% names(x ))) {
89
90
abort(paste(
90
91
" `x` must contain a `value` column" ,
@@ -111,7 +112,11 @@ as.epi_signal.tibble = function(x, name, geo_type, time_type, signal_type,
111
112
" `name` must be specified." ,
112
113
class = " epi_coerce_name" )
113
114
}
114
-
115
+
116
+ # If issue is missing, thne use today's date
117
+ if (missing(issue )) issue = Sys.Date()
118
+
119
+ # If geo type is missing ,then try to guess it
115
120
if (missing(geo_type )) {
116
121
if (is.character(x $ geo_value )) {
117
122
# Convert geo values to lowercase
@@ -143,6 +148,7 @@ as.epi_signal.tibble = function(x, name, geo_type, time_type, signal_type,
143
148
else geo_type = " unknown" # TODO should we use NA? Or some other flag?
144
149
}
145
150
151
+ # If time type is missing, then try to guess it
146
152
if (missing(time_type )) {
147
153
# Convert time values to Date format
148
154
x $ time_value = as.Date(x $ time_value )
@@ -156,6 +162,7 @@ as.epi_signal.tibble = function(x, name, geo_type, time_type, signal_type,
156
162
157
163
# Define metadata fields
158
164
metadata $ name = name
165
+ metadata $ issue = issue
159
166
metadata $ geo_type = geo_type
160
167
metadata $ time_type = time_type
161
168
if (! missing(signal_type )) metadata $ signal_type = signal_type
@@ -167,34 +174,19 @@ as.epi_signal.tibble = function(x, name, geo_type, time_type, signal_type,
167
174
class(x ) = c(" epi_signal" , class(x ))
168
175
attributes(x )$ metadata = metadata
169
176
170
- # Reorder columns: value, geo_value, time_value,
177
+ # Reorder columns: value, geo_value, time_value
171
178
x = dplyr :: relocate(x ,
172
179
.data $ value ,
173
180
.data $ geo_value ,
174
181
.data $ time_value )
175
182
176
- # If no rows, then quit
177
- if (nrow(x ) == 0 ) return (x )
178
-
179
- # Add issue column if we need to
180
- if (! (" issue" %in% names(x ))) {
181
- if (missing(issue )) x $ issue = Sys.Date()
182
- else x $ issue = issue
183
- }
184
-
185
- # Reorder columns: issue after time_value
186
- x = dplyr :: relocate(x ,
187
- .data $ issue ,
188
- .after = .data $ time_value )
189
-
190
183
return (x )
191
184
}
192
185
193
186
# ' @method as.epi_signal data.frame
194
187
# ' @describeIn as.epi_signal The input data frame `x` must contain the columns
195
- # ' `value`, `geo_value`, and `time_value`. If an `issue` column is present in
196
- # ' `x`, it will be used as the issue date for each observation; if not, the
197
- # ' `issue` argument will be used. Other columns will be preserved as-is.
188
+ # ' `value`, `geo_value`, and `time_value`. All other columns will be preserved
189
+ # ' as-is.
198
190
# ' @export
199
191
as.epi_signal.data.frame = as.epi_signal.tibble
200
192
0 commit comments