Learn R Programming


output: md_document: variant: gfm

dataspice

The goal of dataspice is to make it easier for researchers to create basic, lightweight, and concise metadata files for their datasets by editing the kind of files they’re probably most familiar with: CSVs. To spice up their data with a dash of metadata. These metadata files can then be used to:

  • Make useful information available during analysis.
  • Create a helpful dataset README webpage for your data similar to how pkgdown creates websites for R packages.
  • Produce more complex metadata formats for richer description of your datasets and to aid dataset discovery.

Metadata fields are based on Schema.org/Dataset and other metadata standards and represent a lowest common denominator which means converting between formats should be relatively straightforward.

Example

An basic example repository for demonstrating what using dataspice might look like can be found at https://github.com/amoeba/dataspice-example. From there, you can also check out a preview of the HTML dataspice generates at https://amoeba.github.io/dataspice-example and how Google sees it at https://search.google.com/test/rich-results?url=https%3A%2F%2Famoeba.github.io%2Fdataspice-example%2F.

A much more detailed example has been created by Anna Krystalli at https://annakrystalli.me/dataspice-tutorial/ (GitHub repo).

Installation

You can install the latest version from CRAN:

install.packages("dataspice")

Workflow

create_spice()
# Then fill in template CSV files, more on this below
write_spice()
build_site() # Optional

Create spice

create_spice() creates template metadata spreadsheets in a folder (by default created in the data folder in the current working directory).

The template files are:

  • biblio.csv - for title, abstract, spatial and temporal coverage, etc.
  • creators.csv - for data authors
  • attributes.csv - explains each of the variables in the dataset
  • access.csv - for files, file types, and download URLs (if appropriate)

Fill in templates

The user needs to fill in the details of the four template files. These csv files can be directly modified, or they can be edited using either the associated helper function and/or Shiny app.

Helper functions

  • prep_attributes() populates the fileName and variableName columns of the attributes.csv file using the header row of the data files.

  • prep_access() populates the fileName, name and encodingFormat columns of the access.csv file from the files in the folder containing the data.

To see an example of how prep_attributes() works, load the data files that ship with the package:

data_files <- list.files(system.file("example-dataset/", package = "dataspice"),
  pattern = ".csv",
  full.names = TRUE
)

This function assumes that the metadata templates are in a folder called metadata within a data folder.

attributes_path <- file.path("data", "metadata", "attributes.csv")

Using purrr::map(), this function can be applied over multiple files to populate the header names

data_files %>%
  purrr::map(~ prep_attributes(.x, attributes_path),
    attributes_path = attributes_path
  )

The output of prep_attributes() has the first two columns filled out:

Shiny helper apps

Each of the metadata templates can be edited interactively using a Shiny app:

  • edit_attributes() opens a Shiny app that can be used to edit attributes.csv. The Shiny app displays the current attributes table and lets the user fill in an informative description and units (e.g. meters, hectares, etc.) for each variable.
  • edit_access() opens an editable version of access.csv
  • edit_creators() opens an editable version of creators.csv
  • edit_biblio() opens an editable version of biblio.csv

Remember to click on Save when finished editing.

Completed metadata files

The first few rows of the completed metadata tables in this example will look like this:

access.csv has one row for each file

fileNamenamecontentUrlencodingFormat
StockInfo.csvStockInfo.csvNACSV
BroodTables.csvBroodTables.csvNACSV
SourceInfo.csvSourceInfo.csvNACSV

attributes.csv has one row for each variable in each file

fileNamevariableNamedescriptionunitText
BroodTables.csvStock.IDUnique stock identifierNA
BroodTables.csvSpeciesspecies of stockNA
BroodTables.csvStockStock name, generally river where stock is foundNA
BroodTables.csvOcean.RegionOcean regionNA
BroodTables.csvRegionRegion of stockNA
BroodTables.csvSub.RegionSub.Region of stockNA

biblio.csv is one row containing descriptors including spatial and temporal coverage

titledescriptiondatePublishedcitationkeywordslicensefundergeographicDescriptionnorthBoundCoordeastBoundCoordsouthBoundCoordwestBoundCoordwktStringstartDateendDate
Compiled annual statewide Alaskan salmon escapement counts, 1921-2017The number of mature salmon migrating from the marine environment to freshwater streams is defined as escapement. Escapement data are the enumeration of these migrating fish as they pass upstream, …2018-02-12 08:00:00NAsalmon, alaska, escapementNANANA78-13147-171NA1921-01-01 08:00:002017-01-01 08:00:00

creators.csv has one row for each of the dataset authors

idnameaffiliationemail
NAJeanette ClarkNational Center for Ecological Analysis and Synthesisjclark@nceas.ucsb.edu
NARich,BrennerAlaska Department of Fish and Gamerichard.brenner.alaska.gov

Save JSON-LD file

write_spice() generates a json-ld file (“linked data”) to aid in dataset discovery, creation of more extensive metadata (e.g. EML), and creating a website.

Here’s a view of the dataspice.json file of the example data:

Build website

  • build_site() creates a bare-bones index.html file in the repository docs folder with a simple view of the dataset with the metadata and an interactive map. For example, this repository results in this website

Convert to EML

The metadata fields dataspice uses are based largely on their compatibility with terms from Schema.org. However, dataspice metadata can be converted to Ecological Metadata Language (EML), a much richer schema. The conversion isn’t perfect but dataspice will do its best to convert your dataspice metadata to EML:

library(dataspice)

# Load an example dataspice JSON that comes installed with the package
spice <- system.file(
  "examples", "annual-escapement.json",
  package = "dataspice"
)

# Convert it to EML
eml_doc <- spice_to_eml(spice)
#> Warning: variableMeasured not crosswalked to EML because we don't have enough
#> information. Use `crosswalk_variables` to create the start of an EML attributes
#> table. See ?crosswalk_variables for help.
#> You might want to run EML::eml_validate on the result at this point and fix what validations errors are produced. You will commonly need to set `packageId`, `system`, and provide `attributeList` elements for each `dataTable`.

You may receive warnings depending on which dataspice fields you filled in and this process will very likely produce an invalid EML record which is totally fine:

library(EML)
#> 
#> Attaching package: 'EML'
#> The following object is masked from 'package:magrittr':
#> 
#>     set_attributes

eml_validate(eml_doc)
#> [1] FALSE
#> attr(,"errors")
#> [1] "Element '{https://eml.ecoinformatics.org/eml-2.2.0}eml': The attribute 'packageId' is required but missing."                                  
#> [2] "Element '{https://eml.ecoinformatics.org/eml-2.2.0}eml': The attribute 'system' is required but missing."                                     
#> [3] "Element 'dataTable': Missing child element(s). Expected is one of ( physical, coverage, methods, additionalInfo, annotation, attributeList )."
#> [4] "Element 'dataTable': Missing child element(s). Expected is one of ( physical, coverage, methods, additionalInfo, annotation, attributeList )."
#> [5] "Element 'dataTable': Missing child element(s). Expected is one of ( physical, coverage, methods, additionalInfo, annotation, attributeList )."

This is because some fields in dataspice store information in different structures and because EML requires many fields that dataspice doesn’t have fields for. At this point, you should look over the validation errors produced by EML::eml_validate and fix those. Note that this will likely require familiarity with the EML Schema and the EML package.

Once you’re done, you can write out an EML XML file:

out_path <- tempfile()
write_eml(eml_doc, out_path)
#> NULL

Convert from EML

Like converting dataspice to EML, we can convert an existing EML record to a set of dataspice metadata tables which we can then work from within dataspice:

library(EML)

eml_path <- system.file("example-dataset/broodTable_metadata.xml", package = "dataspice")
eml <- read_eml(eml_path)
# Creates four CSVs files in the `data/metadata` directory
my_spice <- eml_to_spice(eml, "data/metadata")

Resources

A few existing tools & data standards to help users in specific domains:

…And others indexed in Fairsharing.org & the RDA metadata directory.

Code of Conduct

Please note that this package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Contributors

This package was developed at rOpenSci’s 2018 unconf by (in alphabetical order):

Copy Link

Version

Install

install.packages('dataspice')

Monthly Downloads

222

Version

1.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bryce Mecum

Last Published

May 16th, 2021

Functions in dataspice (1.1.0)

edit_attributes

Shiny apps for editing dataspice metadata tables
eml_to_spice

Create dataspice metadata tables from EML
edit_creators

Shiny apps for editing dataspice metadata tables
serve_site

Serve site
validate_file_paths

Validate file paths
write_jsonld

Write a list out as object to JSON-LD
spice_to_eml

Convert dataspice metadata to EML
validate_biblio

Validate biblio.csv
prep_access

Prepare access
prep_attributes

Prepare attributes
build_site

Build a dataspice site
es_access

Get access from EML
validate_creators

Validate creators.csv
validate_attributes

Validate attributes.csv
dataspice-package

dataspice: Create Lightweight Schema.org Descriptions of Data
validate_access

Validate access.csv
edit_access

Shiny apps for editing dataspice metadata tables
edit_biblio

Shiny apps for editing dataspice metadata tables
write_spice

Write spice
es_biblio

Get biblio from EML
es_attributes

Get attributes from EML
parse_spatialCoverage

Parse spatialCoverage section for use in a Leaflet map
jsonld_to_mustache

Convert JSONLD to a list suitable for Mustache templating
es_creators

Get creators from EML
%>%

Pipe operator
parse_GeoShape_box

Parse spatialCoverage$geo$box section for use in a Leaflet map
crosswalk_variables

Crosswalk dataspice variables to EML
crosswalk

Crosswalk a term
crosswalk_datetime

Convert a date(time) of unknown format into EML
crosswalk_Organization

Crosswalk a Schema.org/Organization
crosswalk_distribution

Crosswalk a Schema.org/distribution
as_jsonld

Convert a list object to JSON-LD
crosswalk_Person

Crosswalk functions for as_eml Crosswalk a Schema.org/Person
create_spice

Put metadata templates within a metadata subdirectory
crosswalk_creator

Crosswalk a Schema.org/creator