A friendly and consise user interface for performing common tasks onrdf data, such as parsing and converting between formats includingrdfxml, turtle, nquads, ntriples, and trig, creating rdf graphs, andperforming SPARQL queries. This package wraps the redland R packagewhich provides direct bindings to the redland C library. Additionally,the package supports parsing and serialization of rdf into json-ldthrough the json-ld package, which binds the official json-ld javascriptAPI. The package interface takes inspiration from the Python rdfliblibrary.
You can install rdflib from GitHub with:
# install.packages("devtools")devtools::install_github("ropensci/rdflib")While not required,rdflib is designed to play nicelywith%>% pipes, so we will load themagrittr package as well:
library(magrittr)library(rdflib)Parse a file and serialize into a different format:
system.file("extdata/dc.rdf",package="redland")%>%rdf_parse()%>%rdf_serialize("test.nquads","nquads")Perform SPARQL queries:
sparql<-'PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c WHERE { ?a dc:creator ?c . }'system.file("extdata/dc.rdf",package="redland")%>%rdf_parse()%>%rdf_query(sparql)#> # A tibble: 1 × 2#> a c#> <chr> <chr>#> 1 http://www.dajobe.org/ Dave BeckettInitialize graph a new object or add triples statements to anexisting graph:
x<-rdf()x<-rdf_add(x,subject="http://www.dajobe.org/",predicate="http://purl.org/dc/elements/1.1/language",object="en")x#> Total of 1 triples, stored in hashes#> -------------------------------#> <http://www.dajobe.org/> <http://purl.org/dc/elements/1.1/language> "en" .Change the default display format (nquads) for graphobjects:
options(rdf_print_format ="jsonld")x#> Total of 1 triples, stored in hashes#> -------------------------------#> {#> "@id": "http://www.dajobe.org/",#> "http://purl.org/dc/elements/1.1/language": "en"#> }We can also work with the JSON-LD format through additional functionsprovided in the R package,jsonld.
out<-tempfile()rdf_serialize(x, out,"jsonld")rdf_parse(out,format ="jsonld")#> Total of 1 triples, stored in hashes#> -------------------------------#> {#> "@id": "http://www.dajobe.org/",#> "http://purl.org/dc/elements/1.1/language": "en"#> }For more information on the JSON-LD RDF API, seehttps://json-ld.org/spec/latest/json-ld-rdf/.
Seearticlesfrom the documentation for advanced use including applications to largetriplestores, example SPARQL queries, and information about additionaldatabase backends.
Please also cite the underlyingredland library whencitingrdflib
Carl Boettiger. (2018). rdflib: A high level wrapper around theredland package for common rdf applications (Version 0.1.0). Zenodo.https://doi.org/10.5281/zenodo.1098478
Jones M, Slaughter P, Ooms J, Boettiger C, Chamberlain S (2022).redland: RDF Library Bindings in R.doi:10.5063/F1VM496Bhttps://doi.org/10.5063/F1VM496B, R package version1.0.17-16,https://github.com/ropensci/redland-bindings/tree/master/R/redland.