Skip to content

Commit

Permalink
Merge branch 'main' into byebye
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Oct 5, 2023
2 parents 545211d + be779dc commit 469e77b
Show file tree
Hide file tree
Showing 20 changed files with 1,321 additions and 646 deletions.
10 changes: 8 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Imports:
purrr,
rlang,
tibble,
WikidataQueryServiceR,
WikidataR,
xml2,
xmlparsedata
Expand All @@ -37,10 +36,16 @@ Suggests:
httptest2,
httpuv,
knitr,
leaflet,
rmarkdown,
testthat (>= 3.0.0),
withr
Config/Needs/website:
DT,
ggplot2,
leaflet,
lvaudor/sequins,
sf,
tidyr
VignetteBuilder:
knitr,
rmarkdown
Expand All @@ -49,3 +54,4 @@ Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3

105 changes: 74 additions & 31 deletions R/send_sparql.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,23 @@ send_sparql = function(query_string,
request_type = request_control[["request_type"]]
}

# if endpoint wikidata, use WikidataQueryServiceR::query_wikidata()
if (endpoint == "https://query.wikidata.org/") {
return(purrr::quietly(WikidataQueryServiceR::query_wikidata)(query_string)$result)
initial_request = httr2::request(endpoint) %>%
httr2::req_method("POST")

if (request_type == "url") {
initial_request = initial_request %>%
httr2::req_headers(
Accept = "application/sparql-results+json",
`Content-length` = 0
)
} else {
initial_request = initial_request %>%
httr2::req_headers(
Accept = "application/sparql-results+json"
)
}
# else, use httr2

initial_request = httr2::request(endpoint) %>%
httr2::req_method("POST") %>%
httr2::req_headers(Accept = "application/sparql-results+json") %>%
initial_request = initial_request %>%
httr2::req_user_agent(user_agent) %>%
httr2::req_retry(max_tries = max_tries, max_seconds = max_seconds) %>%
httr2::req_timeout(timeout)
Expand Down Expand Up @@ -141,34 +149,30 @@ send_sparql = function(query_string,

# Adapted from https://github.com/wikimedia/WikidataQueryServiceR/blob/accff89a06ad4ac4af1bef369f589175c92837b6/R/query.R#L56
if (length(content$results$bindings) > 0) {
parse_binding = function(binding, name) {
type <- sub(
"http://www.w3.org/2001/XMLSchema#", "",
binding[["datatype"]] %||% "http://www.w3.org/2001/XMLSchema#character"
)

parse = function(x, type) {
switch(
type,
character = x,
# easier for now as dbpedia can return different things with the same name
integer = ifelse(endpoint == "https://dbpedia.org/sparql", x, as.integer(x)),
datetime = anytime::anytime(x),
x
parsed_results = purrr::map(
content[["results"]][["bindings"]],
parse_result,
endpoint = endpoint
)
data_frame = try(
dplyr::bind_rows(parsed_results),
silent = TRUE
)

# for instance this can happen with HAL
binding_failed <- inherits(data_frame, "try-error")

if (binding_failed) {
parsed_results <- purrr::map(
content$results$bindings,
parse_result,
simple = TRUE,
endpoint = endpoint
)
}
value = parse(binding[["value"]], type)
tibble::tibble(.rows = 1) %>%
dplyr::mutate({{name}} := value)
}

parse_result = function(result) {
purrr::map2(result, names(result), parse_binding) %>%
dplyr::bind_cols()
data_frame <- dplyr::bind_rows(parsed_results)
}

data_frame <- purrr::map_df(content$results$bindings, parse_result)

} else {
data_frame <- dplyr::as_tibble(
matrix(
Expand All @@ -183,3 +187,42 @@ send_sparql = function(query_string,
}

utils::globalVariables("usual_endpoints")

parse_result = function(result, simple = FALSE, endpoint) {

purrr::map2(
result,
names(result),
parse_binding,
simple = simple,
endpoint = endpoint
) %>%
dplyr::bind_cols()
}

parse_binding = function(binding, name, simple, endpoint) {
type <- tolower(sub(
"http://www.w3.org/2001/XMLSchema#", "",
binding[["datatype"]] %||% "http://www.w3.org/2001/XMLSchema#character"
))

parse = if (!simple) {
function(x, type, endpoint) {
switch(
type,
character = x,
# easier for now as dbpedia can return different things with the same name
integer = ifelse(endpoint == "https://dbpedia.org/sparql", x, as.integer(x)),
datetime = anytime::anytime(x),
x
)
}
} else {
function(x, type, endpoint) {
as.character(x)
}
}
value = parse(binding[["value"]], type, endpoint = endpoint)
tibble::tibble(.rows = 1) %>%
dplyr::mutate({{name}} := value)
}
4 changes: 2 additions & 2 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ navbar:
href: articles/explore.html
- text: glitter for dataBNF
href: articles/glitter_for_dataBNF.html
- text: Bibliometry with HAL
href: articles/glitter_for_hal.html
- text: Bibliometry with HAL (French)
href: articles/glitter_bibliometry.html
- text: Learn more about how glitter works
href: articles/internals.html
2 changes: 1 addition & 1 deletion data-raw/usual_endpoints.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name,url
wikidata,https://query.wikidata.org/
wikidata,https://query.wikidata.org/sparql
dbpedia,https://dbpedia.org/sparql
databnf,https://data.bnf.fr/sparql
isidore,https://isidore.science/sparql
Expand Down
Binary file modified data/usual_endpoints.rda
Binary file not shown.
Loading

0 comments on commit 469e77b

Please sign in to comment.