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.
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.
endpoint <- "http://localhost:5820/project/query" # Endpoint
query = paste0("
PREFIX eg: <http://www.example.org/project/eg#>
SELECT (COUNT(?file) AS ?numFiles)
?file rdf:type eg:ValidationFile ; # No reasoner
# ?file rdf:type eg:File ; # Use with reasoner
} " )
qd <- SPARQL(endpoint, query)
triplesDF <- qd$results
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.
reasoning=TRUE considered “hacky?” It’s documented as the way to invoke reasoning, e.g. here
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…
Apparently, I can’t read.
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.
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.
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
curl to verify your syntax, then try submitting the known-good query from R via
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.
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:
Thanks John and Stephen for your help!
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.