The R package redland provides methods to create, query and write information stored in the
Resource Description Framework (RDF). This package is implemented as R scripts that provide an
R interface (aka "wrapper") to the Redland RDF C libraries. Documentation for the redland R package classes
and functions are available from the standard R help facility, for example, 'help("Node-class")'
, '?getNodeType'
, etc.
An overview of the redland R package is available with the R command: 'vignette("redland_overview")'
.
The Redland C library functions are described at https://librdf.org/docs/api/index.html.
An introduction to RDF can be found at https://www.w3.org/TR/rdf-primer/.
Details
The redland R package classes and the corresponding Redland C library types are shown in the following table:
Concept | Redland C type | redland R class | Purpose |
Resource / Literal | librdf_node | Node | RDF Model & Syntax nodes |
Statement / Triple | librdf_statement | Statement | RDF Model & Syntax arcs (statements, triples) |
Model | librdf_model | Model | Set of Statements usually held in one Storage. |
Node | librdf_node | Node | The subject, predicate or object of a Statement |
Storage | librdf_storage | Storage | Storage for Models either persistent or in-memory. |
Parser | librdf_parser | Parser | Syntax parsers delivering Stream of Statements or writing to a Model |
Query | librdf_query | Query | Querying of an Model delivering a QueryResults |
QueryResults | librdf_query_results | QueryResults | Results of applying an Query to a Model giving either variable bindings with Node values or Stream of Statements |
Serializer | librdf_serializer | Serializer | Serializes a Model into a syntax such as RDF/XML |
World | librdf_world | World | RDF wrapper class handling Redland startup/shutdown |
Note
In order to communicate with the Redland RDF C libraries, the redland R package uses an interface layer that is created with the software package Simplified Wrapper and Interface Generator (SWIG). The relationship between the redland R package and the Redland C libraries is:
User script -> redland R package -> SWIG R interface -> Redland C libraries -> RDF data
It is recommended that the redland package R classes be used to interact with RDF, as these higher level classes take care of many of the the
details of communicating with the Redland C libraries. However, all of the lower level R interface functions generated by SWIG are made available
by the redland package. These interface functions usually have names beginning with 'librdf_'
, 'rasqal_'
or 'raptor_'
and
are usually the same name as the underlying C library function. Documentation
for the R SWIG interface functions can be found via R help i.e. '?librdf_iterator'
.
Examples
# This example creates the necessary R objects to hold an RDF model and reads
# in a file that contains RDF/XML statements. This model is then queried for
# and the query results inspected.
world <- new("World")
storage <- new("Storage", world, "hashes", name="", options="hash-type='memory'")
model <- new("Model", world, storage, options="")
filePath <- system.file("extdata/example.rdf", package="redland")
parser <- new("Parser", world)
parseFileIntoModel(parser, world, filePath, model)
queryString <- paste("PREFIX dc: <http://purl.org/dc/elements/1.1/> ",
"SELECT ?a ?c WHERE { ?a dc:description ?c . }", sep="")
query <- new("Query", world, queryString, base_uri=NULL,
query_language="sparql", query_uri=NULL)
results <- getResults(query, model, "rdfxml")
# When the query object is no longer needed, the resources it had allocated can be freed.
freeQuery(query)
#> NULL
rm(query)