Skip to content

Commit

Permalink
set_producing_units bug fix (#17)
Browse files Browse the repository at this point in the history
* add testthat for unit testing

* add testthat for unit testing

* update function names to tidyverse style guide: public functions in snake case. Private functions prefixed by .

* update function name and documentation conform to tidyverse snake case style guide

* remove old function name man files

* initial commit snake case function naming

* snake case function naming initial commit

* remove old function name man files

* initial commit snake case function names

* set_DOI to set_doi

* set_DOI to set_doi

* write.readMe to write_readme

* arguments to snake case

* arguments to snake case

* arguments to snake case

* set_DOI to set_doi

* delete man file for old read me function name

* write_readme initial commit

* Version to 0.0.1.1 - all function and argument names changed to snake case.

* delete this random file

* rename internal function objects using snake_case

* enforce tidyverse style via styler

* add pkgdown

* add pkdown files

* add github.io url

* update examples for snake case function names

* snake case function names via knit

* initial commit

* remove blank spaces

* remove 'docs'

* initial commit via pkgdown

* initial commit via pkgdown

* docs added via pkgdown

* fix For_or_by_nps

* set_park_units now works if there are no previous geographic coverage elements, if there is 1 or if there are more than 1 pre-existing geographic coverages.
  • Loading branch information
RobLBaker authored Oct 27, 2022
1 parent 892bf78 commit 32f8cdc
Show file tree
Hide file tree
Showing 81 changed files with 15,256 additions and 1,092 deletions.
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
^\.Rproj\.user$
^README\.Rmd$
^LICENSE\.md$
^_pkgdown\.yml$
^docs$
^pkgdown$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
46 changes: 46 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
release:
types: [published]
workflow_dispatch:

name: pkgdown

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/[email protected]
with:
clean: false
branch: gh-pages
folder: docs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.Rproj.user
R/EML_editor.R
.Rhistory
docs
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Imports:
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
URL: https://roblbaker.github.io/EMLeditor/
102 changes: 60 additions & 42 deletions R/editEMLfunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ set_doi<-function(eml_object, ds_ref, NPS=TRUE){

#If there is a DOI, find the correct doi by searching for the text "doi: ".
else{
mylist<-NULL
my_list<-NULL

#hopefully deals with case when there are multiple DOIs specified under alternateIdentifier tags. Haven't run into this yet and so this remains untested.
if(length(doc)>1){
for(i in seq_along(doc)){
if(stringr::str_detect(doc[i], "doi:" )){
mylist<-append(mylist, doc[i])
my_list<-append(my_list, doc[i])
}
}
}
#if there is only one alternateIdentifier:
else{
mylist<-doc
my_list<-doc
}
doi<-mylist[[1]]
doi<-my_list[[1]]

#If a DOI exists, ask the user what to do about it:
var1<-readline(prompt=cat("Your EML already has a DOI specified in the <alternateIdentifier> tag:\n\n", doi, "\n\nEnter 1 to retain this DOI\nEnter 2 to overwrite this DOI"))
Expand Down Expand Up @@ -203,21 +203,41 @@ set_park_units<-function(eml_object, park_units, NPS=TRUE){

#if there are already geographicCoverage(s)
else{
mylist<-NULL
#ditch the '@context' list from the geographicCoverage:
for(i in seq_along(names(doc))){
if(!names(doc)[i]=='@context'){
mylist<-append(mylist, doc[i])
}
}

#remove @context from list
my_list<-within(doc, rm("@context"))

#remove names from list (critical for writing back to xml)
names(mylist)<-NULL
names(my_list)<-NULL

#if there is more than 1 geo coverage:
if(length(my_list>2)){

#combine new and old geo coverages (new always at the top!)
mylist<-append(unit_list, mylist)
#combine new and old geo coverages (new always at the top!)
my_list<-append(unit_list, my_list)

#write over the existing geographic coverage
eml_object$dataset$coverage$geographicCoverage<-mylist
#write over the existing geographic coverage
eml_object$dataset$coverage$geographicCoverage<-my_list
}

#if there is only one geo coverage:
if(length(my_list==2)){

geocov2 <- EML::eml$geographicCoverage(geographicDescription =
doc$geographicDescription,
boundingCoordinates = EML::eml$boundingCoordinates(
northBoundingCoordinate =
doc$boundingCoordinates$northBoundingCoordinate,
eastBoundingCoordinate =
doc$boundingCoordinates$eastBoundingCoordinate,
southBoundingCoordinate =
doc$boundingCoordinates$southBoundingCoordinate,
westBoundingCoordinate =
doc$boundingCoordinates$westBoundingCoordinate))

#add park unit connections and existing geo coverage (park units always on top!)
eml_object$dataset$coverage$geographicCoverage <- list(geocov, geocov2)
}
}

#Set NPS publisher, if it doesn't already exist
Expand Down Expand Up @@ -259,46 +279,46 @@ set_cui<-function(eml_object, cui_code=c("PUBFUL", "PUBVER", "NOCON", "DL ONLY",
cui_code<-match.arg(cui_code)

#Generate new CUI element for additionalMetadata
myCUI<-list(metadata=list(CUI=cui_code), id="CUI")
my_cui<-list(metadata=list(CUI=cui_code), id="CUI")

#get existing additionalMetadata elements:
doc<-EML::eml_get(eml_object, "additionalMetadata")

#if no prior additionalMetadata elements, add CUI to additionalMetadata:
if(sum(names(doc)!="@context")==0){
eml_object$additionalMetadata<-myCUI
eml_object$additionalMetadata<-my_cui
}

#if additionalMetadata already exists:
if(sum(names(doc)!="@context")>0){
mylist<-NULL
my_list<-NULL
#ditch the '@context' list from doc:
for(i in seq_along(names(doc))){
if(!names(doc)[i]=='@context' && !names(doc)[i]=="id"){
mylist<-append(mylist, doc[i])
my_list<-append(my_list, doc[i])
}
x<-length(mylist)
x<-length(my_list)
}

#Is CUI already specified?
existCUI<-NULL
exist_cui<-NULL
for(i in seq_along(doc)){
if(suppressWarnings(stringr::str_detect(doc[i], "CUI"))){
existCUI<-"CUI"
exist_cui<-"CUI"
}
}

#If existing CUI, stop.
if(!is.null(existCUI)){
if(!is.null(exist_cui)){
stop("CUI has already been specified")
}
#If no existing CUI, add it in:
if(is.null(existCUI)){
if(is.null(exist_cui)){
if(x==1){
eml_object$additionalMetadata<-list(myCUI, eml_object$additionalMetadata)
eml_object$additionalMetadata<-list(my_cui, eml_object$additionalMetadata)
}
if(x>1){
eml_object$additionalMetadata[[x+1]]<-myCUI
eml_object$additionalMetadata[[x+1]]<-my_cui
}
}
}
Expand Down Expand Up @@ -463,10 +483,8 @@ set_lit<-function(eml_object, bibtex_file, NPS=TRUE){
#' @examples
#' \dontrun{
#' prod_units<-c("ABCD", "EFGH")
#' set_producing_units(eml_object, prod_units
#'
#' set_producing_units(eml_object, prod_units)
#' set_producing_units(eml_object, c("ABCD", "EFGH"))
#'
#' set_producing_units(eml_object, "ABCD")
#' }
set_producing_units<-function(eml_object, prod_units, NPS=TRUE){
Expand Down Expand Up @@ -575,8 +593,8 @@ set_language<-function(eml_object, lang, NPS=TRUE){
#if the language is already specified in the metadata:
else{
if(nchar(lng)==3){
fulllang<-dplyr::filter(langcodes, Alpha_3_B==lng)[[4]]
cat("The current language is set to ", crayon::blue$bold(lng),", the ISO 639-2 code for ", fulllang, ".", sep="")
full_lang<-dplyr::filter(langcodes, Alpha_3_B==lng)[[4]]
cat("The current language is set to ", crayon::blue$bold(lng),", the ISO 639-2 code for ", full_lang, ".", sep="")
}
else{
cat("The current language is set to ", crayon::blue$bold(lng),".", sep="")
Expand Down Expand Up @@ -616,7 +634,7 @@ set_language<-function(eml_object, lang, NPS=TRUE){
#' @details set_protocol requires that you have your protocols and projects organized in a specific fashion in DataStore. Errors generated by this function my stem from either a protocol that has not been published (or is not publicly available) or an obsolete protocol/project organization within DataStore.
#'
#' @param eml_object is an R object imported (typically from an EML-formatted .xml file) using EmL::read_eml(<filename>, from="xml").
#' @param protocol_ID a string. The 7-digit number identifying the protocol in DataStore under which the data were collected.
#' @param protocol_id a string. The 7-digit number identifying the DataStore reference number for the Project that describes your inventory or monitoring project.
#' @param NPS Logical. Checks EML for NPS publisher info and injects it if publisher is empty. If publisher already exists, does nothing. If you are not publishing with NPS, set to FALSE. If NPS=TRUE, the originatingAgency will be set to NPS and the field that maps to DataStore's "by or for NPS" will be set to TRUE.
#'
#' @return
Expand All @@ -627,32 +645,32 @@ set_language<-function(eml_object, lang, NPS=TRUE){
#' set_protocol(eml_object, 2222140)
#' }
#'
set_protocol<-function(eml_object, protocol_ID, NPS=TRUE){
set_protocol<-function(eml_object, protocol_id, NPS=TRUE){

#get data to construct project:

#get protocol profile via rest services:
ds_reference <- httr::content(httr::GET(paste0("https://irmaservices.nps.gov/datastore/v4/rest/Profile/",protocol_ID)))
ds_reference <- httr::content(httr::GET(paste0("https://irmaservices.nps.gov/datastore/v4/rest/Profile/",protocol_id)))

#extract project title
projTitle<-ds_reference$bibliography$title
proj_title<-ds_reference$bibliography$title

#generate URL for the DataStore landing page:
URL<-paste0("https://irma.nps.gov/DataStore/Reference/Profile/", protocol_ID)
url<-paste0("https://irma.nps.gov/DataStore/Reference/Profile/", protocol_id)

#get DataStore ref number for the organization Name:
ref<-ds_reference$series$referenceId

#rest services call to get organization name info:
orgName<-httr::content(httr::GET(paste0("https://irmaservices.nps.gov/datastore/v4/rest/Profile/", ref)))$bibliography$title
org_name<-httr::content(httr::GET(paste0("https://irmaservices.nps.gov/datastore/v4/rest/Profile/", ref)))$bibliography$title

#Construct a project to inject into EML. Note 'role' is required but not sure what to put there.
#Also i find it confusing that onlineURL references projTitle not orgName but hopefully we will hash that out soon.

proj<-list(title=projTitle,
personnel=list(organizationName=orgName,
onlineUrl=URL,
role="contentContainer"))
proj<-list(title=proj_title,
personnel=list(organizationName=org_name,
onlineUrl=url,
role="originator"))

#get existing project (if any)
doc<-eml_object$dataset$project
Expand Down
Loading

0 comments on commit 32f8cdc

Please sign in to comment.