Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Project Status: Active – The project has reached a stable, usable state and is being actively developed. CRAN_Status_Badge CRAN Total Downloads License: MIT

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 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



To install the development version:

# install.packages("remotes")


## 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
  ?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 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"]]
SELECT ?state ?stateLabel ?borders
    SELECT ?state (COUNT(?otherState) as ?borders)
    ?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`)
state stateLabel borders Tennessee 8 Missouri 8 Colorado 7 Kentucky 7 Pennsylvania 6 South Dakota 6

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.

You can’t perform that action at this time.