Skip to contents

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:


Getting Started

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.

#> Virtuoso is already installed.

We can now start our Virtuoso server from R:

#> PROCESS 'virtuoso-t', running, pid 352405.
#> Server is now starting up, this may take a few seconds...
#> latest log entry: 22:02:55 Server online at 1111 (pid 352405)

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")
#>                                                                              s
#> 1         
#> 2
#> 3
#> 4
#>                                                 p                                                       o
#> 1
#> 2
#> 3
#> 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)
#> Error: Error importing: bad_quads.nq 37000 SP029: NQuads RDF loader, line 2: Undefined namespace prefix at ITIS:1000000

We can now query the imported data using SPARQL.

"SELECT ?p ?o 
 WHERE { ?s ?p ?o .
        ?s a <>
#>                                                 p                        o
#> 1
#> 2                            Professor
#> 3                                 Jane Doe
#> 4                      (425) 123-4567
#> 5                 
"SELECT ?p ?o 
 WHERE { ?s ?p ?o .
        ?s a <>
#>                                                 p                                        o
#> 1
#> 2 An introductory chapter on The Republic.
#> 3                          The Introduction

Server controls

We can control any virtuoso server started with vos_start() using a series of helper commands.

#> latest log entry: 22:02:56 PL LOG: No more files to load. Loader has finished,
#> [1] "sleeping"

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:

p <- vos_process()
#> [1] TRUE
#>            user          system   children_user children_system 
#>            1.82            0.54            0.00            0.00

Going further

Please see the package vignettes for more information: