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
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.
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…
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...
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.
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.
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.
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