Skip to content

Commit e6a2fe8

Browse files
authored
Merge pull request #204 from cmu-delphi/small-code-simplifications
Small style fixes
2 parents ae709d6 + 6b1d006 commit e6a2fe8

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

app/server.R

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,31 +128,31 @@ server <- function(input, output, session) {
128128
}
129129
# Need to do this after setting dfWithForecasts to leave in aheads for forecasts
130130
filteredScoreDf <- filteredScoreDf %>% filter(ahead %in% input$aheads)
131-
if (dim(filteredScoreDf)[1] == 0) {
131+
if (nrow(filteredScoreDf) == 0) {
132132
output$renderWarningText <- renderText(paste0(
133133
"The selected forecasters do not have enough data ",
134134
"to display the selected scoring metric."
135135
))
136136
return()
137137
}
138138
if (is.null(asOfData)) {
139-
if (!is.null(isolate(PREV_AS_OF_DATA())) && dim(isolate(PREV_AS_OF_DATA()))[1] != 0 &&
139+
if (!is.null(isolate(PREV_AS_OF_DATA())) && nrow(isolate(PREV_AS_OF_DATA())) != 0 &&
140140
isolate(input$asOf) != "" && isolate(input$asOf) != isolate(CURRENT_WEEK_END_DATE())) {
141141
asOfData <- isolate(PREV_AS_OF_DATA())
142142
}
143143
}
144-
if (!is.null(asOfData) && dim(asOfData)[1] != 0) {
144+
if (!is.null(asOfData) && nrow(asOfData) != 0) {
145145
asOfData <- asOfData %>% rename(target_end_date = time_value, as_of_actual = value)
146146
asOfData <- asOfData[c("target_end_date", "geo_value", "as_of_actual")]
147147

148148
# Get the 'as of' dates that are the target_end_dates in the scoring df
149149
dateGroupDf <- asOfData %>% filter(asOfData$target_end_date %in% filteredScoreDf$target_end_date)
150-
if (dim(dateGroupDf)[1] != 0) {
150+
if (nrow(dateGroupDf) != 0) {
151151
# Since cases and deaths are shown as weekly incidence, but the "as of" data from the covidcast API
152152
# is daily, we need to sum over the days leading up to the target_end_date of each week to get the
153153
# weekly incidence
154154
asOfData <- filterAsOfData(asOfData, dateGroupDf, filteredScoreDf)
155-
filteredScoreDf <- merge(filteredScoreDf, asOfData, by = c("target_end_date", "geo_value"), all = TRUE)
155+
filteredScoreDf <- full_join(filteredScoreDf, asOfData, by = c("target_end_date", "geo_value"))
156156
} else {
157157
# Input 'as of' date chosen does not match the available target_end_dates that result from the rest of the selected inputs
158158
# It is too far back or we are switching between hosp and cases/deaths which have different target date days
@@ -249,9 +249,10 @@ server <- function(input, output, session) {
249249
updateAsOfChoices(session, truthDf)
250250

251251
# Format and transform data for plot
252-
filteredScoreDf <- filteredScoreDf %>% filter(!is.na(Week_End_Date))
253-
filteredScoreDf <- filteredScoreDf[c("Forecaster", "Forecast_Date", "Week_End_Date", "Score", "ahead")]
254-
filteredScoreDf <- filteredScoreDf %>% mutate(across(where(is.numeric), ~ round(., 2)))
252+
filteredScoreDf <- filteredScoreDf %>%
253+
filter(!is.na(Week_End_Date)) %>%
254+
select(Forecaster, Forecast_Date, Week_End_Date, Score, ahead) %>%
255+
mutate(across(where(is.numeric), ~ round(., 2)))
255256
if (input$scoreType != "coverage") {
256257
if (input$scaleByBaseline) {
257258
baselineDf <- filteredScoreDf %>% filter(Forecaster %in% "COVIDhub-baseline")
@@ -328,12 +329,10 @@ server <- function(input, output, session) {
328329
}
329330
plotHeight <- 550 + (length(input$aheads) - 1) * 100
330331
finalPlot <-
331-
ggplotly(p, tooltip = c("x", "y", "shape", "label")) %>%
332+
ggplotly(p, tooltip = c("x", "y", "shape", "label"), height = plotHeight) %>%
332333
layout(
333-
height = plotHeight,
334334
legend = list(orientation = "h", y = -0.1),
335335
margin = list(t = 90),
336-
height = 500,
337336
hovermode = "x unified",
338337
xaxis = list(
339338
title = list(text = "Target Date", standoff = 8L),
@@ -389,7 +388,7 @@ server <- function(input, output, session) {
389388
geom_point(aes(y = Reported_As_Of_Incidence, color = "Reported_As_Of_Incidence"))
390389
if (input$showForecasts) {
391390
finalPlot <- finalPlot +
392-
geom_line(aes(y = Quantile_50, color = Forecaster, shape = Forecaster)) +
391+
geom_line(aes(y = Quantile_50, color = Forecaster)) +
393392
geom_point(aes(y = Quantile_50, color = Forecaster, shape = Forecaster))
394393
}
395394
} else {
@@ -460,12 +459,13 @@ server <- function(input, output, session) {
460459
# Create a df to fill in the corresponding target_end_date in a new date_group column for all intervening days
461460
dateGroupDf[, "date_group"] <- NA
462461
dateGroupDf$date_group <- dateGroupDf$target_end_date
463-
asOfData <- merge(asOfData, dateGroupDf, by = c("target_end_date", "geo_value", "as_of_actual"), all = TRUE)
462+
asOfData <- full_join(asOfData, dateGroupDf, by = c("target_end_date", "geo_value", "as_of_actual"))
464463

465464
# Cut off the extra days on beginning and end of series so that when we sum the values we are only
466465
# summing over the weeks included in the score plot
467-
asOfData <- asOfData %>% filter(target_end_date >= min(filteredScoreDf$target_end_date) - 6)
468-
asOfData <- asOfData %>% filter(target_end_date <= isolate(input$asOf))
466+
asOfData <- asOfData %>%
467+
filter(target_end_date >= min(filteredScoreDf$target_end_date) - 6) %>%
468+
filter(target_end_date <= isolate(input$asOf))
469469

470470
# Fill in the date_group column with the target week end days for all intervening days
471471
asOfData <- asOfData %>%
@@ -688,8 +688,8 @@ server <- function(input, output, session) {
688688
# Ensure there is always one forecaster selected
689689
if (length(input$forecasters) < 1) {
690690
updateSelectInput(session, "forecasters",
691-
selected = c("COVIDhub-ensemble")
692-
) # Use ensemble rather than baseline bc it has hospitalization scores
691+
selected = c("COVIDhub-baseline")
692+
)
693693
}
694694
# Ensure COVIDhub-baseline is selected when scaling by baseline
695695
if (input$scaleByBaseline && !("COVIDhub-baseline" %in% input$forecasters)) {
@@ -745,7 +745,7 @@ server <- function(input, output, session) {
745745
hideElement("truth-plot-loading-message")
746746
PREV_AS_OF_DATA(asOfTruthData)
747747

748-
if (dim(asOfTruthData)[1] == 0) {
748+
if (nrow(asOfTruthData) == 0) {
749749
return()
750750
}
751751
summaryPlot(reRenderTruth = TRUE, asOfData = asOfTruthData)

0 commit comments

Comments
 (0)