Skip to content

Commit 0dd29c3

Browse files
authored
Merge pull request #325 from cmu-delphi/r/fix-imports
Fix imports from dplyr and lubridate
2 parents 3de3b81 + 9da6127 commit 0dd29c3

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

R-packages/covidcast/DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Imports:
4343
grDevices,
4444
httr,
4545
jsonlite,
46-
lubridate,
4746
maptools,
4847
purrr,
4948
rlang,

R-packages/covidcast/R/covidcast.R

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,9 @@ covidcast_meta <- function() {
438438
}
439439

440440
meta <- meta$epidata %>%
441-
dplyr::mutate(min_time = as.Date(as.character(.data$min_time), format = "%Y%m%d"),
442-
max_time = as.Date(as.character(.data$max_time), format = "%Y%m%d"),
443-
max_issue = as.Date(as.character(.data$max_issue), format = "%Y%m%d"))
441+
dplyr::mutate(min_time = api_to_date(.data$min_time),
442+
max_time = api_to_date(.data$max_time),
443+
max_issue = api_to_date(.data$max_issue))
444444

445445
class(meta) <- c("covidcast_meta", "data.frame")
446446
return(meta)
@@ -589,15 +589,18 @@ covidcast_days <- function(data_source, signal, start_day, end_day, geo_type,
589589
}
590590
if (dat[[i]]$message == "success") {
591591
desired_geos <- tolower(unique(geo_value))
592+
592593
returned_epidata <- dat[[i]]$epidata
593594
returned_geo_array <- returned_epidata %>%
594-
select(geo_value, time_value) %>%
595-
group_by(time_value) %>%
596-
summarize(geo_value = list(geo_value))
595+
dplyr::select(geo_value, time_value) %>%
596+
dplyr::group_by(time_value) %>%
597+
dplyr::summarize(geo_value = list(geo_value))
597598
returned_time_values <- returned_geo_array$time_value
599+
598600
if (length(returned_time_values) != length(time_values)) {
599601
missing_time_values <- setdiff(time_values, returned_time_values)
600-
missing_dates <- ymd(missing_time_values)
602+
missing_dates <- api_to_date(missing_time_values)
603+
601604
warn(sprintf("Data not fetched for the following days: %s",
602605
paste(missing_dates, collapse = ", ")),
603606
data_source = data_source,
@@ -611,6 +614,7 @@ covidcast_days <- function(data_source, signal, start_day, end_day, geo_type,
611614
if (!identical("*", geo_value)) {
612615
missing_geo_array <- returned_geo_array[
613616
lapply(returned_geo_array$geo_value, length) < length(desired_geos), ]
617+
614618
if (nrow(missing_geo_array) > 0) {
615619
missing_geo_array$warning <-
616620
unlist(apply(returned_geo_array,
@@ -620,7 +624,7 @@ covidcast_days <- function(data_source, signal, start_day, end_day, geo_type,
620624
warn(missing_geo_array$warning,
621625
data_source = data_source,
622626
signal = signal,
623-
day = ymd(missing_geo_array$time_value),
627+
day = api_to_date(missing_geo_array$time_value),
624628
geo_value = geo_value,
625629
api_msg = dat[[i]]$message,
626630
class = "covidcast_missing_geo_values")
@@ -647,8 +651,8 @@ covidcast_days <- function(data_source, signal, start_day, end_day, geo_type,
647651

648652
if (nrow(df) > 0) {
649653
# If no data is found, there is no time_value column to report
650-
df$time_value <- as.Date(as.character(df$time_value), format = "%Y%m%d")
651-
df$issue <- as.Date(as.character(df$issue), format = "%Y%m%d")
654+
df$time_value <- api_to_date(df$time_value)
655+
df$issue <- api_to_date(df$issue)
652656
df$data_source <- data_source
653657
df$signal <- signal
654658

@@ -668,7 +672,7 @@ geo_warning_message <- function(row, desired_geos) {
668672
if (length(missing_geos) > 0) {
669673
missing_geos_str <- paste0(missing_geos, collapse = ", ")
670674
err_msg <- sprintf("Data not fetched for some geographies on %s: %s",
671-
ymd(row$time_value), missing_geos_str)
675+
api_to_date(row$time_value), missing_geos_str)
672676
return(err_msg)
673677
}
674678
}
@@ -755,3 +759,8 @@ covidcast <- function(data_source, signal, time_type, geo_type, time_values,
755759
date_to_string <- function(mydate) {
756760
format(mydate, "%Y%m%d")
757761
}
762+
763+
# Convert dates from API to Date objects
764+
api_to_date <- function(str) {
765+
as.Date(as.character(str), format = "%Y%m%d")
766+
}

R-packages/covidcast/tests/testthat/test-covidcast.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
library(covidcast)
22
library(httptest)
33
library(mockery)
4-
library(lubridate)
54
library(dplyr)
65

76
# Many of these tests use mockery::with_mock_api. This replaces calls to the
@@ -145,7 +144,7 @@ test_that("covidcast_days does not treat \"*\" as a missing geo_value", {
145144
signal = "signal",
146145
time_value = c(20201030, 20201031),
147146
direction = NA,
148-
issue = ymd("2020-11-04"),
147+
issue = as.Date("2020-11-04"),
149148
lag = 2,
150149
value = 3,
151150
stderr = NA,
@@ -156,8 +155,8 @@ test_that("covidcast_days does not treat \"*\" as a missing geo_value", {
156155
covidcast_days(
157156
data_source = "fb-survey",
158157
signal = "raw_cli",
159-
start_day = ymd("2020-10-30"),
160-
end_day = ymd("2020-10-31"),
158+
start_day = as.Date("2020-10-30"),
159+
end_day = as.Date("2020-10-31"),
161160
geo_type = "county",
162161
geo_value = c("*"),
163162
as_of = NULL,
@@ -174,7 +173,7 @@ test_that("covidcast_days does not raise warnings for full response", {
174173
signal = "signal",
175174
time_value = c(20201030, 20201031),
176175
direction = NA,
177-
issue = ymd("2020-11-04"),
176+
issue = as.Date("2020-11-04"),
178177
lag = 2,
179178
value = 3,
180179
stderr = NA,
@@ -185,8 +184,8 @@ test_that("covidcast_days does not raise warnings for full response", {
185184
covidcast_days(
186185
data_source = "fb-survey",
187186
signal = "raw_cli",
188-
start_day = ymd("2020-10-30"),
189-
end_day = ymd("2020-10-31"),
187+
start_day = as.Date("2020-10-30"),
188+
end_day = as.Date("2020-10-31"),
190189
geo_type = "county",
191190
geo_value = c("geoa"),
192191
as_of = NULL,
@@ -202,7 +201,7 @@ test_that("covidcast_days batches calls to covidcast", {
202201
signal = "signal",
203202
time_value = rep(NA, 3),
204203
direction = NA,
205-
issue = ymd("2020-11-04"),
204+
issue = as.Date("2020-11-04"),
206205
lag = 2,
207206
value = 3,
208207
stderr = NA,
@@ -217,8 +216,8 @@ test_that("covidcast_days batches calls to covidcast", {
217216
covidcast_days(
218217
data_source = "fb-survey",
219218
signal = "raw_cli",
220-
start_day = ymd("2020-10-01"),
221-
end_day = ymd("2020-10-06"),
219+
start_day = as.Date("2020-10-01"),
220+
end_day = as.Date("2020-10-06"),
222221
geo_type = "county",
223222
geo_value = "*",
224223
as_of = NULL,

0 commit comments

Comments
 (0)