Closed ablaette closed 1 year ago
File 'utils.R' now includes a simplified version of SPARQL::SPARLQ()
that replaces packages XML and RCurl with xml2 and httr.
sparql_query <- function(endpoint, query){
stopifnot(
is.character(endpoint), length(endpoint) == 1L,
is.character(query), length(query) == 1L
)
url <- paste(
endpoint,
"?query=",
gsub("\\+", "%2B", URLencode(query, reserved = TRUE)),
sep = ""
)
results <- GET(
url = url,
add_headers(Accept = "application/sparql-results+xml")
)
content <- content(results, as = "text")
dom <- read_xml(x = content)
results <- xml_find_all(x = dom, xpath = "//d1:result")
if (length(results) == 0L) return(data.frame(c()))
vars <- xml_find_all(dom, xpath = "//d1:head/d1:variable")
attrs <- xml_attr(vars, attr = "name")
data <- lapply(
attrs,
function(attr){
nodes <- xml_find_all(
results,
xpath = sprintf("//d1:binding[@name='%s']", attr)
)
if (length(nodes) == 0L){
rep(NA, times = length(results))
} else {
xml_text(nodes)
}
}
)
names(data) <- attrs
data.frame(data)
}
We rely on the
SPARQL::SPARQL()
function. The SPARQL package cannot be installed from CRAN and we need to load RCurl and XML seperately. Looking at the code of the function, it should not be too complicated to develop a replacement,