Code Snippets

Code snippets help with templating portions of code that you will be using regularly. To add your own, go to the toolbar ribbon at the top of your Rstudio screen and select:

Tools > Global Options... > Code > Edit Snippets > Add these chunks to the end of the file

More info can be found in this blog post by Mara Averick on how to add them: https://maraaverick.rbind.io/2017/09/custom-snippets-in-rstudio-faster-tweet-chunks-for-all/

Usual arcticdatautils ticket workflow

snippet ticket
    library(dataone)
    library(arcticdatautils)
    library(EML)
    
    cn <- CNode('PROD')
    adc <- getMNode(cn, 'urn:node:ARCTIC')
    
    rm <- "add your rm"
    pkg <- get_package(adc, rm)
    doc <- EML::read_eml(getObject(adc, pkg\$metadata))
    
    doc <- eml_add_publisher(doc)
    doc <- eml_add_entity_system(doc)
    
    eml_validate(doc)
    eml_path <- "eml.xml"   
    write_eml(doc, eml_path)

    #update <- publish_update(adc,
    #                                               metadata_pid = pkg\$metadata,
    #                                               resource_map_pid = pkg\$resource_map,
    #                                               metadata_path = eml_path,
    #                                               data_pids = pkg\$data,
    #                                               public = F)
                                                    
    #datamgmt::categorize_dataset(update\$metadata, c("theme1"), "Your Name")

The datapack ticket workflow

snippet datapack
    library(dataone)
    library(datapack)
    library(digest)
    library(uuid)
    library(dataone)
    library(arcticdatautils)
    library(EML)
    
    d1c <- D1Client("PROD", "urn:node:ARCTIC")
    packageId <- "id here"
    dp <- getDataPackage(d1c, identifier=packageId, lazyLoad=TRUE, quiet=FALSE)
    
    #get metadata id
    metadataId <- selectMember(dp, name="sysmeta@formatId", value="https://eml.ecoinformatics.org/eml-2.2.0")
    
    #edit the metadata
    doc <- read_eml(getObject(d1c@mn, metadataId))
    
    #add the publisher info
    doc <- eml_add_publisher(doc)
    doc <- eml_add_entity_system(doc)
    
    doc\$dataset\$project <- eml_nsf_to_project("nsf id here")
    
    #check and save the metadata
    eml_validate(doc)
    eml_path <- arcticdatautils::title_to_file_name(doc\$dataset\$title)
    write_eml(doc, eml_path)
    
    dp <- replaceMember(dp, metadataId, replacement=eml_path)
    
    #upload the dataset
    myAccessRules <- data.frame(subject="CN=arctic-data-admins,DC=dataone,DC=org", permission="changePermission") 
    packageId <- uploadDataPackage(d1c, dp, public=F, accessRules=myAccessRules, quiet=FALSE)
    #datamgmt::categorize_dataset("doi", c("theme1"), "Jasmine")

Quick way to give access to submitters to their datasets:

snippet access
    library(dataone)
    library(arcticdatautils)
    library(EML)

    cn <- CNode('PROD')
    adc <- getMNode(cn, 'urn:node:ARCTIC')

    rm <- "rm here"
    pkg <- get_package(adc, rm)

    set_access(adc, unlist(pkg), "orcid here")

Quick access to the usual code for common Solr queries:

snippet solr
    library(dataone)
    library(arcticdatautils)
    library(EML)

    cn <- CNode('PROD')
    adc <- getMNode(cn, 'urn:node:ARCTIC')
    
    result <- query(adc, list(q = "rightsHolder:*orcid.org/0000-000X-XXXX-XXXX* AND (*:* NOT obsoletedBy:*)",
                              fl = "identifier,rightsHolder,formatId, fileName, dateUploaded",
                              sort = 'dateUploaded+desc',
                              start ="0",
                              rows = "1500"),
                         as="data.frame")