-
Notifications
You must be signed in to change notification settings - Fork 1
/
refresh_covid19mobility_apple.R
163 lines (144 loc) · 4.04 KB
/
refresh_covid19mobility_apple.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#' Refresh The Apple Covid-19 Mobility Data for Countries
#'
#' @description Pulls in the CSV of the Apple Mobility Data, filters to country, and reshapes it
#'
#' @return Returns a tibble that meets the Covid19R Project tidy data standard
#' @export
#'
#' @references \url{https://www.apple.com/covid19/mobility}
#'
#' @examples
#' \donttest{
#'
#' mob <- refresh_covid19mobility_apple_country()
#'
#' head(mob)
#' }
#'
refresh_covid19mobility_apple_country <- function() {
# import the apple mobility data
mob_data <- import_apple_mob_data() %>%
dplyr::filter(geo_type == "country/region")
# reshape the apple mobility data
mob_data <- reshape_apple_mob_data(mob_data) %>%
add_country_codes()
# return the apple mobility data
mob_data %>%
dplyr::mutate(location_type = "country") %>%
reorder_apple() %>%
dplyr::select(-sub_region, -country)
}
#' Refresh The Apple Covid-19 Mobility Data for Subregions
#'
#' @description Pulls in the CSV of the Apple Mobility Data, filters to subregions, and reshapes it
#'
#' @return Returns a tibble that meets the Covid19R Project tidy data standard
#' @export
#'
#' @references \url{https://www.apple.com/covid19/mobility}
#'
#' @examples
#' \donttest{
#'
#' mob <- refresh_covid19mobility_apple_subregion()
#'
#' head(mob)
#' }
#'
refresh_covid19mobility_apple_subregion <- function() {
# import the apple mobility data
mob_data <- import_apple_mob_data() %>%
dplyr::filter(geo_type == "sub-region")
# reshape the apple mobility data
mob_data <- reshape_apple_mob_data(mob_data) %>%
add_subregion_codes()
# return the apple mobility data
mob_data %>%
dplyr::mutate(location_type = "state") %>%
reorder_apple() %>%
dplyr::select(-sub_region)
}
#' Refresh The Apple Covid-19 Mobility Data for Cities
#'
#' @description Pulls in the CSV of the Apple Mobility Data, filters to cities, and reshapes it
#'
#' @return Returns a tibble that meets the Covid19R Project tidy data standard
#' @export
#'
#' @references \url{https://www.apple.com/covid19/mobility}
#'
#' @examples
#' \donttest{
#'
#' mob <- refresh_covid19mobility_apple_city()
#'
#' head(mob)
#' }
#'
refresh_covid19mobility_apple_city <- function() {
# import the apple mobility data
mob_data <- import_apple_mob_data() %>%
dplyr::filter(geo_type == "city")
# reshape the apple mobility data
mob_data <- reshape_apple_mob_data(mob_data) %>%
add_city_codes()
# return the apple mobility data
mob_data %>%
dplyr::mutate(location_type = "city") %>%
reorder_apple()
}
# gak the data from apple
import_apple_mob_data <- function() {
# what is the base apple mobility URL
base_url <- "https://covid19-static.cdn-apple.com"
# get the JSON that tells us where the data file is
dat_json <- paste0(
base_url,
"/covid19-mobility-data/current/v3/index.json"
) %>%
jsonlite::fromJSON()
# make a URL for the data file
dat_url <- paste0(
base_url,
dat_json$basePath,
dat_json$regions$`en-us`$csvPath
)
# read in the data file as a tibble
suppressMessages(
readr::read_csv(dat_url)
)
}
# reshape the data to the covid19R standard
reshape_apple_mob_data <- function(mob_data) {
# reshape dates
mob_data %>%
tidyr::pivot_longer(
cols = dplyr::starts_with("202"), #debug here
names_to = "date",
values_to = "value"
) %>%
dplyr::mutate(date = lubridate::ymd(date)) %>%
# rename to covid19R standard
dplyr::rename(
location = region, location_type = geo_type,
data_type = transportation_type
) %>%
dplyr::mutate(location = ifelse(location == "UK", "United Kingdom", location)) %>%
# make data types in covid19R standard
dplyr::mutate(data_type = dplyr::case_when(
data_type == "driving" ~ "driving_req_rel_volume",
data_type == "walking" ~ "walking_req_rel_volume",
data_type == "transit" ~ "transit_req_rel_volume"
)) %>%
janitor::clean_names()
}
reorder_apple <- . %>%
dplyr::relocate(
date,
location,
location_type,
location_code,
location_code_type,
data_type,
value
)