@@ -5,7 +5,7 @@ library(aws.s3)
5
5
shinyOptions(cache = cachem :: cache_mem(max_size = 1000 * 1024 ^ 2 , evict = " lru" ))
6
6
cache <- getShinyOption(" cache" )
7
7
8
- # Since covidcast data updates about once a day. Add date arg to
8
+ # Since covidcast data updates about once a day, add date arg to
9
9
# covidcast_signal so caches aren't used after that.
10
10
covidcast_signal_mem <- function (... , date = Sys.Date()) {
11
11
return (covidcast_signal(... ))
@@ -67,65 +67,74 @@ getCreationDate <- function(loadFile) {
67
67
}
68
68
69
69
70
- getAllData <- function (loadFile ) {
71
- dfStateCases <- loadFile(" score_cards_state_cases.rds" )
72
- dfStateDeaths <- loadFile(" score_cards_state_deaths.rds" )
73
- dfStateHospitalizations <- loadFile(" score_cards_state_hospitalizations.rds" )
74
- dfNationCases <- loadFile(" score_cards_nation_cases.rds" )
75
- dfNationDeaths <- loadFile(" score_cards_nation_deaths.rds" )
76
- dfNationHospitalizations <- loadFile(" score_cards_nation_hospitalizations.rds" )
70
+ getAllData <- function (loadFile , targetVariable ) {
71
+ df <- switch (targetVariable ,
72
+ " Deaths" = bind_rows(
73
+ loadFile(" score_cards_state_deaths.rds" ),
74
+ loadFile(" score_cards_nation_deaths.rds" )
75
+ ),
76
+ " Cases" = bind_rows(
77
+ loadFile(" score_cards_state_cases.rds" ),
78
+ loadFile(" score_cards_nation_cases.rds" )
79
+ ),
80
+ " Hospitalizations" = bind_rows(
81
+ loadFile(" score_cards_state_hospitalizations.rds" ),
82
+ loadFile(" score_cards_nation_hospitalizations.rds" )
83
+ )
84
+ )
77
85
78
- # Pick out expected columns only
79
- covCols <- paste0(" cov_" , COVERAGE_INTERVALS )
80
- expectedCols <- c(
86
+ # The names of the `covCols` elements become the new names of those columns
87
+ # when we use this vector in the `select` below.
88
+ covCols <- setNames(paste0(" cov_" , COVERAGE_INTERVALS ), COVERAGE_INTERVALS )
89
+ keepCols <- c(
81
90
" ahead" , " geo_value" , " forecaster" , " forecast_date" ,
82
91
" data_source" , " signal" , " target_end_date" , " incidence_period" ,
83
92
" actual" , " wis" , " sharpness" , " ae" , " value_50" ,
84
93
covCols
85
94
)
86
-
87
- df <- bind_rows(
88
- dfStateCases %> % select(all_of(expectedCols )),
89
- dfStateDeaths %> % select(all_of(expectedCols )),
90
- dfStateHospitalizations %> % select(all_of(expectedCols )),
91
- dfNationCases %> % select(all_of(expectedCols )),
92
- dfNationDeaths %> % select(all_of(expectedCols )),
93
- dfNationHospitalizations %> % select(all_of(expectedCols ))
94
- )
95
- df <- df %> % rename(
96
- " 10" = cov_10 , " 20" = cov_20 , " 30" = cov_30 ,
97
- " 40" = cov_40 , " 50" = cov_50 , " 60" = cov_60 , " 70" = cov_70 ,
98
- " 80" = cov_80 , " 90" = cov_90 , " 95" = cov_95 , " 98" = cov_98
99
- )
95
+ df <- select(df , all_of(keepCols ))
100
96
101
97
return (df )
102
98
}
103
99
104
100
createS3DataLoader <- function () {
101
+ # Cached connection info
105
102
s3bucket <- getS3Bucket()
106
- df <- data.frame ()
103
+ s3DataFetcher <- createS3DataFactory(s3bucket )
104
+ s3Contents <- s3bucket [attr(s3bucket , " names" , exact = TRUE )]
105
+
106
+ # Cached data
107
+ df_list <- list ()
107
108
dataCreationDate <- as.Date(NA )
108
109
109
- getRecentData <- function () {
110
- newS3bucket <- getS3Bucket( )
110
+ getRecentData <- function (targetVariable = TARGET_OPTIONS ) {
111
+ targetVariable <- match.arg( targetVariable )
111
112
112
- s3Contents <- s3bucket [attr( s3bucket , " names " , exact = TRUE )]
113
+ newS3bucket <- getS3Bucket()
113
114
newS3Contents <- newS3bucket [attr(newS3bucket , " names" , exact = TRUE )]
115
+ s3BucketHasChanged <- ! identical(s3Contents , newS3Contents )
114
116
115
- # Fetch new score data if contents of S3 bucket has changed (including file
117
+ # Fetch new data if contents of S3 bucket has changed (including file
116
118
# names, sizes, and last modified timestamps). Ignores characteristics of
117
- # bucket and request, including bucket region, name, content type, request
118
- # date, request ID, etc.
119
- if (nrow(df ) == 0 || ! identical(s3Contents , newS3Contents )) {
120
- # Save new data and new bucket connection info to vars in env of
121
- # `getRecentDataHelper`. They persist between calls to `getRecentData` a
122
- # la https://stackoverflow.com/questions/1088639/static-variables-in-r
119
+ # bucket and request, including bucket region, name, content type,
120
+ # request date, request ID, etc.
121
+ #
122
+ # Save new score data and new bucket connection info to vars in env of
123
+ # `createS3DataLoader`. They persist between calls to `getRecentData` a
124
+ # la https://stackoverflow.com/questions/1088639/static-variables-in-r
125
+ if (s3BucketHasChanged ) {
123
126
s3bucket <<- newS3bucket
124
- df <<- getAllData(createS3DataFactory(s3bucket ))
125
- dataCreationDate <<- getCreationDate(createS3DataFactory(s3bucket ))
127
+ s3DataFetcher <<- createS3DataFactory(newS3bucket )
128
+ s3Contents <<- newS3Contents
129
+ }
130
+ if (s3BucketHasChanged ||
131
+ ! (targetVariable %chin % names(df_list )) ||
132
+ nrow(df_list [[targetVariable ]]) == 0 ) {
133
+ df_list [[targetVariable ]] <<- getAllData(s3DataFetcher , targetVariable )
134
+ dataCreationDate <<- getCreationDate(s3DataFetcher )
126
135
}
127
136
128
- return (list (df = df , dataCreationDate = dataCreationDate ))
137
+ return (list (df_list = df_list , dataCreationDate = dataCreationDate ))
129
138
}
130
139
131
140
return (getRecentData )
@@ -134,12 +143,17 @@ createS3DataLoader <- function() {
134
143
135
144
# ' create a data loader with fallback data only
136
145
createFallbackDataLoader <- function () {
137
- df <- getAllData(getFallbackData )
146
+ df_list <- list ()
147
+ for (targetVariable in TARGET_OPTIONS ) {
148
+ df_list [[targetVariable ]] <- getAllData(getFallbackData , targetVariable )
149
+ }
150
+ dataCreationDate <- getCreationDate(getFallbackData )
138
151
139
152
dataLoader <- function () {
140
- df
153
+ return ( list ( df_list = df_list , dataCreationDate = dataCreationDate ))
141
154
}
142
- dataLoader
155
+
156
+ return (dataLoader )
143
157
}
144
158
145
159
0 commit comments