The Resource Description Framework(RDF) is a W3C standard for describing resources and their relationships using machine-readable statements. RDF makes metadata about resources created in R—such as datasets, visualisations, and analytical outputs—interoperable with systems outside the R ecosystem, without requiring those systems to understand R objects.
RDF has several standard serialization formats. N-Triples is a particularly simple, line-oriented format in which each statement consists of a subject, predicate, and object. It is well suited to describing the metadata and structural semantics of tidy datasets, as well as their provenance, authorship, and relationships to other resources.
The ntriples package provides lightweight tools for
creating these statements directly from R.
The built-in Orange dataset provides measurements of
orange tree growth. We use the published dataset Draper–Smith
(1998): Growth of Orange Trees, identified by DOI 10.5281/zenodo.14917851,
to demonstrate how to describe an R dataset using interoperable RDF
metadata.
We first load the dataset, represent its authors using
utils::person(), and then serialize the dataset’s identity,
descriptive metadata, and provenance as N-Triples.
The example uses PROV-O, the W3C provenance ontology, to describe agents and their relationships to the dataset.
RDF vocabularies provide standard terms for describing resources and
their relationships. Each vocabulary has a namespace IRI, commonly
abbreviated using a prefix. For example,
prov:wasAttributedTo refers to http://www.w3.org/ns/prov#wasAttributedTo.
We use several established RDF vocabularies and datatype definitions:
PROV Ontology (PROV-O) to express the PROV Data Model for provenance, or information about entities, activities, and people involved in producing a piece of data.
Data Catalog Vocabulary (DCAT), provides a
standard way to describe datasets and their distributions in data
catalogues. By identifying Orange as a dcat:Dataset,
we make its resource type understandable to data catalogues and other
systems that use DCAT, independently of how the dataset is represented
in R.
Dublin Core Metadata Initiative Terms (DCMI) for descriptive metadata, such as defining what is the “title” of a dataset or a publication
RDF Schema (RDFS) to provide human-readable labels for agents.
XML Schema Datatypes(XSD) specifies the datatypes of literal values, such as strings, integers, and dates, so that they remain understandable outside of the R environment.
The person_triples() and rdf_term()
functions handle RDFS labels and XSD datatypes automatically, so we only
need to define namespace variables for DCAT, DCT, and PROV-O in this
example.
prov <- "http://www.w3.org/ns/prov#"
dcat <- "http://www.w3.org/ns/dcat#"
dct <- "http://purl.org/dc/terms/"In RDF documentation, long IRIs are often abbreviated using prefixes.
For example, prov:wasAttributedTo is shorthand for http://www.w3.org/ns/prov#wasAttributedTo.
In our R code, we use paste0(prov, "wasAttributedTo") to
construct this complete IRI without repeatedly typing the namespace. The
N-Triples format requires complete IRIs, so the abbreviated form
prov:wasAttributedTo does not appear in the serialized
output.
triples <- n_triples(c(
# Identify and describe the dataset
n_triple(dataset_iri, "a", paste0(dcat, "Dataset")),
n_triple(dataset_iri, paste0(dct, "title"), "Growth of Orange Trees"),
# Attribute the dataset to its creators
n_triple(dataset_iri, paste0(prov, "wasAttributedTo"), draper_iri),
n_triple(dataset_iri, paste0(prov, "wasAttributedTo"), smith_id),
# Describe the creators as RDF agents
person_triples(draper),
person_triples(smith, subject = smith_id)
))cat(triples, sep = "\n")
#> <https://doi.org/10.5281/zenodo.14917851> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/dcat#Dataset> .
#> <https://doi.org/10.5281/zenodo.14917851> <http://purl.org/dc/terms/title> "Growth of Orange Trees"^^<http://www.w3.org/2001/XMLSchema#string> .
#> <https://doi.org/10.5281/zenodo.14917851> <http://www.w3.org/ns/prov#wasAttributedTo> <http://viaf.org/viaf/84585260> .
#> <https://doi.org/10.5281/zenodo.14917851> <http://www.w3.org/ns/prov#wasAttributedTo> _:smith .
#> <http://viaf.org/viaf/84585260> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/prov#Agent> .
#> <http://viaf.org/viaf/84585260> <http://www.w3.org/2000/01/rdf-schema#label> "N.R. Draper"^^<http://www.w3.org/2001/XMLSchema#string> .
#> _:smith <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/prov#Agent> .
#> _:smith <http://www.w3.org/2000/01/rdf-schema#label> "H. Smith"^^<http://www.w3.org/2001/XMLSchema#string> .Each line is an RDF statement consisting of three terms:
For example, the statement using prov:wasAttributedTo
connects the dataset to one of its creators. The statements generated by
person_triples() identify the creators as prov:Agent
resources and describe their names using rdfs:label.
The resulting RDF statements can be interpreted by other systems without knowing how the original R objects were constructed.
The rdf_term() function converts individual R values
into RDF terms. It is also used internally by
n_triple().
rdf_term("Growth of Orange Trees")
#> [1] "\"Growth of Orange Trees\"^^<http://www.w3.org/2001/XMLSchema#string>"
rdf_term(42L)
#> [1] "\"42\"^^<http://www.w3.org/2001/XMLSchema#integer>"
rdf_term(iri("urn:example:dataset"))
#> [1] "<urn:example:dataset>"Character values become typed string literals, integers become
xsd:integer literals, and explicit IRIs become RDF resource
identifiers.
RDF distinguishes between a literal and a resource identifier. For
this reason, iri() can be used to make the intended
interpretation explicit.
N-Triples is a plain-text format. Each statement occupies one line
and ends with a period. The statements can therefore be written directly
to a .nt file:
The resulting file can be imported into RDF databases, processed by Semantic Web tools, or exchanged with applications outside R.
The original Orange data frame remains unchanged. Its
observations can be distributed in a tabular format such as CSV, while
the N-Triples file provides interoperable metadata about the dataset and
its provenance.
The ntriples package deliberately separates RDF
serialization from higher-level data modelling. Its principal functions
are:
iri() — identify and validate IRIs.rdf_term() — serialize individual R values as RDF
terms.n_triple() — construct a single RDF statement.n_triples() — collect statements and remove
duplicates.person_triples() — describe a person using PROV-O or
FOAF.Together, these functions provide a lightweight foundation for describing R resources using standard RDF vocabularies.