Skip to content

Commit d1aedca

Browse files
Merge pull request #12 from BirdStudiesCanada/sandbox
# naturecounts 0.2.0 * Clarified access in `nc_counts()` * Added extra event columns to `format_zero_fill()` * No funny dates in Databases and no warnings for data frames when missing `survey_year`, `survey_month` or `survey_day` in `format_dates()`
2 parents ebe1b79 + 574d3f1 commit d1aedca

File tree

150 files changed

+14514
-2723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+14514
-2723
lines changed

.Rbuildignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
^\.Rproj\.user$
77
^RELEASE.R$
88
^pkgdown$
9+
^CODE_DESIGN.md$
10+
^\.travis\.yml$
911
^appveyor\.yml$

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: R
2+
cache: packages
3+
before_install:
4+
- sudo apt-get -y install libudunits2-dev gdal-bin libgdal1-dev libproj-dev
5+
env:
6+
global:
7+
secure: PpldXRYEeEjF0mmR3Kv04WJZiE3w7e48K2Bc5PkP/jK7rrqnswbrvNAe0nuchwEEd8ZruDnU0Ac5ptVOIXTLU2XJ8zF3ZAsIKHcSaGxoHG7Y33+vfcy3Zk4dor3bNC2pyTPCfdaoSU7rotcpmCWDPYBQMGZODBBPT5KkGO1e9vVrCPrTjR9WKORiIf1TkaiPLR4tgNHZFcvZ2yETIoFdyZ6Sjab/TMBrVHuNAK91L5yzgHrnyLl/jHd5Jscr6W+2JBUpQkpQeu8DI8MbHxFi5sMApLeVOMP5EYT5zXHccQJlOR0GpwcvDjAk0UnomyCjO5cLYjLWHItqsiMWkUX8fkQzIw8eQ1c/guLuUYlSVxYoJBsB8QNGI52U3HOCrNE5lPZyJkahc9vgYseClZJtRSUtN+EuQKYOUpiXPSSCu5V4P7zgp2zvfz5FKejYPlGzvtsYe4Yqd4d75nwqisKug9WcLt0ZoQi/v3Zr6e5O6g99yGlb+vUJwdjNLzo7xjWA5yz7pIuFzFyRBWUJKVWkFgggc7Mh856yWxFe6MtsmgF1Qz0TnBBRVl9cu4vKtFp0bNsas2Pe7uGCNbQwArRWXgGpmAjvNJ7FOZD0GZcMF0eoznbExdhq/CQXMcXKU8V4LCP6n80dKB03ID4iZFiYy2T2U4ahfwem0fT2ldOfK5Y=
8+
9+
r_build_args: "--no-build-vignettes"
10+
r_check_args: "--no-build-vignettes --ignore-vignettes"

CODE_DESIGN.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# naturecounts design principles
2+
3+
## Fields returned by the API
4+
5+
#### Desired behaviour
6+
Required and extra fields returned by the API are expected and treated, respectively, as follows:
7+
8+
Function | API Entry Point | Required fields | Extra fields
9+
------------------ |-------------------------- | -------------------------- | ------------
10+
`nc_count()` | `data/list_collections` | `collection`, `nrecords` | Ignored
11+
`nc_count()` | `data/list_permissions` | `collection`, `akn_level` | Ignored
12+
`nc_count()` | `metadata/collections` | `collection`, `akn_level` | Ignored
13+
`nc_permissions()` | `data/list_permissions` | `collection`, `akn_level` | Ignored
14+
`srv_auth()` | `data/authenticate` | `token` | Ignored
15+
`nc_data_dl()` | `data/release_request_id` | None (close request only) | Ignored
16+
`nc_data_dl()` | `data/get_data` | Any | Added
17+
`meta_XXX()` | `metadata/XXX` | Any | Added
18+
19+
#### What will break the package
20+
- Missing "Required fields"
21+
- Missing/renamed "container"
22+
Some API entry points return data in a container, for example,
23+
24+
- `data/get_data` -> `results` (holds main data)
25+
- `data/list_collections` -> `results` (holds counts); `request_id` (holds request id)
26+
- `data/list_requests` -> `requests` (holds details on individual requests)
27+
28+
If these "containers" change names, the package will break.
29+
30+
#### Coding Principles
31+
- In package, after accessing the API, explictly `select()` the fields/columns expected. This way extra fields won't break existing code
32+
- Missing fields **will** the code, but at least they will break the code early!
33+
- Do **not** do this for data downloads (i.e. `nc_single_dl()` under `nc_data_dl()`) or metadata downloads (i.e. `meta_XXX()`)
34+
- **Unless**, using a `meta_XXX()` download internally. Then always `select()` the fields required
35+
36+
## Testing
37+
- Tests are run using the user "testuser"
38+
- Locally password can be stored in .Renviron as naturecounts_testuser = PASSWORD
39+
- For remote testing, password is supplied as encrypted values

DESCRIPTION

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: naturecounts
22
Type: Package
33
Title: Access and download data on plant and animal populations from NatureCounts
4-
Version: 0.1.0
4+
Version: 0.2.0
55
Authors@R: c(
66
person("Steffi", "LaZerte", email = "[email protected]", role = "aut"),
77
person("Denis", "LePage", email = "[email protected]", role = c("aut", "cre")))
@@ -16,24 +16,28 @@ Depends:
1616
Imports:
1717
askpass (>= 1.1),
1818
DBI (>= 1.0.0),
19+
dbplyr (>= 1.4.4),
1920
dplyr (>= 0.7.8),
2021
httr (>= 1.4.0),
2122
jsonlite (>= 1.6),
2223
lubridate (>= 1.7.4),
2324
magrittr,
24-
purrr(>= 0.3.2),
25+
memoise (>= 1.1.0),
26+
purrr (>= 0.3.2),
2527
readr (>= 1.3.1),
2628
rlang (>= 0.1.2),
2729
RSQLite (>= 2.1.1),
2830
stringi (>= 1.2.4),
2931
stringr (>= 1.3.1),
30-
tidyr (>= 0.8.2)
32+
tidyr (>= 0.8.2),
33+
tidyselect (>= 1.0.0)
3134
Suggests:
3235
ggplot2,
3336
ggmap,
3437
here,
3538
knitr,
3639
mapview,
40+
rgeos,
3741
rnaturalearth,
3842
roxygen2,
3943
rmarkdown,
@@ -42,6 +46,6 @@ Suggests:
4246
testthat
4347
Language: en-US
4448
Roxygen: list(markdown = TRUE)
45-
RoxygenNote: 6.1.1
49+
RoxygenNote: 7.1.1
4650
URL: https://github.com/BirdStudiesCanada/naturecounts, https://naturecounts.ca
4751
VignetteBuilder: knitr

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export(meta_utm_squares)
2323
export(nc_count)
2424
export(nc_data_dl)
2525
export(nc_metadata)
26+
export(nc_permissions)
2627
export(nc_requests)
2728
export(region_search)
2829
export(search_region)

NEWS.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# naturecounts 0.2.0
2+
3+
* Clarified access in `nc_counts()`
4+
* Added extra event columns to `format_zero_fill()`
5+
* No funny dates in Databases and no warnings for data frames when missing
6+
`survey_year`, `survey_month` or `survey_day` in `format_dates()`
7+
18
# naturecounts 0.1.0
29

310
* Initial package development

R/checks.R

+33-9
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ site_type_check <- function(site_type) {
9595
projects_check <- function(project_ids, collections = NULL) {
9696
if(!is.null(project_ids)) {
9797
c1 <- meta_collections() %>%
98-
dplyr::filter(project_id %in% project_ids)
98+
dplyr::filter(.data$project_id %in% project_ids)
9999

100100
if(nrow(c1) == 0) {
101101
stop("'project_ids' must be either NULL or a vector of valid ",
@@ -106,14 +106,38 @@ projects_check <- function(project_ids, collections = NULL) {
106106
c1
107107
}
108108

109-
collections_check <- function(c) {
110-
c1 <- meta_collections() %>%
111-
dplyr::filter(collection %in% c)
109+
collections_check <- function(c, token = NULL) {
110+
c1 <- nc_permissions_internal(token) %>%
111+
dplyr::pull("collection")
112112

113-
if(!is.character(c) || nrow(c1) == 0) {
114-
stop("'collections' must be either NULL (for all collections) ",
115-
"or a character vector of valid 'collection' names specified ",
116-
"in 'meta_collections()'.", call. = FALSE)
113+
if(!is.null(token)) {
114+
c_req <- nc_requests_internal(token = token)
115+
if(!is.null(c_req)) {
116+
c1 <- c_req %>%
117+
dplyr::pull("collection") %>%
118+
append(c1) %>%
119+
unique()
120+
}
121+
}
122+
c1 <- c[!c %in% c1]
123+
124+
if(!is.null(c) && (!is.character(c) || length(c1) > 0)) {
125+
msg <- paste0("'collections' must be either NULL (return all collections) ",
126+
"or a \ncharacter vector of valid 'collection' names to which ",
127+
"'username' has access.\nSee 'vignette(\"Data Access\", ",
128+
"package = \"naturecounts\")' for details.")
129+
if(any(c1 %in% meta_collections()$collection)) {
130+
msg <- paste0(msg, "\nNo access to collection(s): ",
131+
paste0(c1[c1 %in% meta_collections()$collection],
132+
collapse = ", "))
133+
}
134+
if(any(!c1 %in% meta_collections()$collection)) {
135+
msg <- paste0(msg, "\nNo access or no collection: ",
136+
paste0(c1[!c1 %in% meta_collections()$collection],
137+
collapse = ", "))
138+
}
139+
140+
stop(msg, call. = FALSE)
117141
}
118142
c
119143
}
@@ -144,7 +168,7 @@ fields_set_check <- function(fields_set) {
144168

145169
fields_check <- function(fields) {
146170
f <- meta_bmde_fields(version = NULL) %>%
147-
dplyr::pull(local_name) %>%
171+
dplyr::pull(.data$local_name) %>%
148172
unique()
149173

150174
w <- fields[!fields %in% f]

R/common_docs.R

+35-25
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@
3131
#' @param verbose Logical. Show messages?
3232
#'
3333
#' @section NatureCounts account:
34-
#' All public data is available without a username/password. However, to
35-
#' access private/semi-public projects/collections you must [sign
36-
#' up](https://www.birdscanada.org/birdmon/default/register.jsp) for a free
37-
#' NatureCounts account and
38-
#' [register](https://www.birdscanada.org/birdmon/default/projects.jsp) for
39-
#' the projects you'd like to access. See the `request_id` section for more
40-
#' information.
34+
#' All public data is available with a username/password
35+
#' ([sign up](https://www.birdscanada.org/birdmon/default/register.jsp)
36+
#' for a free NatureCounts account). However, to access private/semi-public
37+
#' projects/collections you must request access. See the Access and
38+
#' `request_id`s section for more information.
4139
#'
4240
#' @section Species ids (`species`):
4341
#' Numeric species id codes can determined from the functions
44-
#' [search_species()] or [search_species_code()]. See
45-
#' also the [species codes](https://birdstudiescanada.github.io/naturecounts/articles/species-codes.html)
42+
#' [search_species()] or [search_species_code()]. See also the article on
43+
#' [species codes](https://birdstudiescanada.github.io/naturecounts/articles/species-codes.html)
4644
#' for more information.
4745
#'
4846
#' @section Day of Year (`doy`):
@@ -86,26 +84,38 @@
8684
#' Note that in all cases there are a set of fields/columns that are *always*
8785
#' returned, no matter what `fields_set` is used.
8886
#'
89-
#' @section `request_id`'s:
90-
#' There are two types of data requests: ones made through the api (i.e. this
91-
#' `naturecounts` R package) and those made through the online [Web Request
92-
#' Form](https://www.birdscanada.org/birdmon/default/searchquery.jsp). Each
93-
#' request generates a request id which identifies the filter set and
94-
#' collections requested.
87+
#' @section Access and `request_id`s:
9588
#'
96-
#' Requests made through this package via the [nc_data_dl()] function only
97-
#' return data the user has access to. If data was successfully downloaded, the
98-
#' user can see the request details with the [nc_requests()] function.
89+
#' Access to a data collection is either available as "full" or "by request".
90+
#' Use `nc_count(username = "USER", show = "all")`, to see the accessibility of
91+
#' collections.
9992
#'
100-
#' Requests made through the online [Web Request
101-
#' Form](https://www.birdscanada.org/birdmon/default/searchquery.jsp) may
102-
#' include collections to which the user does not have access. [nc_requests()]
103-
#' will list the requests along with the approval status (either 'approved' or
104-
#' 'pending').
93+
#' "Full" access means that data can be immediately requested directly through
94+
#' the `naturecounts` R package. "By request" means that a request must be
95+
#' [submitted
96+
#' online](https://www.birdscanada.org/birdmon/default/searchquery.jsp) and
97+
#' approved before the data can be downloaded through `naturecounts`.
10598
#'
106-
#' Any approved request id can be downloaded by supplying the `request_id` to
107-
#' [nc_data_dl()] (e.g., `nc_data_dl(request_id = 152000, username = "USER")`).
99+
#' This means that there are two types of data requests: ones made through this
100+
#' `naturecounts` R package (API requests) and those made through the online
101+
#' [Web Request
102+
#' Form](https://www.birdscanada.org/birdmon/default/searchquery.jsp) (Web
103+
#' requests). Every request (from either method) generates a `request_id` which
104+
#' identifies the filter set and collections requested. Details of all of
105+
#' requests can be reviewed with the [nc_requests()] function.
108106
#'
107+
#' To download data with "full" access, users can either specify filters, or if
108+
#' they are repeating a download, can use the `request_id` from [nc_requests()].
109+
#'
110+
#' Otherwise, if the user doesn't have "full" access, they must supply an
111+
#' approved `request_id` to the [nc_data_dl()] function (e.g.,
112+
#' `nc_data_dl(request_id = 152000, username = "USER")`). Use [nc_requests()] to
113+
#' see `request_id`s, filters, and approval status.
114+
#'
115+
#' Requests for "full" access to additional collections can be made online
116+
#' through the [Web Request
117+
#' Form](https://www.birdscanada.org/birdmon/default/searchquery.jsp) by
118+
#' checking the "Full access?" box in Step 2 of the form.
109119
#'
110120
#' @keywords internal
111121
#' @name args

R/database.R

+2-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
#'
1313
#' @keywords internal
1414
#'
15-
#' @examples
16-
#'
17-
#' \donttest{
18-
#' db_connect()
19-
#' }
2015

2116
db_connect <- function(name = paste0("./naturecounts_", Sys.Date()),
2217
verbose = TRUE) {
@@ -59,8 +54,8 @@ db_create_primary <- function(con, df, primary_key) {
5954
# This function is used internally to create a database for use by the db_create function
6055
db_create_empty <- function(con) {
6156
# Download and copy empty naturecounts table
62-
naturecounts <- nc_data_dl(collections = "RCBIOTABASE", species = 14280,
63-
years = 2010, username = "sample",
57+
naturecounts <- nc_data_dl(collections = "SAMPLE1", species = 14280,
58+
username = "sample",
6459
info = "nc: create database",
6560
verbose = FALSE)[0, ]
6661
db_create_primary(con, naturecounts, primary_key = keys$data)

0 commit comments

Comments
 (0)