WikidataQueryServiceR/ 0000755 0001762 0000144 00000000000 13672223432 014504 5 ustar ligges users WikidataQueryServiceR/NAMESPACE 0000644 0001762 0000144 00000000203 13672221347 015721 0 ustar ligges users # Generated by roxygen2: do not edit by hand
export(get_example)
export(query_wikidata)
export(scrape_example)
import(ratelimitr)
WikidataQueryServiceR/LICENSE 0000644 0001762 0000144 00000000061 13032777103 015505 0 ustar ligges users YEAR: 2016
COPYRIGHT HOLDER: Wikimedia Foundation WikidataQueryServiceR/README.md 0000644 0001762 0000144 00000015200 13672173564 015773 0 ustar ligges users WikidataQueryServiceR
================
- [Installation](#installation)
- [Usage](#usage)
- [Example: fetching genres of a particular
movie](#example-fetching-genres-of-a-particular-movie)
- [Fetching queries from Wikidata’s examples
page](#fetching-queries-from-wikidatas-examples-page)
- [Links for learning SPARQL](#links-for-learning-sparql)
- [Additional Information](#additional-information)
[](http://www.repostatus.org/#active)
[](https://cran.r-project.org/package=WikidataQueryServiceR)
[](https://cran.r-project.org/package=WikidataQueryServiceR)
[](https://opensource.org/licenses/MIT)
This is an R wrapper for the [Wikidata Query Service
(WDQS)](https://www.mediawiki.org/wiki/Wikidata_query_service) which
provides a way for tools to query
[Wikidata](https://www.wikidata.org/wiki/Wikidata:Main_Page) via
[SPARQL](https://en.wikipedia.org/wiki/SPARQL) (see the beta at
). It is written in and for R, and was
inspired by Os Keyes’
[WikipediR](https://github.com/Ironholds/WikipediR) and
[WikidataR](https://github.com/Ironholds/WikidataR) packages.
**Author:** Mikhail Popov (Wikimedia Foundation)
**License:**
[MIT](http://opensource.org/licenses/MIT)
**Status:** Active
## Installation
``` r
install.packages("WikidataQueryServiceR")
```
To install the development version:
``` r
# install.packages("remotes")
remotes::install_github("bearloga/WikidataQueryServiceR")
```
## Usage
``` r
library(WikidataQueryServiceR)
```
## See ?WDQS for resources on Wikidata Query Service and SPARQL
You submit SPARQL queries using the `query_wikidata()` function.
### Example: fetching genres of a particular movie
In this example, we find an “instance of”
([P31](https://www.wikidata.org/wiki/Property:P31)) “film”
([Q11424](https://www.wikidata.org/wiki/Q11424)) that has the label “The
Cabin in the Woods” ([Q45394](https://www.wikidata.org/wiki/Q45394)),
get its genres ([P136](https://www.wikidata.org/wiki/Property:P136)),
and then use [WDQS label
service](https://www.mediawiki.org/wiki/Wikidata_query_service/User_Manual#Label_service)
to return the genre labels.
``` r
query_wikidata('SELECT DISTINCT
?genre ?genreLabel
WHERE {
?film wdt:P31 wd:Q11424.
?film rdfs:label "The Cabin in the Woods"@en.
?film wdt:P136 ?genre.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}')
```
| genre | genreLabel |
| :---------------------------------------- | :------------------- |
| | zombie film |
| | science fiction film |
| | comedy-drama |
| | monster film |
| | slasher film |
| | comedy horror |
For more example SPARQL queries, see [this
page](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples)
on [Wikidata](https://www.wikidata.org/wiki/Wikidata:Main_Page).
`query_wikidata()` can accept multiple queries, returning a (potentially
named) list of data frames. If the vector of SPARQL queries is named,
the results will inherit those names.
### Fetching queries from Wikidata’s examples page
The package provides a
[WikipediR](https://github.com/Ironholds/WikipediR/)-based function for
getting SPARQL queries from the [WDQS examples
page](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples).
``` r
sparql_query <- get_example(c("Cats", "How many states this US state borders"))
```
``` r
sparql_query[["How many states this US state borders"]]
```
``` sparql
#added before 2016-10
SELECT ?state ?stateLabel ?borders
WHERE
{
{
SELECT ?state (COUNT(?otherState) as ?borders)
WHERE
{
?state wdt:P31 wd:Q35657 .
?otherState wdt:P47 ?state .
?otherState wdt:P31 wd:Q35657 .
}
GROUP BY ?state
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}
}
ORDER BY DESC(?borders)
```
Now we can run all extracted SPARQL queries:
``` r
results <- query_wikidata(sparql_query)
lapply(results, dim)
```
## $Cats
## [1] 147 2
##
## $`How many states this US state borders`
## [1] 48 3
``` r
head(results$`How many states this US state borders`)
```
| state | stateLabel | borders |
| :------------------------------------- | :----------- | ------: |
| | Tennessee | 8 |
| | Missouri | 8 |
| | Colorado | 7 |
| | Kentucky | 7 |
| | Pennsylvania | 6 |
| | South Dakota | 6 |
## Links for learning SPARQL
- [A beginner-friendly course for
SPARQL](https://www.wikidata.org/wiki/Wikidata:A_beginner-friendly_course_for_SPARQL)
- Building a SPARQL query: [Museums on
Instagram](https://www.wikidata.org/wiki/Help:SPARQL/Building_a_query/Museums_on_Instagram)
- [SPARQL Query
Examples](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples)
for WDQS
- [Using SPARQL to access Linked Open
Data](http://programminghistorian.org/lessons/graph-databases-and-SPARQL)
by Matthew Lincoln
- Interesting or illustrative [SPARQL
queries](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries)
for Wikidata
- Wikidata [2016 SPARQL
Workshop](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/2016_SPARQL_Workshop)
- [Wikidata SPARQL Query video
tutorial](https://www.youtube.com/watch?v=1jHoUkj_mKw) by Navino
Evans
- *[Learning SPARQL](http://www.learningsparql.com/)* by Bob DuCharme
- [WDQS User
Manual](https://www.mediawiki.org/wiki/Wikidata_query_service/User_Manual)
## Additional Information
Please note that this project is released with a [Contributor Code of
Conduct](https://github.com/bearloga/WikidataQueryServiceR/blob/master/CONDUCT.md).
By participating in this project you agree to abide by its terms.
WikidataQueryServiceR/man/ 0000755 0001762 0000144 00000000000 13672156741 015267 5 ustar ligges users WikidataQueryServiceR/man/WDQSR-deprecated.Rd 0000644 0001762 0000144 00000001272 13101761457 020547 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/deprecated.R
\name{WDQSR-deprecated}
\alias{WDQSR-deprecated}
\alias{scrape_example}
\title{Deprecated functions}
\usage{
scrape_example(example_name, ...)
}
\arguments{
\item{example_name}{the names of the examples as they appear on
\href{https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples}{this page}}
\item{...}{ignored (kept for backwards-compatibility)}
}
\description{
Why did I have to go and make things so deprecated?
}
\section{Functions}{
\itemize{
\item \code{scrape_example}: use \link{get_example} instead which employs \link[WikipediR:page_content]{WikipediR::page_content}
}}
WikidataQueryServiceR/man/WikidataQueryServiceR-package.Rd 0000644 0001762 0000144 00000004606 13672157426 023404 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/WikidataQueryServiceR-package.R
\docType{package}
\name{WikidataQueryServiceR-package}
\alias{WikidataQueryServiceR-package}
\alias{_PACKAGE}
\alias{WDQS}
\title{WikidataQueryServiceR: API Client Library for 'Wikidata Query Service'}
\description{
An API client for the 'Wikidata Query Service'
.
}
\details{
\href{https://www.mediawiki.org/wiki/Wikidata_query_service}{Wikidata Query Service}
is maintained by the \href{https://wikimediafoundation.org/}{Wikimedia Foundation}.
}
\section{Resources}{
\itemize{
\item \href{https://www.wikidata.org/wiki/Wikidata:A_beginner-friendly_course_for_SPARQL}{A beginner-friendly course for SPARQL}
\item Building a SPARQL query: \href{https://www.wikidata.org/wiki/Help:SPARQL/Building_a_query/Museums_on_Instagram}{Museums on Instagram}
\item \href{https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples}{SPARQL Query Examples} for WDQS
\item \href{http://programminghistorian.org/lessons/graph-databases-and-SPARQL}{Using SPARQL to access Linked Open Data}
by Matthew Lincoln
\item Interesting or illustrative \href{https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries}{SPARQL queries}
for Wikidata
\item Wikidata \href{https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/2016_SPARQL_Workshop}{2016 SPARQL Workshop}
\item \href{https://www.youtube.com/watch?v=1jHoUkj_mKw}{Wikidata SPARQL Query video tutorial}
by Navino Evans
\item \emph{\href{http://www.learningsparql.com/}{Learning SPARQL}} by Bob DuCharme
\item \href{https://www.mediawiki.org/wiki/Wikidata_query_service/User_Manual}{WDQS User Manual}
\item \href{https://github.com/bearloga/wmf/blob/master/presentations/talks/Cascadia\%20R\%20Conference\%202017/presentation.md#wikidata-query-service-wdqs}{Quick intro to WDQS & SPARQL}
from \href{https://github.com/bearloga/wmf/tree/master/presentations/talks/Cascadia\%20R\%20Conference\%202017}{my Cascadia R Conference 2017 talk}
}
}
\seealso{
Useful links:
\itemize{
\item \url{https://github.com/bearloga/WikidataQueryServiceR}
\item Report bugs at \url{https://github.com/bearloga/WikidataQueryServiceR/issues}
}
}
\author{
\strong{Maintainer}: Mikhail Popov \email{mikhail@wikimedia.org} (@bearloga on Twitter)
Other contributors:
\itemize{
\item Wikimedia Foundation [copyright holder]
}
}
\keyword{internal}
WikidataQueryServiceR/man/query_wikidata.Rd 0000644 0001762 0000144 00000002547 13672170536 020606 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/query.R
\name{query_wikidata}
\alias{query_wikidata}
\title{Send one or more SPARQL queries to WDQS}
\usage{
query_wikidata(sparql_query, format = c("simple", "smart"))
}
\arguments{
\item{sparql_query}{SPARQL query (can be a vector of queries)}
\item{format}{"simple" uses CSV and returns pure character data frame, while
"smart" fetches JSON-formatted data and returns a data frame with datetime
columns converted to \code{POSIXct}}
}
\value{
A tibble data frame
}
\description{
Makes a POST request to Wikidata Query Service SPARQL endpoint.
}
\section{Query limits}{
There is a hard query deadline configured which is set to 60 seconds. There
are also following limits:
\itemize{
\item One client (user agent + IP) is allowed 60 seconds of processing time each
60 seconds
\item One client is allowed 30 error queries per minute
See \href{https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual#Query_limits}{query limits section}
in the WDQS user manual for more information.
}
}
\examples{
sparql_query <- "SELECT
?softwareVersion ?publicationDate
WHERE {
BIND(wd:Q206904 AS ?R)
?R p:P348 [
ps:P348 ?softwareVersion;
pq:P577 ?publicationDate
] .
}"
query_wikidata(sparql_query)
\dontrun{
query_wikidata(sparql_query, format = "smart")
}
}
\seealso{
\link{get_example}
}
WikidataQueryServiceR/man/get_example.Rd 0000644 0001762 0000144 00000002203 13101761457 020036 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{get_example}
\alias{get_example}
\title{Get an example SPARQL query from Wikidata}
\usage{
get_example(example_name)
}
\arguments{
\item{example_name}{the names of the examples as they appear on
\href{https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples}{this page}}
}
\value{
The SPARQL query as a character vector.
}
\description{
Gets the specified example(s) from
\href{https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples}{SPARQL query service examples page}
using \href{https://www.wikidata.org/w/api.php}{Wikidata's MediaWiki API}.
}
\details{
If you are planning on extracting multiple examples, please provide
all the names as a single vector for efficiency.
}
\examples{
\dontrun{
sparql_query <- extract_example(c("Cats", "Horses"))
query_wikidata(sparql_query)
# returns a named list with two data frames
# one called "Cats" and one called "Horses"
sparql_query <- extract_example("Largest cities with female mayor")
cat(sparql_query)
query_wikidata(sparql_query)
}
}
\seealso{
\link{query_wikidata}
}
WikidataQueryServiceR/DESCRIPTION 0000644 0001762 0000144 00000002141 13672223432 016210 0 ustar ligges users Package: WikidataQueryServiceR
Title: API Client Library for 'Wikidata Query Service'
Version: 1.0.0
Date: 2020-06-16
Authors@R: c(
person("Mikhail", "Popov", email = "mikhail@wikimedia.org",
role = c("aut", "cre"), comment = "@bearloga on Twitter"),
person("Wikimedia Foundation", role = "cph")
)
Description: An API client for the 'Wikidata Query Service'
.
Depends: R (>= 3.1.2)
Imports: httr (>= 1.2.1), dplyr (>= 1.0.0), jsonlite (>= 1.2),
WikipediR (>= 1.5.0), ratelimitr (>= 0.4.1), purrr (>= 0.3.4),
readr (>= 1.3.1), rex (>= 1.2.0)
Suggests: testthat (>= 2.3.0), lintr (>= 2.0.1)
URL: https://github.com/bearloga/WikidataQueryServiceR
BugReports: https://github.com/bearloga/WikidataQueryServiceR/issues
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
NeedsCompilation: no
Packaged: 2020-06-16 20:01:17 UTC; mpopov
Author: Mikhail Popov [aut, cre] (@bearloga on Twitter),
Wikimedia Foundation [cph]
Maintainer: Mikhail Popov
Repository: CRAN
Date/Publication: 2020-06-16 20:10:02 UTC
WikidataQueryServiceR/tests/ 0000755 0001762 0000144 00000000000 13672156157 015657 5 ustar ligges users WikidataQueryServiceR/tests/testthat/ 0000755 0001762 0000144 00000000000 13672223432 017506 5 ustar ligges users WikidataQueryServiceR/tests/testthat/test-query.R 0000644 0001762 0000144 00000001445 13672156157 021770 0 ustar ligges users context("Querying")
query <- "SELECT DISTINCT
?softwareVersion ?publicationDate
WHERE {
BIND(wd:Q206904 AS ?R)
?R p:P348 [
ps:P348 ?softwareVersion;
pq:P577 ?publicationDate
] .
}"
suppressMessages({
simple_results <- query_wikidata(query, format = "simple")
smart_results <- query_wikidata(query, format = "smart")
})
test_that("data", {
expect_s3_class(simple_results, "data.frame")
expect_s3_class(smart_results, "data.frame")
expect_equal(names(simple_results), c("softwareVersion", "publicationDate"))
expect_equal(names(smart_results), c("softwareVersion", "publicationDate"))
})
test_that("date formatting", {
expect_s3_class(smart_results$publicationDate, "POSIXct")
})
test_that("simple ~= smart", {
expect_equal(nrow(simple_results), nrow(smart_results))
})
WikidataQueryServiceR/tests/testthat.R 0000644 0001762 0000144 00000000126 13672156157 017641 0 ustar ligges users library(testthat)
library(WikidataQueryServiceR)
test_check("WikidataQueryServiceR")
WikidataQueryServiceR/R/ 0000755 0001762 0000144 00000000000 13672163717 014716 5 ustar ligges users WikidataQueryServiceR/R/http.R 0000644 0001762 0000144 00000000521 13672167033 016011 0 ustar ligges users #' @import ratelimitr
wdqs_requester <- function() {
req <- function(query, ...) {
httr::POST(
url = "https://query.wikidata.org/sparql",
query = list(query = query),
httr::user_agent("https://github.com/bearloga/WikidataQueryServiceR"),
...
)
}
return(limit_rate(req, rate(n = 30, period = 60)))
}
WikidataQueryServiceR/R/WikidataQueryServiceR-package.R 0000644 0001762 0000144 00000003375 13672157373 022671 0 ustar ligges users #' @keywords internal
#' @aliases WDQS
#' @details [Wikidata Query Service](https://www.mediawiki.org/wiki/Wikidata_query_service)
#' is maintained by the [Wikimedia Foundation](https://wikimediafoundation.org/).
#' @section Resources:
#' - [A beginner-friendly course for SPARQL](https://www.wikidata.org/wiki/Wikidata:A_beginner-friendly_course_for_SPARQL)
#' - Building a SPARQL query: [Museums on Instagram](https://www.wikidata.org/wiki/Help:SPARQL/Building_a_query/Museums_on_Instagram)
#' - [SPARQL Query Examples](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples) for WDQS
#' - [Using SPARQL to access Linked Open Data](http://programminghistorian.org/lessons/graph-databases-and-SPARQL)
#' by Matthew Lincoln
#' - Interesting or illustrative [SPARQL queries](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries)
#' for Wikidata
#' - Wikidata [2016 SPARQL Workshop](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/2016_SPARQL_Workshop)
#' - [Wikidata SPARQL Query video tutorial](https://www.youtube.com/watch?v=1jHoUkj_mKw)
#' by Navino Evans
#' - _[Learning SPARQL](http://www.learningsparql.com/)_ by Bob DuCharme
#' - [WDQS User Manual](https://www.mediawiki.org/wiki/Wikidata_query_service/User_Manual)
#' - [Quick intro to WDQS & SPARQL](https://github.com/bearloga/wmf/blob/master/presentations/talks/Cascadia\%20R\%20Conference\%202017/presentation.md#wikidata-query-service-wdqs)
#' from [my Cascadia R Conference 2017 talk](https://github.com/bearloga/wmf/tree/master/presentations/talks/Cascadia\%20R\%20Conference\%202017)
"_PACKAGE"
# The following block is used by usethis to automatically manage
# roxygen namespace tags. Modify with care!
## usethis namespace: start
## usethis namespace: end
NULL
WikidataQueryServiceR/R/utils.R 0000644 0001762 0000144 00000004031 13672177366 016204 0 ustar ligges users #' @title Get an example SPARQL query from Wikidata
#' @description Gets the specified example(s) from
#' [SPARQL query service examples page](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples)
#' using [Wikidata's MediaWiki API](https://www.wikidata.org/w/api.php).
#' @details If you are planning on extracting multiple examples, please provide
#' all the names as a single vector for efficiency.
#' @param example_name the names of the examples as they appear on
#' [this page](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples)
#' @return The SPARQL query as a character vector.
#' @examples
#' \dontrun{
#' sparql_query <- extract_example(c("Cats", "Horses"))
#' query_wikidata(sparql_query)
#' # returns a named list with two data frames
#' # one called "Cats" and one called "Horses"
#'
#' sparql_query <- extract_example("Largest cities with female mayor")
#' cat(sparql_query)
#' query_wikidata(sparql_query)
#' }
#' @seealso [query_wikidata]
#' @export
get_example <- function(example_name) {
content <- WikipediR::page_content(
domain = "www.wikidata.org",
page_name = "Wikidata:SPARQL query service/queries/examples",
as_wikitext = TRUE
)
wikitext <- strsplit(content$parse$wikitext$`*`, "\n")[[1]]
wikitext <- wikitext[wikitext != ""]
examples <- purrr::map(example_name, function(example_name) {
regex <- paste0(
"^={2,}\\s?()?\\s?",
rex::escape(example_name),
"\\s?()?\\s?={2,}$"
)
heading_line <- which(grepl(regex, wikitext, fixed = FALSE))
start_line <- which(grepl("{{SPARQL", wikitext[(heading_line + 1):length(wikitext)], fixed = TRUE))[1]
end_line <- which(grepl("}}", wikitext[(heading_line + start_line + 1):length(wikitext)], fixed = TRUE))[1]
query <- paste0(wikitext[(heading_line + start_line):(heading_line + start_line + end_line - 1)], collapse = "\n")
return(sub("^\\s*\\{\\{SPARQL2?\\n?\\|query\\=", "", query))
})
names(examples) <- example_name
return(examples)
}
WikidataQueryServiceR/R/zzz.R 0000644 0001762 0000144 00000000204 13672174212 015662 0 ustar ligges users .onAttach <- function(libname, pkgname) {
packageStartupMessage("See ?WDQS for resources on Wikidata Query Service and SPARQL")
}
WikidataQueryServiceR/R/deprecated.R 0000644 0001762 0000144 00000000672 13101761457 017136 0 ustar ligges users #' @title Deprecated functions
#' @description Why did I have to go and make things so deprecated?
#' @name WDQSR-deprecated
NULL
#' @inheritParams get_example
#' @param ... ignored (kept for backwards-compatibility)
#' @describeIn WDQSR-deprecated use [get_example] instead which employs [WikipediR::page_content]
#' @export
scrape_example <- function(example_name, ...) {
.Deprecated("get_example")
return(get_example(example_name))
}
WikidataQueryServiceR/R/query.R 0000644 0001762 0000144 00000006673 13672170521 016211 0 ustar ligges users #' @title Send one or more SPARQL queries to WDQS
#' @description Makes a POST request to Wikidata Query Service SPARQL endpoint.
#' @param sparql_query SPARQL query (can be a vector of queries)
#' @param format "simple" uses CSV and returns pure character data frame, while
#' "smart" fetches JSON-formatted data and returns a data frame with datetime
#' columns converted to `POSIXct`
#' @return A tibble data frame
#' @examples
#' sparql_query <- "SELECT
#' ?softwareVersion ?publicationDate
#' WHERE {
#' BIND(wd:Q206904 AS ?R)
#' ?R p:P348 [
#' ps:P348 ?softwareVersion;
#' pq:P577 ?publicationDate
#' ] .
#' }"
#' query_wikidata(sparql_query)
#'
#' \dontrun{
#' query_wikidata(sparql_query, format = "smart")
#' }
#' @section Query limits:
#' There is a hard query deadline configured which is set to 60 seconds. There
#' are also following limits:
#' - One client (user agent + IP) is allowed 60 seconds of processing time each
#' 60 seconds
#' - One client is allowed 30 error queries per minute
#' See [query limits section](https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual#Query_limits)
#' in the WDQS user manual for more information.
#' @seealso [get_example]
#' @export
query_wikidata <- function(sparql_query, format = c("simple", "smart")) {
format <- format[1]
if (!format %in% c("simple", "smart")) {
stop("`format` must be either \"simple\" or \"smart\"")
}
output <- lapply(sparql_query, function(sparql_query) {
rate_limited_query <- wdqs_requester()
if (format == "simple") {
response <- rate_limited_query(sparql_query, httr::add_headers(Accept = "text/csv"))
httr::stop_for_status(response)
if (httr::http_type(response) == "text/csv") {
content <- httr::content(response, as = "text", encoding = "UTF-8")
return(readr::read_csv(content))
} else {
stop("returned response is not formatted as a CSV")
}
} else {
response <- rate_limited_query(sparql_query, httr::add_headers(Accept = "application/sparql-results+json"))
httr::stop_for_status(response)
if (httr::http_type(response) == "application/sparql-results+json") {
content <- httr::content(response, as = "text", encoding = "UTF-8")
temp <- jsonlite::fromJSON(content, simplifyVector = FALSE)
}
if (length(temp$results$bindings) > 0) {
data_frame <- purrr::map_dfr(temp$results$bindings, function(binding) {
return(purrr::map_chr(binding, ~ .x$value))
})
datetime_columns <- purrr::map_lgl(temp$results$bindings[[1]], function(binding) {
if ("datatype" %in% names(binding)) {
return(binding[["datatype"]] == "http://www.w3.org/2001/XMLSchema#dateTime")
} else {
return(FALSE)
}
})
data_frame <- dplyr::mutate_if(
.tbl = data_frame,
.predicate = datetime_columns,
.funs = as.POSIXct,
format = "%Y-%m-%dT%H:%M:%SZ", tz = "GMT"
)
} else {
data_frame <- dplyr::as_tibble(
matrix(
character(),
nrow = 0, ncol = length(temp$head$vars),
dimnames = list(c(), unlist(temp$head$vars))
)
)
}
return(data_frame)
}
})
if (length(output) == 1) {
return(output[[1]])
} else {
if (!is.null(names(sparql_query))) {
names(output) <- names(sparql_query)
} else {
names(output) <- NULL
}
return(output)
}
}
WikidataQueryServiceR/NEWS.md 0000644 0001762 0000144 00000001552 13672171571 015613 0 ustar ligges users # WikidataQueryServiceR 1.0.0
* Fixed example retrieval (was broken due to translation wikitext markers)
* Rate-limiting ([11](https://github.com/bearloga/WikidataQueryServiceR/issues/11))
* Using tidyverse family of packages (tibble, dplyr, purrr, readr)
* Various improvements and modernizations
# WikidataQueryServiceR 0.1.1
## Changes
* Updated documentation to use Markdown thanks to [roxygen2 6.0.0](https://blog.rstudio.org/2017/02/01/roxygen2-6-0-0/)
* Added more links for learning SPARQL in context of Wikidata
(see `help("WDQS", package = "WikidataQueryServiceR")`)
## Bug fixes
* Fixed a bug with JSON-formatted results ([#3](https://github.com/bearloga/WikidataQueryServiceR/issues/3))
# WikidataQueryServiceR 0.1.0
* Initial CRAN release:
- Support for multiple SPARQL queries
- Support for scraping example SPARQL queries via Suggested packages
WikidataQueryServiceR/MD5 0000644 0001762 0000144 00000001533 13672223432 015016 0 ustar ligges users ceba56d2055ac031b17ca565464bfe77 *DESCRIPTION
b4b561e5a9e75e00b9b876fcf290e67c *LICENSE
4ea8f312aa5eaeb106b3f0d27d296b50 *NAMESPACE
c18b8417c26cb61b233fbcfe00e5f6be *NEWS.md
fdd0a404e7d3447ad8dc5779db8c9b90 *R/WikidataQueryServiceR-package.R
cd753e662ea9d3801ed70390fbfba249 *R/deprecated.R
79a3f8b20b14916f66b77b04e43c41c0 *R/http.R
55fc5c4283eb40fe2c9068e14a1d85fd *R/query.R
a32586ab40f93abd9518da119f79b4aa *R/utils.R
49eee1545f4639c1c84d501979b71055 *R/zzz.R
74f4a36279c27455fe7c84eecaec9a0c *README.md
c2a3f3e761265ef40b23b27ac34d9ccf *man/WDQSR-deprecated.Rd
8daaf2bd62b503e5ff92e978e615910b *man/WikidataQueryServiceR-package.Rd
6b86be8313823e484666f55acdba3faf *man/get_example.Rd
b0a68be7d9df8034052f8ca79921466a *man/query_wikidata.Rd
b9c7ee06b1e051feb402933c5cb8a484 *tests/testthat.R
d99eb850e7bb4deb20c1ad6e14135537 *tests/testthat/test-query.R