@@ -50,27 +50,18 @@ get_forecast_date <- function(new_data, info, epi_keys_checked, latency, columns
5050 )
5151 }
5252 }
53+ max_time <- get_max_time(new_data , epi_keys_checked , columns )
5354 # the source data determines the actual time_values
54- # these are the non-na time_values;
55- # get the minimum value across the checked epi_keys' maximum time values
56- max_time <- new_data %> %
57- select(all_of(columns )) %> %
58- drop_na()
59- # null and "" don't work in `group_by`
60- if (! is.null(epi_keys_checked ) && all(epi_keys_checked != " " )) {
61- max_time <- max_time %> % group_by(across(all_of(epi_keys_checked )))
62- }
63- max_time <- max_time %> %
64- summarise(time_value = max(time_value )) %> %
65- pull(time_value ) %> %
66- min()
6755 if (is.null(latency )) {
6856 forecast_date <- attributes(new_data )$ metadata $ as_of
6957 } else {
58+ if (is.null(max_time )) {
59+ cli_abort(" max_time is null. This likely means there is one of {columns} that is all `NA`" )
60+ }
7061 forecast_date <- max_time + latency
7162 }
7263 # make sure the as_of is sane
73- if (! inherits(forecast_date , class(max_time )) & ! inherits(forecast_date , " POSIXt" )) {
64+ if (! inherits(forecast_date , class(new_data $ time_value )) & ! inherits(forecast_date , " POSIXt" )) {
7465 cli_abort(
7566 paste(
7667 " the data matrix `forecast_date` value is {forecast_date}, " ,
@@ -84,13 +75,13 @@ get_forecast_date <- function(new_data, info, epi_keys_checked, latency, columns
8475 if (is.null(forecast_date ) || is.na(forecast_date )) {
8576 cli_warn(
8677 paste(
87- " epi_data's `forecast_date` was {forecast_date} , setting to " ,
88- " the latest time value, {max_time}."
78+ " epi_data's `forecast_date` was `NA` , setting to " ,
79+ " the latest non-`NA` time value for these columns , {max_time}."
8980 ),
9081 class = " epipredict__get_forecast_date__max_time_warning"
9182 )
9283 forecast_date <- max_time
93- } else if (forecast_date < max_time ) {
84+ } else if (! is.null( max_time ) && ( forecast_date < max_time ) ) {
9485 cli_abort(
9586 paste(
9687 " `forecast_date` ({(forecast_date)}) is before the most " ,
@@ -101,19 +92,46 @@ get_forecast_date <- function(new_data, info, epi_keys_checked, latency, columns
10192 )
10293 }
10394 # TODO cover the rest of the possible types for as_of and max_time...
104- if (inherits(max_time , " Date" )) {
95+ if (inherits(new_data $ time_value , " Date" )) {
10596 forecast_date <- as.Date(forecast_date )
10697 }
10798 return (forecast_date )
10899}
109100
101+ get_max_time <- function (new_data , epi_keys_checked , columns ) {
102+ # these are the non-na time_values;
103+ # get the minimum value across the checked epi_keys' maximum time values
104+ max_time <- new_data %> %
105+ select(all_of(columns )) %> %
106+ drop_na()
107+ if (nrow(max_time ) == 0 ) {
108+ return (NULL )
109+ }
110+ # null and "" don't work in `group_by`
111+ if (! is.null(epi_keys_checked ) && all(epi_keys_checked != " " )) {
112+ max_time <- max_time %> % group_by(across(all_of(epi_keys_checked )))
113+ }
114+ max_time <- max_time %> %
115+ summarise(time_value = max(time_value )) %> %
116+ pull(time_value ) %> %
117+ min()
118+ return (max_time )
119+ }
120+
121+
122+
110123# ' the latency is also the amount the shift is off by
111124# ' @param sign_shift integer. 1 if lag and -1 if ahead. These represent how you
112125# ' need to shift the data to bring the 3 day lagged value to today.
113126# ' @keywords internal
114127get_latency <- function (new_data , forecast_date , column , sign_shift , epi_keys_checked ) {
115128 shift_max_date <- new_data %> %
116129 drop_na(all_of(column ))
130+ if (nrow(shift_max_date ) == 0 ) {
131+ # if everything is an NA, there's infinite latency, but shifting by that is
132+ # untenable. May as well not shift at all
133+ return (0 )
134+ }
117135 # null and "" don't work in `group_by`
118136 if (! is.null(epi_keys_checked ) && all(epi_keys_checked != " " )) {
119137 shift_max_date <- shift_max_date %> % group_by(across(all_of(epi_keys_checked )))
0 commit comments