Skip to content

Commit 2845f90

Browse files
authored
Merge pull request #52 from RobLBaker/master
housekeeping updates
2 parents 5c08bfe + 7200afa commit 2845f90

Some content is hidden

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

71 files changed

+3028
-3769
lines changed

.Rbuildignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
^.*\.Rproj$
22
^\.Rproj\.user$
33
^LICENSE\.md$
4+
^_pkgdown\.yml$
5+
^docs$
6+
^pkgdown$

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ dataPackages/*
66
data/raw/*
77
data/raw/*/*
88
inst/doc
9-
data/
9+
data/
10+
docs

DESCRIPTION

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ Imports:
2929
readr,
3030
magrittr,
3131
crayon,
32-
stringr,
3332
leaflet,
3433
lifecycle,
3534
EMLeditor,
3635
DPchecker,
3736
here,
38-
jsonlite
39-
RoxygenNote: 7.2.3
37+
jsonlite,
38+
cli
39+
RoxygenNote: 7.3.2
4040
Suggests:
4141
knitr,
42-
rmarkdown
42+
rmarkdown,
43+
testthat (>= 3.0.0)
4344
VignetteBuilder: knitr
4445
URL: https://github.com/nationalparkservice/NPSutils
4546
BugReports: https://github.com/nationalparkservice/NPSutils/issues
4647
Roxygen: list(markdown = TRUE)
48+
Config/testthat/edition: 3

NAMESPACE

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(check_is_data_package)
4+
export(check_new_version)
5+
export(check_ref_exists)
6+
export(get_data_package)
37
export(get_data_packages)
8+
export(get_new_version_id)
49
export(get_park_code)
510
export(get_park_taxon_citations)
611
export(get_park_taxon_refs)
@@ -9,7 +14,7 @@ export(get_ref_info)
914
export(get_unit_code)
1015
export(get_unit_code_info)
1116
export(get_unit_info)
12-
export(load_EML_df)
17+
export(load_core_metadata)
1318
export(load_data_package)
1419
export(load_data_packages)
1520
export(load_domains)

NEWS.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# NPSutils 0.3.2 (in development)
2+
* Update readme to us pak for package installation instead of devtools.
3+
* Update _pkgdown.yml to use bootstrap 5
4+
* added helper functions for API requests and user input to facilitate unit testing.
5+
* refactored `get_data_packages()` to take advantage of new helper functions.
6+
* added `get_data_package()` which aliases `get_data_packages()` mostly because many people will want to load one data package and forget that the function is plural.
7+
* renamed `load_EML_df()` to `load_core_metadata()`.
8+
19
# NPSutils 0.3.1 "Ooh Aah Point"
210
* added private functions `.get_authors()` and `.get_contacts()` to retrieve authors and contacts (and emails) from EML
311
* added `load_EML_df()`, which retrieves commonly available metadata items from an EML-formatted R object and returns them as a single dataframe (for loading into Power BI)

R/datastore_interactions.R

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
2+
3+
4+
#' Check whether a reference exists on DataStore
5+
#'
6+
#' @description given a DataStore reference ID, the function will hit the DataStore API to determine whether the reference exists. If it cannot contact DataStore, it will stop with an error that states the connection failed.
7+
#'
8+
#' @param reference_id Integer. A 7-digit number indicating the datastore reference to be queried.
9+
#' @param secure Logical. Defaults to TRUE, indicating that the user is logged on to the NPS VPN (or is accessing the resource from an NPS office). Can be set to FALSE for non-NPS users. If `secure` is set to FALSE, `dev` is automatically set to FALSE.
10+
#' @param dev Logical. Defaults to FALSE, indicating that the user wishes to work with the production version of DataStore. Can be set to TRUE if the user wants to work with the development environment on DataStore. Setting dev = TRUE will only work if the user has the appropriate permissions. Requires secure = TRUE.
11+
#'
12+
#' @return logical. TRUE means the reference exists, false means it does not exist.
13+
#' @export
14+
#'
15+
#' @examples
16+
#' \dontrun{
17+
#' check_ref_exists(1234567)
18+
#' check_ref_exists(1234567, secure = FALSE)
19+
#' }
20+
check_ref_exists <- function (reference_id, secure = TRUE, dev = FALSE) {
21+
22+
test_req <- NULL
23+
if (secure == FALSE) {
24+
#using secure api should be an option though...
25+
url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id)
26+
}
27+
if (secure == TRUE) {
28+
if (dev == FALSE) {
29+
url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id)
30+
}
31+
if (dev == TRUE) {
32+
url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id)
33+
}
34+
}
35+
test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm"))
36+
status_code <- httr::stop_for_status(test_req)$status_code
37+
#if API call fails, alert user and remind them to log on to VPN:
38+
if (!status_code == 200) {
39+
stop("DataStore connection failed. Are you logged in to the VPN?\n")
40+
}
41+
42+
#get API request info:
43+
ref_data <- jsonlite::fromJSON(httr::content(test_req, "text"))
44+
45+
#if an invalid reference number was supplied (no reference found):
46+
if(length(ref_data) == 0){
47+
return(FALSE)
48+
} else {
49+
return (TRUE)
50+
}
51+
}
52+
53+
#' Check whether reference is a data package
54+
#'
55+
#' @description The function tests whether a valid DataStore reference is a data package or not. If it is a data package, the value "TRUE" is returned. If it is not a data package, the value FALSE is returned. The default setting assumes the user is logged on to the VPN (secure = TRUE), but this can be set to secure = FALSE for non-NPS users. This is a relatively simple helper function that will not test whether a reference exists and may produce unexpected errors if the reference does not exist or is not accessible. You are advised to run `check_reference_exists()` prior to using this function.
56+
#'
57+
#' @inheritParams check_ref_exists
58+
#'
59+
#' @return Logical.FALSE if the DataStore reference is not a data package. TRUE if the DataStore reference is a data package.
60+
#' @export
61+
#'
62+
#' @examples
63+
#' \dontrun{
64+
#' check_is_data_package(1234567)
65+
#' check_is_data_package(1234567, secure = FALSE)
66+
#' }
67+
check_is_data_package <- function(reference_id, secure = TRUE, dev = FALSE) {
68+
test_req <- NULL
69+
70+
test_req <- NULL
71+
if (secure == FALSE) {
72+
#using secure api should be an option though...
73+
url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id)
74+
}
75+
if (secure == TRUE) {
76+
if (dev == FALSE) {
77+
url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id)
78+
}
79+
if (dev == TRUE) {
80+
url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id)
81+
}
82+
}
83+
84+
test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm"))
85+
status_code <- httr::stop_for_status(test_req)$status_code
86+
#if API call fails, alert user and remind them to log on to VPN:
87+
if (!status_code == 200) {
88+
stop("DataStore connection failed. Are you logged in to the VPN?\n")
89+
}
90+
91+
#get reference info:
92+
ref_data <- jsonlite::fromJSON(httr::content(test_req, "text"))
93+
ref_type <- ref_data$referenceType
94+
95+
#test whether it is a data package or not:
96+
if (!identical(ref_type, "Data Package")) {
97+
return(FALSE)
98+
} else {
99+
return(TRUE)
100+
}
101+
}
102+
103+
104+
#' Checks whether a reference has a more recent version
105+
#'
106+
#' @description
107+
#' This function tests whether an existing and accessible DataStore reference
108+
#' has a newer version or not. If a newer version exists, the value "TRUE" is returned. If a newer version does not exist, the value FALSE is returned. The default setting assumes the user is logged on to the VPN (secure = TRUE), but this can be set to secure = FALSE for non-NPS users. This is a relatively simple helper function that will not test whether a reference exists and may produce unexpected errors if the reference does not exist or is not accessible. You are advised to run `check_reference_exists()` prior to using this function.
109+
#'
110+
#' @inheritParams check_ref_exists
111+
#'
112+
#' @return Logical. TRUE if a newer version exists, FALSE if a newer version
113+
#' does not exist.
114+
#' @export
115+
#'
116+
#' @examples
117+
#' \dontrun{
118+
#' check_new_version(1234567)
119+
#' check_new_version(1234567, secure = FALSE)
120+
#' }
121+
check_new_version <- function(reference_id, secure = TRUE, dev = FALSE) {
122+
test_req <- NULL
123+
124+
test_req <- NULL
125+
if (secure == FALSE) {
126+
#using secure api should be an option though...
127+
url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id)
128+
}
129+
if (secure == TRUE) {
130+
if (dev == FALSE) {
131+
url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id)
132+
}
133+
if (dev == TRUE) {
134+
url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id)
135+
}
136+
}
137+
138+
test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm"))
139+
status_code <- httr::stop_for_status(test_req)$status_code
140+
#if API call fails, alert user and remind them to log on to VPN:
141+
if (!status_code == 200) {
142+
stop("DataStore connection failed. Are you logged in to the VPN?\n")
143+
}
144+
145+
#get request info:
146+
ref_data <- jsonlite::fromJSON(httr::content(test_req, "text"))
147+
#check for a newer version:
148+
version <-ref_data$mostRecentVersion
149+
150+
#test for newer version:
151+
if (!is.na(version)) {
152+
return(TRUE)
153+
} else {
154+
return(FALSE)
155+
}
156+
}
157+
158+
#' Get newest DataStore reference version
159+
#'
160+
#' @description If a DataStore reference has been versioned, `get_new_version_id()` will return the DataStore reference ID for the newest version. If the reference has not been versioned, the function returns NULL.
161+
#'
162+
#' @inheritParams check_ref_exists
163+
#'
164+
#' @return Integer. A 7-digit integer corresponding to the DataStore reference ID for the newest version of the supplied DataStore reference. If the supplied reference has not been versioned, NULL is returned.
165+
#' @export
166+
#'
167+
#' @examples
168+
#' \dontrun{
169+
#' get_new_version_id(1234567)
170+
#' get_new_version_id(1234567, secure = FALSE)
171+
#' }
172+
get_new_version_id <- function(reference_id, secure = TRUE, dev = FALSE) {
173+
#gets a newer version (assuming a newer version exists)
174+
test_req <- NULL
175+
176+
test_req <- NULL
177+
if (secure == FALSE) {
178+
#using secure api should be an option though...
179+
url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id)
180+
}
181+
if (secure == TRUE) {
182+
if (dev == FALSE) {
183+
url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id)
184+
}
185+
if (dev == TRUE) {
186+
url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id)
187+
}
188+
}
189+
test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm"))
190+
status_code <- httr::stop_for_status(test_req)$status_code
191+
#if API call fails, alert user and remind them to log on to VPN:
192+
if (!status_code == 200) {
193+
stop("DataStore connection failed. Are you logged in to the VPN?\n")
194+
}
195+
196+
#get request info:
197+
ref_data <- jsonlite::fromJSON(httr::content(test_req, "text"))
198+
#check for a newer version:
199+
version <-ref_data$mostRecentVersion
200+
201+
if (!is.na(version)) {
202+
return(version)
203+
}
204+
if (is.na(version)) {
205+
return(NULL)
206+
}
207+
}

R/getParkUnitInfo.R

+18-15
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ get_unit_info <- function(code = NULL,
124124
state = NULL) { # input must have quotes to indicate strings
125125
f <- file.path(tempdir(), "irmadownload.xml")
126126
if (!file.exists(f)) {
127-
curl::curl_download("https://irmaservices.nps.gov/v2/rest/unit/", f) # access all park codes from NPS xml file
127+
# access all park codes from NPS xml file
128+
curl::curl_download("https://irmaservices.nps.gov/v2/rest/unit/", f)
128129
}
129130
result <- XML::xmlParse(file = f)
130131
dat <- XML::xmlToDataFrame(result) # xml to dataframe
@@ -144,38 +145,40 @@ get_unit_info <- function(code = NULL,
144145
# check UnitCycle:
145146
if (!is.null(life_cycle)) {
146147
# LifeCycle<-tolower(LifeCycle)
147-
# if( (LifeCycle == "active") + (LifeCycle == "inactive") + (LifeCycle=="pending") < 1)
148+
# if( (LifeCycle == "active") + (LifeCycle == "inactive")\
149+
# (LifeCycle=="pending") < 1)
148150
# stop("LifeCycle must be \"Active\", \"Inactive\", or \"Pending\"")
149151

150152
names <- paste0("\\<", life_cycle, "\\>")
151-
dat <- dat %>% dplyr::filter(grepl(names, UnitLifecycle, ignore.case = TRUE))
153+
dat <- dat %>% dplyr::filter(grepl(names, UnitLifecycle,
154+
ignore.case = TRUE))
152155
}
153-
154156
# check Park Name:
155157
if (!is.null(park)) {
156-
dat <- dat %>% dplyr::filter(grepl(park, FullName, ignore.case = TRUE))
158+
dat <- dat %>% dplyr::filter(grepl(park, FullName,
159+
ignore.case = TRUE))
157160
}
158-
159161
# Network Code:
160162
if (!is.null(network_code)) {
161-
dat <- dat %>% dplyr::filter(grepl(network_code, Network, ignore.case = TRUE))
163+
dat <- dat %>% dplyr::filter(grepl(network_code, Network,
164+
ignore.case = TRUE))
162165
}
163-
164166
# Network Name:
165167
if (!is.null(net_name)) {
166-
dat <- dat %>% dplyr::filter(grepl(net_name, NetworkName, ignore.case = TRUE))
168+
dat <- dat %>% dplyr::filter(grepl(net_name, NetworkName,
169+
ignore.case = TRUE))
167170
}
168-
169171
if (!is.null(region_abb)) {
170-
dat <- dat %>% dplyr::filter(grepl(region_abb, Region, ignore.case = TRUE))
172+
dat <- dat %>% dplyr::filter(grepl(region_abb, Region,
173+
ignore.case = TRUE))
171174
}
172-
173175
if (!is.null(region)) {
174-
dat <- dat %>% dplyr::filter(grepl(region, RegionName, ignore.case = TRUE))
176+
dat <- dat %>% dplyr::filter(grepl(region, RegionName,
177+
ignore.case = TRUE))
175178
}
176-
177179
if (!is.null(state)) {
178-
dat <- dat %>% dplyr::filter(grepl(state, StateCodes, ignore.case = TRUE))
180+
dat <- dat %>% dplyr::filter(grepl(state, StateCodes,
181+
ignore.case = TRUE))
179182
}
180183
return(dat) # return park info
181184
}

0 commit comments

Comments
 (0)