---
title: "Advanced functions for package developers and contributors"
date: "Last updated `r Sys.Date()`"
output: 
    rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Advanced functions for package developers and contributors}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)
```


## Clone package

The `clone_package()` function clones a data package from one DataONE Member Node to another. It can also be used to copy an older version of a data package to the same Member Node in order to restore it, provided that the old package is then obsoleted by the copied version. Use the `copy_package()` function to copy a data package from one DataONE Member Node to another while generating new PIDs and new system metadata.

Example:

```{r}
cn_pull <- CNode("PROD")
mn_pull <- getMNode(cn_pull, "urn:node:ARCTIC")

cn_push <- CNode("STAGING")
mn_push <- getMNode(cn_push, "urn:node:mnTestARCTIC")

clone_package("resource_map_doi:10.18739/A2RZ6X",
              mn_pull,
              mn_push,
              add_access_to = arcticdatautils:::get_token_subject(),
              change_auth_node = TRUE)
```


## Get NSF awards

The `get_awards()` function uses the NSF API to get all records pertaining to the Arctic or Polar programs.

Example:

```{r}
all_awards <- get_awards()
new_awards <- get_awards(from_date = "01/01/2017")
```


## Which in EML

The `which_in_eml()` function returns indices within an EML list that contain an instance where `test == TRUE`.

Example:

```{r}
# Question: Which creators have a surName "Smith"?
n <- which_in_eml(eml@dataset@creator, "surName", "Smith")
# Answer: eml@dataset@creator[n]

# Question: Which dataTables have an entityName that begins with "2016"
n <- which_in_eml(eml@dataset@dataTable, "entityName", function(x) {grepl("^2016", x)})
# Answer: eml@dataset@dataTable[n]

# Question: Which attributes in dataTable[[1]] have a numberType "natural"?
n <- which_in_eml(eml@dataset@dataTable[[1]]@attributeList@attribute, "numberType", "natural")
# Answer: eml@dataset@dataTable[[1]]@attributeList@attribute[n]

# Question: Which dataTables have at least one attribute with a numberType "natural"?
n <- which_in_eml(eml@dataset@dataTable, "numberType", function(x) {"natural" %in% x})
# Answer: eml@dataset@dataTable[n]
```
