@@ -5,7 +5,7 @@ library(aws.s3)
55shinyOptions(cache = cachem :: cache_mem(max_size = 1000 * 1024 ^ 2 , evict = " lru" ))
66cache <- getShinyOption(" cache" )
77
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
99# covidcast_signal so caches aren't used after that.
1010covidcast_signal_mem <- function (... , date = Sys.Date()) {
1111 return (covidcast_signal(... ))
@@ -67,65 +67,74 @@ getCreationDate <- function(loadFile) {
6767}
6868
6969
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+ )
7785
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(
8190 " ahead" , " geo_value" , " forecaster" , " forecast_date" ,
8291 " data_source" , " signal" , " target_end_date" , " incidence_period" ,
8392 " actual" , " wis" , " sharpness" , " ae" , " value_50" ,
8493 covCols
8594 )
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 ))
10096
10197 return (df )
10298}
10399
104100createS3DataLoader <- function () {
101+ # Cached connection info
105102 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 ()
107108 dataCreationDate <- as.Date(NA )
108109
109- getRecentData <- function () {
110- newS3bucket <- getS3Bucket( )
110+ getRecentData <- function (targetVariable = TARGET_OPTIONS ) {
111+ targetVariable <- match.arg( targetVariable )
111112
112- s3Contents <- s3bucket [attr( s3bucket , " names " , exact = TRUE )]
113+ newS3bucket <- getS3Bucket()
113114 newS3Contents <- newS3bucket [attr(newS3bucket , " names" , exact = TRUE )]
115+ s3BucketHasChanged <- ! identical(s3Contents , newS3Contents )
114116
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
116118 # 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 ) {
123126 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 )
126135 }
127136
128- return (list (df = df , dataCreationDate = dataCreationDate ))
137+ return (list (df_list = df_list , dataCreationDate = dataCreationDate ))
129138 }
130139
131140 return (getRecentData )
@@ -134,12 +143,17 @@ createS3DataLoader <- function() {
134143
135144# ' create a data loader with fallback data only
136145createFallbackDataLoader <- 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 )
138151
139152 dataLoader <- function () {
140- df
153+ return ( list ( df_list = df_list , dataCreationDate = dataCreationDate ))
141154 }
142- dataLoader
155+
156+ return (dataLoader )
143157}
144158
145159
0 commit comments