knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of virtuoso is to provide an easy interface to Virtuoso RDF database from R.
You can install the development version of virtuoso from GitHub with:
remotes::install_github("ropensci/virtuoso")
library(virtuoso)
For Mac users, virtuoso
package includes a utility function to install and configure a local Virtuoso Open Source instance using Homebrew. Otherwise, simply install the Virtuoso Open Source edition for your operating system.
vos_install()
We can now start our Virtuoso server from R:
vos_start()
Once the server is running, we can connect to the database.
con <- vos_connect()
Our connection is now live, and accepts SPARQL queries directly.
DBI::dbGetQuery(con, "SPARQL SELECT * WHERE { ?s ?p ?o } LIMIT 4")
virtuoso
also provides wrappers around some common queries to make it easier to work with Virtuoso and RDF.
The bulk loader can be used to quickly import existing sets of triples.
example <- system.file("extdata", "person.nq", package = "virtuoso") vos_import(con, example)
Can also read in compressed formats as well. Remember to set the pattern match appropriately. This is convenient because N-Quads compress particularly well, often by a factor of 20 (or rather, can be particularly large when uncompressed, owing to the repeated property and subject URIs).
ex <- system.file("extdata", "library.nq.gz", package = "virtuoso") vos_import(con, ex)
vos_import
invisibly returns a table of the loaded files, with error message and loading times. If a file cannot be imported, an error message is returned:
bad_file <- system.file("extdata", "bad_quads.nq", package = "virtuoso") vos_import(con, bad_file)
We can now query the imported data using SPARQL.
df <- vos_query(con, "SELECT ?p ?o WHERE { ?s ?p ?o . ?s a <http://schema.org/Person> }") head(df)
vos_query(con, "SELECT ?p ?o WHERE { ?s ?p ?o . ?s a <http://example.org/vocab#Chapter> }")
We can control any virtuoso
server started with vos_start()
using a series of helper commands.
vos_status()
Advanced usage note: vos_start()
invisibly returns a processx
object which we can pass to other server control functions, or access the embedded processx
control methods directly. The virtuoso
package also caches this object in an environment so that it can be accessed directly without having to keep track of an object in the global environment. Use vos_process()
to return the processx
object. For example:
library(ps) p <- vos_process() ps_is_running(p) ps_cpu_times(p) ps_suspend(p) ps_resume(p)
vos_kill()
Please see the package vignettes for more information:
Please note that the virtuoso
R package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
if(require(codemetar)) codemetar::write_codemeta()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.