Skip to content

Commit 2390115

Browse files
authored
Merge pull request #75 from RobLBaker/master
2 parents 1498df1 + 1350b3c commit 2390115

79 files changed

Lines changed: 4940 additions & 110 deletions

File tree

Some content is hidden

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

DESCRIPTION

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@ Imports:
4343
lifecycle
4444
RoxygenNote: 7.3.2
4545
Suggests:
46+
httptest,
4647
knitr,
48+
leaflet,
4749
rmarkdown,
48-
testthat (>= 3.0.0),
4950
sf,
50-
leaflet,
51-
stringr
51+
stringr,
52+
testthat (>= 3.0.0),
53+
withr,
54+
randgeo,
55+
mockr,
56+
EML
5257
VignetteBuilder: knitr
5358
URL: https://nationalparkservice.github.io/NPSutils/
5459
BugReports: https://github.com/nationalparkservice/NPSutils/issues

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# NPSutils 1.0.1 (development version)
2+
## 2025-05-08
3+
* add unit tests for all functions. Add packages necessary for unit tests to Suggests in DESCRIPTION file.
24

35
## 2025-03-25
46
* fix bug that caused some functions to fail to detect certain .csv files

R/getParkUnitInfo.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#' @export
1717
#' @examples
1818
#' \dontrun{
19-
#' get_unit_code("ROMO")
19+
#' get_unit_code("Rocky")
2020
#' }
2121
get_unit_code <- function(unit) { # input must have quotes to indicate strings
2222
# To do:

R/getReferenceInfo.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Get DS References for a park-species combination
22
#'
3-
#' @description \code{get_park_taxon_refs} returns a data frame of metadata for references in Data Store corresponding to a particular taxon at a park.
3+
#' @description \code{get_park_taxon_refs} returns a tibble of metadata for references in Data Store corresponding to a particular taxon at a park.
44
#' The resultant data frame is then usable by other functions to extract metadata from the records.
55
#'
66
#' Because `get_park_taxon_refs()` accesses restricted data on the secure DataStore server, results of these queries need to be considered for potential park-specific data protection protocols.
@@ -79,13 +79,13 @@ get_park_taxon_url <- function(park_code, taxon_code) {
7979
return(URLs)
8080
}
8181

82-
#' Get citation for Data Store holding info by HoldingID
82+
#' Get citation for Data Store holding info by reference ID
8383
#'
84-
#' @description \code{get_ref_info} returns a character string or a vector with information from one of the
84+
#' @description `get_ref_info` returns a character string or a vector with information from one of the
8585
#' metadata fields in a Data Store reference's associated xml file.
8686
#'
8787
#'
88-
#' @param holding_id The six-seven digit reference / holding ID number unique to the data store record.
88+
#' @param reference_id The six-seven digit reference ID number unique to the data store record.
8989
#' @param field is one of the following: "Title" returns the title of the data store reference as a string value;
9090
#' "Abstract" returns the abstract as a string value; "Citation" returns the citation as a string value, and "Keywords" returns a vector containing
9191
#' all keywords as character values.
@@ -96,10 +96,10 @@ get_park_taxon_url <- function(park_code, taxon_code) {
9696
#' \dontrun{
9797
#' get_ref_info(2266196, "Title")
9898
#' }
99-
get_ref_info <- function(holding_id, field) {
99+
get_ref_info <- function(reference_id, field) {
100100
url <- paste0(.ds_api(),
101101
"Profile/",
102-
holding_id)
102+
reference_id)
103103
DSReference <- httr::content(httr::GET(
104104
url, httr::authenticate(":", ":", "ntlm")
105105
))

R/get_data_packages.R

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,10 @@ get_data_packages <- function(reference_id,
3838
path = here::here(),
3939
force = FALSE,
4040
dev = FALSE) {
41-
# capture original working directory
42-
orig_wd <- getwd()
43-
# set directory back to original working directory on exit.
44-
on.exit(setwd(orig_wd), add = TRUE)
45-
# set wd to path; defaults to wd.
46-
setwd(path)
41+
4742
# create "data" directory, if necessary:
48-
if (!file.exists("data")) {
49-
dir.create("data")
43+
if (!file.exists(file.path(path, "data"))) {
44+
dir.create(file.path(path, "data"))
5045
}
5146

5247
# enforce proper specification of TRUE/FALSE parameters:
@@ -124,7 +119,7 @@ get_data_packages <- function(reference_id,
124119
}
125120

126121
# if necessary, create a package-specific directory within the /data:
127-
destination_dir <- paste("data/", reference_id[i], sep = "")
122+
destination_dir <- file.path(path, "data", reference_id[i])
128123
# if the directory already exists, prompt user to overwrite:
129124
if (force == FALSE) {
130125
if (file.exists(destination_dir)) {
@@ -230,9 +225,10 @@ get_data_packages <- function(reference_id,
230225
file_id <- xml[[j]]$resourceId
231226
rest_file_download_url <- paste0(rest_file_download_base, file_id)
232227

233-
download_file_path <- paste0(
234-
"data/",
235-
reference_id[i], "/",
228+
download_file_path <- file.path(
229+
path,
230+
"data",
231+
reference_id[i],
236232
download_filename
237233
)
238234
# download the file:
@@ -241,6 +237,7 @@ get_data_packages <- function(reference_id,
241237
rest_file_download_url,
242238
httr::timeout(300),
243239
httr::progress(),
240+
# during re-write use content not write_disk as write_disk is not well supported by httptest.
244241
httr::write_disk(download_file_path,
245242
overwrite = TRUE
246243
),
@@ -257,20 +254,11 @@ get_data_packages <- function(reference_id,
257254
# check to see if the downloaded file is a zip; unzip.
258255
if (tools::file_ext(tolower(download_filename)) == "zip") {
259256
utils::unzip(
260-
zipfile = paste0(
261-
"data\\",
262-
reference_id[i], "\\",
263-
download_filename
264-
),
265-
exdir = paste0("data\\", reference_id[i])
257+
zipfile = download_file_path,
258+
exdir = dirname(download_file_path)
266259
)
267260
# delete .zip file
268-
file.remove(paste0(
269-
"data/",
270-
reference_id[i],
271-
"/",
272-
download_filename
273-
))
261+
file.remove(download_file_path)
274262
if (force == FALSE) {
275263
cat("Unzipping ",
276264
crayon::blue$bold(download_filename),

R/load_core_metadata.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@
2020
#' df <- load_core_metadata(1234567)
2121
#' }
2222
#'
23-
load_core_metadata <- function(ds_ref, path = paste0(getwd(), "/data")){
23+
load_core_metadata <- function(ds_ref, path = here::here("data")){
2424
#construct path to downloaded data package:
2525
#capture current/original working directory:
26-
origin_wd <- getwd()
26+
27+
#origin_wd <- getwd()
2728
#set directory back to original working directory on exit.
28-
on.exit(setwd(origin_wd), add=TRUE)
29+
#on.exit(setwd(origin_wd), add=TRUE)
2930

30-
setwd(path)
31-
directory <- paste0(path, "/", ds_ref)
31+
#setwd(path)
32+
#directory <- paste0(path, "/", ds_ref)
3233

3334
#load metadata
34-
metadata <- DPchecker::load_metadata(directory = directory)
35+
#metadata <- DPchecker::load_metadata(directory = directory)
36+
metadata <- DPchecker::load_metadata(directory = here::here(path, ds_ref))
3537

3638
title <- EMLeditor::get_title(metadata)
3739
pub_date <- metadata$dataset$pubDate
@@ -72,7 +74,9 @@ load_core_metadata <- function(ds_ref, path = paste0(getwd(), "/data")){
7274
license_name <- metadata$dataset$licensed$licenseName
7375

7476
#get files lists:
75-
files <- list.files(path=directory, pattern = "*\\.csv", ignore.case = TRUE)
77+
#files <- list.files(path=directory, pattern = "*\\.csv", ignore.case = TRUE)
78+
files <- list.files(path=here::here(path, ds_ref),
79+
pattern = "*\\.csv", ignore.case = TRUE)
7680
file_names <- data.frame(files, files)
7781
file_names$eml_element <- "data_file"
7882
file_names <- file_names[,c(3,1,2)]

R/load_pkg_metadata.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' @description `load_pkg_metadata()` is essentially a wrapper around `DPchecker::load_metadata` with the directory structure pre-set to work well the default location that `get_data_package` stores downloaded data packages. If you did not use the default settings for `get_data_package` (or downloaded a data package manually) you may find it easier to adjust the directory structure pointing to your data package and load the metadata using `DPchecker::load_metadata()`. Much like `load_metadata`, `load_pkg_metadata` requires that there be a single .xml file in the data package directory, that the metadata file name end in *_metadata.xml, and that the file contain schema-valid EML metadata.
44
#'
5-
#' @param holding_id is a 6-7 digit number corresponding to the holding ID of the data package zip file. Your data should be in a directory that that has the holding ID as its name.
5+
#' @param reference_id is a 6-7 digit number corresponding to the reference ID of the data package on DataStore. Your data should be in a directory that that has the reference ID as its name.
66
#' @param directory String. Path to the data package directory, defaults to "data".
77
#'
88
#' @return one data frame to the global environment.
@@ -13,9 +13,10 @@
1313
#' \dontrun{
1414
#' load_pkg_metadata(2266200)
1515
#' }
16-
load_pkg_metadata <- function(holding_id, directory = "data") {
16+
load_pkg_metadata <- function(reference_id, directory = "data") {
1717

18-
meta <- DPchecker::load_metadata(directory = here::here("data", holding_id))
18+
meta <- DPchecker::load_metadata(directory = here::here(directory,
19+
reference_id))
1920

2021
return(invisible(meta))
2122
}

R/map_wkt.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
#' #map species observations - points only
2323
#' map_wkt(my_NPS_species_obs, wellknowntext = "footprintWKT", type = "points")
2424
#' }
25-
map_wkt <- function(df, wellknowntext = "footprintWKT", type = "all", remove.duplicates = TRUE) {
25+
map_wkt <- function(df,
26+
wellknowntext = "footprintWKT",
27+
type = "all",
28+
remove.duplicates = TRUE) {
2629
#filter to just wellknowntext column:
2730
wkt_grepl <- paste0('\\b', wellknowntext, '\\b')
2831
df <- df[grepl(wkt_grepl, colnames(df))]

R/rm_local_packages.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
#' #delete data packages from a "data" directory in a custom location:
2626
#' rm_local_packages(1234567, path = "C:/Users/username/Documents")
2727
#' }
28-
rm_local_packages <- function (reference_id, all = FALSE, path = here::here(), force = FALSE){
28+
rm_local_packages <- function (reference_id,
29+
all = FALSE,
30+
path = here::here(),
31+
force = FALSE){
2932
if(all == FALSE){
3033
for(i in seq_along(reference_id)){
3134
d_path <- paste0(path, "/data/", reference_id[i])

docs/404.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)