WikidataQueryServiceR

This is an R wrapper for the Wikidata Query Service (WDQS) which provides a way for tools to query Wikidata via SPARQL (see the beta at https://query.wikidata.org/). It is written in and for R, and was inspired by Os Keyes’ WikipediR and WikidataR packages.

Author: Mikhail Popov (Wikimedia Foundation) License: MIT Status: Active

Installation

install.packages("WikidataQueryServiceR")

To install the development version:

# install.packages("remotes")
remotes::install_github("bearloga/WikidataQueryServiceR")

Usage

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) “film” (Q11424) that has the label “The Cabin in the Woods” (Q45394), get its genres (P136), and then use WDQS label service to return the genre labels.

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". }
}')
genregenreLabel
http://www.wikidata.org/entity/Q3072049zombie film
http://www.wikidata.org/entity/Q471839science fiction film
http://www.wikidata.org/entity/Q859369comedy-drama
http://www.wikidata.org/entity/Q1342372monster film
http://www.wikidata.org/entity/Q853630slasher film
http://www.wikidata.org/entity/Q224700comedy horror

For more example SPARQL queries, see this page on Wikidata.

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-based function for getting SPARQL queries from the WDQS examples page.

sparql_query <- get_example(c("Cats", "How many states this US state borders"))
sparql_query[["How many states this US state borders"]]
 #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:

results <- query_wikidata(sparql_query)
lapply(results, dim)
## $Cats
## [1] 147   2
## 
## $`How many states this US state borders`
## [1] 48  3
head(results$`How many states this US state borders`)
statestateLabelborders
http://www.wikidata.org/entity/Q1509Tennessee8
http://www.wikidata.org/entity/Q1581Missouri8
http://www.wikidata.org/entity/Q1261Colorado7
http://www.wikidata.org/entity/Q1603Kentucky7
http://www.wikidata.org/entity/Q1400Pennsylvania6
http://www.wikidata.org/entity/Q1211South Dakota6

Links for learning SPARQL

Additional Information

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copy Link

Version

Down Chevron

Install

install.packages('WikidataQueryServiceR')

Monthly Downloads

25,962

Version

1.0.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

June 16th, 2020

Functions in WikidataQueryServiceR (1.0.0)