How to query with reasoner from R (rstats)?

Hi Everyone,

I do most of my data manipulation and analysis in R and have used a variety of R packages (redland, rrdf/rdflibs, SPARQL) to retrieve data from Stardog with SPARQL queries.

Now I want to leverage ontologies and reasoning in my queries. I’ve seen recent posts here about how to do this from JavaScript with { reasoning : true }. I can’t find how to do this in with my usual R packages.

Are there any R users out there who have experience with reasoner-based querying? Is one potential solution to write the query in JavaScript and execute a .JS file from R (eeew - rather kludgy) ? If JS is the only option I would appreciate a code example to get me started.

Here is my example R code for a simple query. ValidationFile is a subclass of File, so I want to query for all “Files” with the reasoner on. I hope that makes sense.

library(SPARQL)

endpoint <- "http://localhost:5820/project/query"  # Endpoint

query = paste0("
PREFIX eg:  <http://www.example.org/project/eg#> 
SELECT (COUNT(?file) AS ?numFiles)
WHERE {
   ?file rdf:type eg:ValidationFile ;  # No reasoner
  #  ?file rdf:type eg:File ;  # Use with reasoner
} " )

qd <- SPARQL(endpoint, query)
triplesDF <- qd$results
triplesDF

Hi Tim,

It’s somewhat hacky (and I’m not sure if it’s officially supported), but you can append the query string param ?reasoning=true to your endpoint, and that should enable reasoning without you having to dive into the SPARQL library to set a flag.

Why is reasoning=TRUE considered "hacky?" It's documented as the way to invoke reasoning, e.g. here

1 Like

Considering I wrote the linked blog post, you might think I would have remembered that. You’re absolutely right, and I misspoke. It was, at one time or another, undocumented, so I suppose I still had that mindset…

I've tested with both library(SPARQL) and library(rrdf). Both failed. :frowning:

With endpoint set as:
endpoint <- 'http://localhost:5820/project/query?reasoner=TRUE'

  1. library(SPARQL)
    Results in error in console:
    Error: XML content does not seem to be XML: '
    {
    _:node1cm0aieofx10 a http://www.w3.org/ns/sparql-service-description#Service ;
    http://www.w3.org/ns/sparql-service-description#endpoint "/project/query" ;
    http://www.w3.org/ns/sparql-service-description#supportedLanguage http://www.w3.org/ns/sparql-service-description#SPARQL11Query ;
    http://www.w3.org/ns/sparql-service-description#defaultEntailmentRegime http://www.w3.org/ns/entailment/OWL-Direct ;
    http://www.w3.org/ns/sparql-service-description#supportedEntailmentProfile http://www.w3.org/ns/owl-profile/RDFS , http://www.w3.org/ns/owl-profile/QL , http://www.w3.org/ns/owl-profile/RL , http://www.w3.org/ns/owl-profile/EL , http://www.w3.org/ns/owl-profile/DL ;
    http://www.w3.org/ns/sparql-service-description#inputFormat http://www.w3.org/ns/formats/RDF_XML ;
    http://www.w3.org/ns/sparql-service-description#resultFormat http://www.w3.org/ns/formats/RDF_XML ;
    <http://www.w3.org/ns/sparq

  2. library(rrdf)
    Result: No error, but no results returned. Executed same query in Stardog Studio and returned a count of 16 with the reasoner turned on.

The parameter name should be "reasoning," not "reasoner." As for why that seems to have returned a document according to the graph store protocol, that might be worth us taking a look at...

Apparently, I can’t read. :crazy_face:

The good news:
With reasoning=TRUE , the code now works for library(rrdf). Hooray!
I’d stopped using this library a while back because it is no longer actively supported and is not on CRAN. I prefer a library that is actively supported.

Bad news:
Still getting “Error: XML content does not seem to be XML:” error for library(SPARQL), which is actively developed and is on CRAN.

I mistakenly do not have an example using library(redland) - I had only used it to read from TTL files, and not from triplestores.

While I have something that works, my quest for a supported R package continues.

Thanks for the advice and proof reading. :slightly_smiling_face:

T

Note that R SPARQL is just a (very thin) wrapper around RCurl. It might be useful for you to first test your query at the OS level using wget or curl to verify your syntax, then try submitting the known-good query from R via RCurl.

If these both work, the error is then either in what R SPARQL is tacking on or how you’re parameterizing it.

Unfortunately, we’ve found that to gain confidence in what we’re getting through SPARQL we need to sanity-check via the steps above.

John

Hi John,
Thanks for confirming curl because I had just started looking into RCurl! I have no experience with it, but I’m thinking there is a way to pass the arguments in the SPARQL() line. I just need to figure out the syntax.

SOLVED for library(SPARQL)

The R SPARQL library has an additional parameter "extra" that can be used to specify parameters and values. In this case:

qd <- SPARQL(endpoint, query, extra=list(reasoning='TRUE'))

Additional good info here:
http://linkedscience.org/tools/sparql-package-for-r/linked-open-piracy-tutorial/

Thanks John and Stephen for your help!

Tim

1 Like

just taking advantage of lunchtime to pass philosophical judgement…

it’s hacky because it consists in writing (hacking) the URL as opposed to using the prescribed library method for building that URL indicated in Novas’ solution

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.