-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rhistory
371 lines (371 loc) · 11.4 KB
/
.Rhistory
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
get_species_list <- function(country) {
country <- tolower(country)
readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/monthly/species/country/{country}?format=csv"
),
col_types = readr::cols_only(
Spp = readr::col_integer(),
Common_group = readr::col_character(),
Common_species = readr::col_character(),
Genus = readr::col_character(),
Species = readr::col_character()
)
)
}
kenya_list <- get_species_list("kenya")
View(kenya_list)
find_species <- function(species_list, species_names) {
df_to_search <- species_list %>%
dplyr::mutate(SearchColumn = as.character(tolower(
glue::glue(
"{Common_group} {Common_species} {Genus} {Species} {Common_species} {Common_group} {Species} {Genus}"
)
)))
species_names %>%
purrr::map_df( ~ dplyr::filter(
df_to_search,
stringr::str_detect(df_to_search$SearchColumn, tolower(.x))
))
}
interesting_species <- c("boubou")
find_species(species_list = kenya_list, species_name = interesting_species)
library(magrittr)
find_species(species_list = kenya_list, species_name = interesting_species)
interesting_species <- c("drongo")
find_species(species_list = kenya_list, species_name = interesting_species)
library(rabm)
library(rabm)
#' region_type = 'country',
#' region_id = 'kenya',
#' return_type = 'data',
#' return_format = 'CSV'
#' )
#'
#'
#' }
#'
#'
extract_all <- function(start_date = '2019-01-01',
end_date = '2019-02-01',
region_type = 'country',
region_id = 'kenya',
return_type = 'data',
return_format = 'CSV') {
# requirements on inputs
if (is.na(as.Date(start_date, format = "%Y-%m-%d"))) {
stop(message = "start_date is incorrectly specified. Please use the YYYY-MM-DD format")
}
if (is.na(as.Date(end_date, format = "%Y-%m-%d"))) {
stop(message = "end_date is incorrectly specified. Please use the YYYY-MM-DD format")
}
region_type = tolower(region_type)
region_id = tolower(region_id)
return_type = tolower(return_type)
return_data <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/{return_type}/all?format={return_format}"
),
col_types = readr::cols()
)
return_data
}
View(extract_all())
?get_species_list
start_date = '2019-01-01'
start_date = '2019-01-01'
start_date = '2019-01-01'
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'data'
return_format = 'CSV'
return_data <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count/all?format={return_format}"
),
col_types = readr::cols()
)
return_data
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'count'
return_format = 'CSV'
return_data <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count/all?format={return_format}"
),
col_types = readr::cols()
)
return_data
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count/all?format={return_format}"
)
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'count'
return_format = 'CSV'
return_data <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count?format={return_format}"
),
col_types = readr::cols()
)
return_data
return_data$records[1]
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'count'
return_format = 'JSON'
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count?format={return_format}"
),
col_types = readr::cols()
)
return_data_count
return_format = 'CSV'
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count?format={return_format}"
),
col_types = readr::cols()
)
#' start_date = '2019-01-01',
#' end_date = '2019-02-01',
#' region_type = 'country',
#' region_id = 'kenya',
#' return_type = 'data',
#' return_format = 'CSV')
#'
#'
#'
#'
extract_all <- function(start_date = '2019-01-01',
end_date = '2019-02-01',
region_type = 'country',
region_id = 'kenya',
return_type = 'data',
return_format = 'CSV') {
# requirements on inputs
if (is.na(as.Date(start_date, format = "%Y-%m-%d"))) {
stop(message = "start_date is incorrectly specified. Please use the YYYY-MM-DD format")
}
if (is.na(as.Date(end_date, format = "%Y-%m-%d"))) {
stop(message = "end_date is incorrectly specified. Please use the YYYY-MM-DD format")
}
region_type = tolower(region_type)
region_id = tolower(region_id)
return_type = tolower(return_type)
# get number of records to be returned
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count?format={return_format}"
),
col_types = readr::cols()
)
if (return_data_count$records[1] > 250000) {
stop(message = "Your query returns more than 250,000 records. The API does not allow more than 250,000 records to be drawn at one. Please split your query.")
}
# get data
return_data <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/{return_type}/all?format={return_format}"
),
col_types = readr::cols()
)
return_data
}
View(extract_all())
#' Extract raw data from the Africa Bird Atlas
#'
#' @param start_date A character representing the start date from which you want to query the database. It must be in the YYYY-MM-DD format. Defaults to 1 Jan,2019. Example: '2019-01-26'.
#' @param end_date A character representing the end date to which you want to query the database. It must be in the YYYY-MM-DD format. Defaults to 1 Feb, 2019. Example: '2019-02-26'.
#' @param region_type Can take one of the following types: country, pentad, group, qdgc. Default is 'country'
#' @param region_id Has to be co-defined with `region_type`. That is, it is an instantiation of the `region_type`. For instance if the `region_type` = 'country', then the `region_id` = 'kenya'. Or if `region_type` = 'pentad', then `region_id` = '3355_1825'. Default is 'kenya'.
#' @param return_type There are two options, 'data' and 'count'. 'data' is the actual records submitted to the africa bird map. 'count' return a count of the rows. Default is 'data'.
#' @param return_format Default s 'CSV', though the API default is 'JSON'
#'
#' @return Depending on the return_format, the function will return a dataframe with the following:
#' 1. `return_type = data` A tidy dataframe is returned with the records that fall within the specification defined.
#' 2. `return_type = count` A tidy dataframe is returns with the number of rows that will be produced if the return_type was to be specified as 'data'.
#'
#' @examples
#'
#' # extract data for all of Kenya for Jan 2019
#'
#' extract_all(
#' start_date = '2019-01-01',
#' end_date = '2019-02-01',
#' region_type = 'country',
#' region_id = 'kenya',
#' return_type = 'data',
#' return_format = 'CSV')
#'
#'
#'
#'
extract_all <- function(start_date = '2019-01-01',
end_date = '2019-02-01',
region_type = 'country',
region_id = 'kenya',
return_type = 'data',
return_format = 'CSV') {
# requirements on inputs
if (is.na(as.Date(start_date, format = "%Y-%m-%d"))) {
stop(message = "start_date is incorrectly specified. Please use the YYYY-MM-DD format")
}
if (is.na(as.Date(end_date, format = "%Y-%m-%d"))) {
stop(message = "end_date is incorrectly specified. Please use the YYYY-MM-DD format")
}
region_type = tolower(region_type)
region_id = tolower(region_id)
return_type = tolower(return_type)
# get number of records to be returned
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count?format={return_format}"
),
col_types = readr::cols()
)
if (return_data_count$records[1] > 4000) {
stop(message = "Your query returns more than 250,000 records. The API does not allow more than 250,000 records to be drawn at one. Please split your query.")
}
# get data
return_data <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/{return_type}/all?format={return_format}"
),
col_types = readr::cols()
)
return_data
}
View(extract_all())
library(rabm)
species_ids = 123
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'data'
return_format = 'CSV'
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/species/{species_id}/count?format={return_format}"
),
col_types = readr::cols()
)
species_id = 123
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'data'
return_format = 'CSV'
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/species/{species_id}/count?format={return_format}"
),
col_types = readr::cols()
)
return_data_count
species_id = 123
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'data'
return_format = 'CSV'
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count/species/{species_id}?format={return_format}"
),
col_types = readr::cols()
)
return_data_count
species_id = 123
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'data'
return_format = 'CSV'
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count/species/{species_id}?format={return_format}"
),
col_types = readr::cols()
)
return_data_count
species_id = 762
start_date = '2019-01-01'
end_date = '2019-02-01'
region_type = 'country'
region_id = 'kenya'
return_type = 'data'
return_format = 'CSV'
return_data_count <- readr::read_csv(
glue::glue(
"http://api.adu.org.za/sabap2/v2/R/{start_date}/{end_date}/{region_type}/{region_id}/count/species/{species_id}?format={return_format}"
),
col_types = readr::cols()
)
return_data_count
extract_species(762)
test <- c("a", "b", "c")
test
paste(test, collapse = ",")
test <- "a"
paste(test, collapse = ",")
library(rabm)
devtools::load_all(".")
devtools::use_vignette("rabm-vignette")
install.packages("pkgdown")
library(rabm)
pkgdown::build_site()
library(rabm)
pkgdown::build_site()
library(rabm)
pkgdown::build_site()
library(rabm)
pkgdown::build_site()
library(rabm)
?find_species
library(rabm)
library(rabm)
library(rabm)
find_species(rabm::get_species_list("Kenya"), species_names = c("Bulbul"))
usethis::use_pipe()
devtools::document()
usethis::use_pipe()
devtools::document()
library(rabm)
find_species(rabm::get_species_list("Kenya"), species_names = c("Bulbul"))
usethis::use_pipe(export = TRUE)
?usethis::use_pipe(export = TRUE)
find_species(rabm::get_species_list("Kenya"), species_names = c("Bulbul"))
?usethis::use_pipe(export = TRUE)
usethis::use_pipe(export = TRUE)
devtools::document()
devtools::document()
devtools::load_all()
library(rabm)
rabm::get_species_list("Kenya")
library(rabm)
library(rabm)
library(rabm)
find_species(rabm::get_species_list("Kenya"), species_names = c("Bulbul"))
?pipe
pkgdown::build_home()
vignette("roxygen2")
usethis::use_pipe()
devtools::document()
find_species(rabm::get_species_list("Kenya"), species_names = c("Bulbul"))
find_species(rabm::get_species_list("Kenya"), species_names = c("Bulbul"))