roxigraph provides RDF storage and SPARQL query capabilities for R by wrapping the Oxigraph graph database. This vignette introduces the core functionality.
For persistent storage that survives R sessions:
roxigraph supports multiple RDF serialization formats:
turtle_data <- '
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix ex: <http://example.org/> .
ex:alice a foaf:Person ;
foaf:name "Alice" ;
foaf:age 30 ;
foaf:knows ex:bob .
ex:bob a foaf:Person ;
foaf:name "Bob" ;
foaf:age 25 .
'
rdf_load(store, turtle_data, format = "turtle")
rdf_size(store)
#> [1] 7sparql_query(store, "
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT (COUNT(?person) as ?count) (AVG(?age) as ?avg_age)
WHERE {
?person a foaf:Person ;
foaf:age ?age .
}
")
#> count
#> 1 "2"^^<http://www.w3.org/2001/XMLSchema#integer>
#> avg_age
#> 1 "27.5"^^<http://www.w3.org/2001/XMLSchema#decimal>Export your RDF data:
# Export to N-Quads format
output <- rdf_serialize(store, format = "nquads")
cat(substr(output, 1, 500), "...\n")
#> <http://example.org/carol> <http://xmlns.com/foaf/0.1/age> "28"^^<http://www.w3.org/2001/XMLSchema#integer> .
#> <http://example.org/carol> <http://xmlns.com/foaf/0.1/name> "Carol" .
#> <http://example.org/carol> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
#> <http://example.org/bob> <http://xmlns.com/foaf/0.1/age> "25"^^<http://www.w3.org/2001/XMLSchema#integer> .
#> <http://example.org/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
#> <http://example.org/bob> <http:/ ...Supported output formats: nquads, trig,
rdfxml
rdf_load() for
bulk data rather than individual rdf_add() calls