/
get_tb_burden.R
237 lines (211 loc) · 8.21 KB
/
get_tb_burden.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#' Get the WHO TB Burden Data
#'
#' @description Get the TB burden data, and multi-drug resistant TB (MDR-TB) data from the WHO, see
#' [here](http://www.who.int/tb/country/data/download/en/) for details. This function will first attempt
#' to load the data from the temporary directory (\code{\link[base]{tempdir}}). If that fails, and \code{download_data = TRUE}, it
#' will instead download the data. The MDR TB data is only available for the latest year of data.
#'
#' @param url Character string, indicating the url of the TB burden data.
#' Default is current url. This argument is depreciated and will be removed from future releases.
#' The TB burden URL is now supplied internally - see \code{\link[getTBinR]{available_datasets}} for details.
#' @param add_mdr_data Logical, defaults to \code{TRUE}. Should MDR TB burden data be downloaded and joined
#' to the TB burden data.
#' @param additional_datasets A character vector specifying the names of the additional datasets to import.
#' See \code{\link[getTBinR]{available_datasets}} for available datasets. Use "all" to download all available
#' datasets (experimental datasets such as incidence by age and sex are excluded from this list).
#' @param mdr_save_name Character string, name to save the MDR data under. This argument is depreciated
#' and will be removed from future releases. Dataset naming is now handled internally.
#' @param mdr_url Character string, indicating the url of the MDR TB data. This argument is depreciated
#' and will be removed from future releases. The MDR-TB burden URL is now supplied internally -
#' see \code{\link[getTBinR]{available_datasets}} for details.
#' @param burden_save_name Character string, name to save the data under. This argument is depreciated
#' and will be removed from future releases. Dataset naming is now handled internally.
#' @param return Logical, should the data be returned as a dataframe.
#' Defaults to \code{TRUE}.
#'
#' @return The WHO TB burden data as a tibble.
#' @inheritParams get_data
#' @importFrom dplyr case_when mutate mutate_if mutate_all left_join full_join filter rename
#' @importFrom tibble as_tibble
#' @importFrom purrr map reduce
#' @export
#' @seealso get_data search_data_dict
#' @examples
#'
#'
#' ## Default datasets
#' tb_burden <- get_tb_burden(additional_datasets = available_datasets$dataset[3])
#'
#' head(tb_burden)
#'
#' ## Add in the latent TB dataset as an additional dataset (see getTBinR::avaiable_datasets)
#' tb_with_latents <- get_tb_burden(additional_datasets = available_datasets$dataset[3])
#'
#' head(tb_with_latents)
get_tb_burden <- function(url = NULL,
download_data = TRUE,
save = TRUE,
burden_save_name = NULL,
add_mdr_data = TRUE,
additional_datasets = NULL,
mdr_save_name = NULL,
mdr_url = NULL,
return = TRUE,
verbose = FALSE,
use_utils = FALSE,
retry_download = TRUE) {
g_whoregion <- NULL
. <- NULL
best <- NULL
lo <- NULL
hi <- NULL
age_group <- NULL
sex <- NULL
if (!is.null(url)) {
warning("This argument is depreciated and will be removed from future releases.
The TB burden URL is now supplied internally.")
} else {
url <- getTBinR::available_datasets$url[1]
}
if (!is.null(burden_save_name)) {
warning("This argument is depreciated and will be removed from future releases.
The dataset savename is now supplied internally.")
} else {
burden_save_name <- "tb_burden"
}
if (!is.null(mdr_url)) {
warning("This argument is depreciated and will be removed from future releases.
The MDR-TB burden URL is now supplied internally.")
} else {
mdr_url <- getTBinR::available_datasets$url[2]
}
if (!is.null(mdr_save_name)) {
warning("This argument is depreciated and will be removed from future releases.
The dataset savename is now supplied internally.")
} else {
mdr_save_name <- "mdr_tb"
}
trans_burden_data <- function(tb_df) {
tb_df <- tibble::as_tibble(tb_df)
tb_df <- mutate_all(tb_df, .funs = list(~ {
ifelse(. %in% c("NA", "`<NA>`"), NA, .)
}))
tb_df <- mutate_if(tb_df, is.numeric, .funs = list(~ {
ifelse(. %in% c(Inf, NaN), NA, .)
}))
tb_df$iso_numeric <- tb_df$iso_numeric %>%
as.numeric() %>%
as.integer()
return(tb_df)
}
## Get TB burden data
tb_burden <- get_data(
url = url,
download_data = download_data,
data_trans_fn = trans_burden_data,
save = save,
save_name = burden_save_name,
return = return,
verbose = verbose,
use_utils = use_utils
)
## Get MDR TB data
if (add_mdr_data) {
trans_mdr_data <- function(tb_df) {
tb_df <- tibble::as_tibble(tb_df)
tb_df <- mutate_all(tb_df, .funs = list(~ {
ifelse(. %in% c("NA", "`<NA>`"), NA, .)
}))
tb_df <- mutate_if(tb_df, is.numeric, .funs = list(~ {
ifelse(. %in% c(Inf, NaN), NA, .)
}))
tb_df$iso_numeric <- tb_df$iso_numeric %>%
as.numeric() %>%
as.integer()
return(tb_df)
}
mdr_tb <- get_data(
url = mdr_url,
download_data = download_data,
data_trans_fn = trans_mdr_data,
save = save,
save_name = mdr_save_name,
return = return,
verbose = verbose,
use_utils = use_utils
)
if (verbose) {
message("Joining TB burden data and MDR TB data.")
}
tb_burden <- suppressMessages(left_join(tb_burden, mdr_tb))
}
## Get additional datasets if asked to
if (!is.null(additional_datasets)) {
if (additional_datasets == "all") {
additional_datasets <- getTBinR::available_datasets$dataset[-c(1:3)]
}
load_additional_dataset <- function(dataset) {
if (verbose) {
message("Getting additional dataset: ", dataset)
}
if (!any(grepl(dataset, getTBinR::available_datasets$dataset))) {
stop(dataset, " is not listed in available_datasets and so cannot be imported.")
}
generic_trans <- function(df) {
df <- tibble::as_tibble(df)
df <- mutate_all(df, .funs = list(~ {
ifelse(. %in% c("NA", "`<NA>`"), NA, .)
}))
df <- mutate_if(df, is.numeric, .funs = list(~ {
ifelse(. %in% c(Inf, NaN), NA, .)
}))
df$iso_numeric <- df$iso_numeric %>%
as.numeric() %>%
as.integer()
return(df)
}
# Use available_datasets for url and get name based on that supplied.
url <- getTBinR::available_datasets$url[grepl(dataset, getTBinR::available_datasets$dataset)][1]
name <- tolower(dataset) %>%
gsub(" ", "_", .)
ad_df <- get_data(
url = url,
download_data = download_data,
data_trans_fn = generic_trans,
save = save,
save_name = name,
return = return,
verbose = verbose,
use_utils = use_utils
)
if (grepl("Incidence by age and sex", dataset)) {
message("Incidence by age and sex data is experimental and may cause issues for other
datasets. Use with caution!
Open an issue here if you run into problems: https://github.com/seabbs/getTBinR/issues")
ad_df <- ad_df %>%
dplyr::rename(inc_age_sex = best, inc_age_sex_lo = lo, inc_age_sex_hi = hi) %>%
dplyr::filter(!(age_group %in% c("all", "15plus")), sex != "a")
}
return(ad_df)
}
# Run data loading function over all supplied additional dataset names
datasets <- map(additional_datasets, ~ load_additional_dataset(.))
if (verbose) {
message("Joining TB burden data and additional datasets.")
}
tb_burden <- suppressMessages(
reduce(datasets, full_join, .init = tb_burden)
)
}
## Common dataset cleaning
tb_burden <- mutate(tb_burden, g_whoregion = case_when(
g_whoregion %in% "AFR" ~ "Africa",
g_whoregion %in% "AMR" ~ "Americas",
g_whoregion %in% "EMR" ~ "Eastern Mediterranean",
g_whoregion %in% "EUR" ~ "Europe",
g_whoregion %in% "SEA" ~ "South-East Asia",
g_whoregion %in% "WPR" ~ "Western Pacific",
TRUE ~ g_whoregion
))
return(tb_burden)
}