API Client Library for 'Wikidata Query Service'

An API client for the 'Wikidata Query Service' < https://query.wikidata.org/>.

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 https://query.wikidata.org/). It is written in and for R, and was inspired by Oliver Keyes' WikipediR and WikidataR packages.

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



To install the development version:

# install.packages(c("devtools", "httr", "dplyr", "jsonlite"))


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.

sparql_query <- 'PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT DISTINCT ?genre ?genreLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  ?film wdt:P31 wd:Q11424.
  ?film rdfs:label "The Cabin in the Woods"@en.
  ?film wdt:P136 ?genre.
# 5 rows were returned by WDQS
genre genreLabel
http://www.wikidata.org/entity/Q200092 horror film
http://www.wikidata.org/entity/Q471839 science fiction film
http://www.wikidata.org/entity/Q224700 comedy horror
http://www.wikidata.org/entity/Q859369 comedy-drama
http://www.wikidata.org/entity/Q1342372 monster film

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.

Extracting and running example SPARQL queries

This package does not rely on the rvest and urltools R packages for core functionality, but if the user has them installed then there is a bonus function for scraping the examples page and extracting SPARQL queries.

# install.packages(c("rvest", "urltools"))
sparql_query <- scrape_example(c("Cats", "Horses", "Largest cities with female mayor"))
cat(sparql_query[["Largest cities with female mayor"]])
# Largest cities with female mayor
#added before 2016-10
#TEMPLATE={"template":"Largest ?c with ?sex head of government","variables":{"?sex":{"query":" SELECT ?id WHERE { ?id wdt:P31 wd:Q48264 .  } "},"?c":{"query":"SELECT DISTINCT ?id WHERE {  ?c wdt:P31 ?id.  ?c p:P6 ?mayor. }"} } }
SELECT DISTINCT ?city ?cityLabel ?mayor ?mayorLabel 
  BIND(wd:Q6581072 AS ?sex)
  BIND(wd:Q515 AS ?c)
    ?city wdt:P31/wdt:P279* ?c .  # find instances of subclasses of city
    ?city p:P6 ?statement .            # with a P6 (head of goverment) statement
    ?statement ps:P6 ?mayor .          # ... that has the value ?mayor
    ?mayor wdt:P21 ?sex .       # ... where the ?mayor has P21 (sex or gender) female
    FILTER NOT EXISTS { ?statement pq:P582 ?x }  # ... but the statement has no P582 (end date) qualifier
    # Now select the population value of the ?city
    # (wdt: properties use only statements of "preferred" rank if any, usually meaning "current population")
    ?city wdt:P1082 ?population .
    # Optionally, find English labels for city and mayor:
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en" .
ORDER BY DESC(?population)

Now we can run all three scraped SPARQL queries and get back three data.frames:

results <- query_wikidata(sparql_query)
# 113 rows were returned by WDQS
# 6677 rows were returned by WDQS
# 10 rows were returned by WDQS
results$`Largest cities with female mayor`
city cityLabel mayor mayorLabel
http://www.wikidata.org/entity/Q1490 Tokyo http://www.wikidata.org/entity/Q261703 Yuriko Koike
http://www.wikidata.org/entity/Q1156 Mumbai http://www.wikidata.org/entity/Q18218029 Snehal Ambekar
http://www.wikidata.org/entity/Q38283 Yokohama http://www.wikidata.org/entity/Q529363 Fumiko Hayashi
http://www.wikidata.org/entity/Q2807 Madrid http://www.wikidata.org/entity/Q19592761 Manuela Carmena
http://www.wikidata.org/entity/Q11462 Surabaya http://www.wikidata.org/entity/Q12522317 Tri Rismaharini
http://www.wikidata.org/entity/Q220 Rome http://www.wikidata.org/entity/Q23766020 Virginia Raggi
http://www.wikidata.org/entity/Q90 Paris http://www.wikidata.org/entity/Q2851133 Anne Hidalgo
http://www.wikidata.org/entity/Q16555 Houston http://www.wikidata.org/entity/Q213847 Annise Parker
http://www.wikidata.org/entity/Q1563 Havana http://www.wikidata.org/entity/Q6774124 Marta Hernández Romero
http://www.wikidata.org/entity/Q19660 Bucharest http://www.wikidata.org/entity/Q16593781 Gabriela Fireaa

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.


WikidataQueryServiceR 0.1.1


  • Updated documentation to use Markdown thanks to 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)

WikidataQueryServiceR 0.1.0

  • Initial CRAN release:
    • Support for multiple SPARQL queries
    • Support for scraping example SPARQL queries via Suggested packages

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.1.1 by Mikhail Popov, 3 years ago


Report a bug at https://github.com/bearloga/WikidataQueryServiceR/issues

Browse source code at https://github.com/cran/WikidataQueryServiceR

Authors: Mikhail Popov [aut, cre] (@bearloga on Twitter) , Wikimedia Foundation [cph]

Documentation:   PDF Manual  

Task views: Web Technologies and Services

MIT + file LICENSE license

Imports httr, dplyr, jsonlite

Suggests rvest, urltools

See at CRAN