Skip to content

Commit 49896af

Browse files
authored
Merge pull request #266 from cmu-delphi/ndefries/cleanup-dplyr-in-usage
Simplify `dplyr` use in dashboard `filter` logic
2 parents ffb4484 + 49c1da0 commit 49896af

File tree

6 files changed

+98
-95
lines changed

6 files changed

+98
-95
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Imports:
2525
stringr,
2626
markdown,
2727
memoise,
28-
purrr
28+
purrr,
29+
data.table
2930
Suggests:
3031
styler,
3132
lintr,

app/R/data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ createS3DataLoader <- function() {
128128
s3Contents <<- newS3Contents
129129
}
130130
if (s3BucketHasChanged ||
131-
!(targetVariable %in% names(df_list)) ||
131+
!(targetVariable %chin% names(df_list)) ||
132132
nrow(df_list[[targetVariable]]) == 0) {
133133
df_list[[targetVariable]] <<- getAllData(s3DataFetcher, targetVariable)
134134
dataCreationDate <<- getCreationDate(s3DataFetcher)

app/R/data_manipulation.R

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ renameScoreCol <- function(filteredScoreDf, scoreType, coverageInterval) {
1414

1515
filterOverAllLocations <- function(filteredScoreDf, scoreType, hasAsOfData = FALSE, filterDate) {
1616
locationsIntersect <- list()
17-
filteredScoreDf <- filteredScoreDf %>% filter(!is.na(Score) | target_end_date >= filterDate)
17+
filteredScoreDf <- filter(filteredScoreDf, !is.na(Score) | target_end_date >= filterDate)
1818
# Create df with col for all locations across each unique date, ahead and forecaster combo
1919
locationDf <- filteredScoreDf %>%
2020
group_by(forecaster, target_end_date, ahead) %>%
2121
summarize(location_list = paste(sort(unique(geo_value)), collapse = ","))
22-
locationDf <- locationDf %>% filter(location_list != c("us"))
22+
locationDf <- filter(locationDf, location_list != c("us"))
2323
# Create a list containing each row's location list
2424
locationList <- sapply(locationDf$location_list, function(x) strsplit(x, ","))
2525
locationList <- lapply(locationList, function(x) x[x != "us"])
2626
# Get the intersection of all the locations in these lists
2727
locationsIntersect <- unique(Reduce(intersect, locationList))
28-
filteredScoreDf <- filteredScoreDf %>% filter(geo_value %in% locationsIntersect)
28+
filteredScoreDf <- filter(filteredScoreDf, geo_value %chin% locationsIntersect)
2929
if (scoreType == "coverage") {
3030
if (hasAsOfData) {
3131
filteredScoreDf <- filteredScoreDf %>%
@@ -56,40 +56,23 @@ filterOverAllLocations <- function(filteredScoreDf, scoreType, hasAsOfData = FAL
5656
# Only use weekly aheads for hospitalizations
5757
# May change in the future
5858
filterHospitalizationsAheads <- function(scoreDf) {
59-
scoreDf["weekday"] <- weekdays(as.Date(scoreDf$target_end_date))
60-
scoreDf <- scoreDf %>% filter(weekday == HOSPITALIZATIONS_TARGET_DAY)
59+
days_list <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
60+
# Make sure to use `data.table`'s `wday`; `lubridate` has a function of the same name.
61+
scoreDf["weekday"] <- days_list[data.table::wday(as.Date(scoreDf$target_end_date, "%Y-%m-%d"))]
62+
scoreDf <- filter(scoreDf, weekday == HOSPITALIZATIONS_TARGET_DAY)
63+
scoreDf$ahead_group <- case_when(
64+
scoreDf$ahead >= HOSPITALIZATIONS_OFFSET & scoreDf$ahead < 7 + HOSPITALIZATIONS_OFFSET ~ 1L,
65+
scoreDf$ahead >= 7 + HOSPITALIZATIONS_OFFSET & scoreDf$ahead < 14 + HOSPITALIZATIONS_OFFSET ~ 2L,
66+
scoreDf$ahead >= 14 + HOSPITALIZATIONS_OFFSET & scoreDf$ahead < 21 + HOSPITALIZATIONS_OFFSET ~ 3L,
67+
scoreDf$ahead >= 21 + HOSPITALIZATIONS_OFFSET & scoreDf$ahead < 28 + HOSPITALIZATIONS_OFFSET ~ 4L,
68+
TRUE ~ NA_integer_
69+
)
6170

62-
oneAheadDf <- scoreDf %>%
63-
filter(ahead >= HOSPITALIZATIONS_OFFSET) %>%
64-
filter(ahead < 7 + HOSPITALIZATIONS_OFFSET) %>%
65-
group_by(target_end_date, forecaster) %>%
66-
filter(ahead == min(ahead)) %>%
67-
mutate(ahead = HOSPITALIZATIONS_AHEAD_OPTIONS[1])
68-
69-
return(bind_rows(
70-
scoreDf %>%
71-
filter(ahead >= HOSPITALIZATIONS_OFFSET) %>%
72-
filter(ahead < 7 + HOSPITALIZATIONS_OFFSET) %>%
73-
group_by(target_end_date, forecaster) %>%
74-
filter(ahead == min(ahead)) %>%
75-
mutate(ahead = HOSPITALIZATIONS_AHEAD_OPTIONS[1]),
76-
scoreDf %>%
77-
filter(ahead >= 7 + HOSPITALIZATIONS_OFFSET) %>%
78-
filter(ahead < 14 + HOSPITALIZATIONS_OFFSET) %>%
79-
group_by(target_end_date, forecaster) %>%
80-
filter(ahead == min(ahead)) %>%
81-
mutate(ahead = HOSPITALIZATIONS_AHEAD_OPTIONS[2]),
82-
scoreDf %>%
83-
filter(ahead >= 14 + HOSPITALIZATIONS_OFFSET) %>%
84-
filter(ahead < 21 + HOSPITALIZATIONS_OFFSET) %>%
85-
group_by(target_end_date, forecaster) %>%
86-
filter(ahead == min(ahead)) %>%
87-
mutate(ahead = HOSPITALIZATIONS_AHEAD_OPTIONS[3]),
71+
return(
8872
scoreDf %>%
89-
filter(ahead >= 21 + HOSPITALIZATIONS_OFFSET) %>%
90-
filter(ahead < 28 + HOSPITALIZATIONS_OFFSET) %>%
91-
group_by(target_end_date, forecaster) %>%
73+
filter(!is.na(ahead_group)) %>%
74+
group_by(target_end_date, forecaster, ahead_group) %>%
9275
filter(ahead == min(ahead)) %>%
93-
mutate(ahead = HOSPITALIZATIONS_AHEAD_OPTIONS[4])
94-
))
76+
mutate(ahead = HOSPITALIZATIONS_AHEAD_OPTIONS[ahead_group])
77+
)
9578
}

app/R/exportScores.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exportScoresUI <- function(id = "exportScores") {
88
createExportScoresDataFrame <- function(scoreDf, targetVariable, scoreType, forecasters, loc, coverageInterval) {
99
scoreDf <- filter(
1010
scoreDf[[targetVariable]],
11-
forecaster %in% forecasters
11+
forecaster %chin% forecasters
1212
)
1313
scoreDf <- renameScoreCol(scoreDf, scoreType, coverageInterval)
1414

@@ -19,7 +19,7 @@ createExportScoresDataFrame <- function(scoreDf, targetVariable, scoreType, fore
1919
scoreDf <- filterOverAllLocations(scoreDf, scoreType)
2020
return(scoreDf[[1]])
2121
} else {
22-
scoreDf <- scoreDf %>% filter(geo_value == tolower(loc))
22+
scoreDf <- filter(scoreDf, geo_value == tolower(loc))
2323
scoreDf <- scoreDf[c(
2424
"ahead", "geo_value", "forecaster", "forecast_date",
2525
"data_source", "target_end_date", "Score", "actual"

app/global.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ library(lubridate)
88
library(viridis)
99
library(tsibble)
1010
library(covidcast)
11+
library(data.table)
1112

1213
appVersion <- "6.1.0"
1314

@@ -35,10 +36,10 @@ ARCHIVE_TAB_SUFFIX <- "_archive"
3536

3637

3738
TARGET_VARS_BY_TAB <- list()
38-
TARGET_VARS_BY_TAB[[paste0("evaluations", CURRENT_TAB_SUFFIX)]] <- list(
39+
TARGET_VARS_BY_TAB[[paste0("evaluations", CURRENT_TAB_SUFFIX)]] <- c(
3940
"Hospital Admissions" = "Hospitalizations"
4041
)
41-
TARGET_VARS_BY_TAB[[paste0("evaluations", ARCHIVE_TAB_SUFFIX)]] <- list(
42+
TARGET_VARS_BY_TAB[[paste0("evaluations", ARCHIVE_TAB_SUFFIX)]] <- c(
4243
"Incident Deaths" = "Deaths",
4344
"Incident Cases" = "Cases"
4445
)

0 commit comments

Comments
 (0)