All Projects → wikimedia → WikidataQueryServiceR

wikimedia / WikidataQueryServiceR

Licence: other
An R package for the Wikidata Query Service API

Programming Languages

r
7636 projects

Projects that are alternatives of or similar to WikidataQueryServiceR

Taxize
A taxonomic toolbelt for R
Stars: ✭ 209 (+808.7%)
Mutual labels:  api-wrapper, rstats, r-package
worrms
World Register of Marine Species R client
Stars: ✭ 13 (-43.48%)
Mutual labels:  api-wrapper, rstats, r-package
rredlist
IUCN Red List API Client
Stars: ✭ 31 (+34.78%)
Mutual labels:  api-wrapper, rstats, r-package
Gistr
Interact with GitHub gists from R
Stars: ✭ 90 (+291.3%)
Mutual labels:  api-wrapper, rstats, r-package
Rorcid
A programmatic interface the Orcid.org API
Stars: ✭ 101 (+339.13%)
Mutual labels:  api-wrapper, rstats, r-package
Ckanr
R client for the CKAN API
Stars: ✭ 91 (+295.65%)
Mutual labels:  api-wrapper, rstats, r-package
kaggler
🏁 API client for Kaggle
Stars: ✭ 50 (+117.39%)
Mutual labels:  api-wrapper, rstats, r-package
Rcrossref
R client for various CrossRef APIs
Stars: ✭ 137 (+495.65%)
Mutual labels:  api-wrapper, rstats, r-package
cablecuttr
An R wrapper for CanIStream.It API
Stars: ✭ 17 (-26.09%)
Mutual labels:  api-wrapper, rstats, r-package
heddlr
Bring a functional programming mindset to R Markdown document generation
Stars: ✭ 14 (-39.13%)
Mutual labels:  rstats, r-package
agent
Store sensitive data such as API tokens
Stars: ✭ 19 (-17.39%)
Mutual labels:  rstats, r-package
rfishbase
R interface to the fishbase.org database
Stars: ✭ 79 (+243.48%)
Mutual labels:  rstats, r-package
geoparser
⛔ ARCHIVED ⛔ R package for the Geoparser.io API
Stars: ✭ 38 (+65.22%)
Mutual labels:  rstats, r-package
cusumcharter
Easier CUSUM control charts. Returns simple CUSUM statistics, CUSUMs with control limit calculations, and function to generate faceted CUSUM Control Charts
Stars: ✭ 17 (-26.09%)
Mutual labels:  rstats, r-package
rLandsat
R Package to make Landsat8 data accessible
Stars: ✭ 95 (+313.04%)
Mutual labels:  api-wrapper, rstats
cranlogs
Download Logs from the RStudio CRAN Mirror
Stars: ✭ 70 (+204.35%)
Mutual labels:  rstats, r-package
pkgreviewr
R package to facilitate rOpenSci package reviews.
Stars: ✭ 35 (+52.17%)
Mutual labels:  rstats, r-package
individual
R Package for individual based epidemiological models
Stars: ✭ 20 (-13.04%)
Mutual labels:  rstats, r-package
SPARQL
Lib PHP for SPARQL 1.1
Stars: ✭ 23 (+0%)
Mutual labels:  sparql, wikidata
styles
Custom themes for base plots
Stars: ✭ 32 (+39.13%)
Mutual labels:  rstats, r-package

WikidataQueryServiceR

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 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("wikimedia/WikidataQueryServiceR@main")

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". }
}')
genre genreLabel
http://www.wikidata.org/entity/Q3072049 zombie film
http://www.wikidata.org/entity/Q471839 science fiction film
http://www.wikidata.org/entity/Q859369 comedy-drama
http://www.wikidata.org/entity/Q1342372 monster film
http://www.wikidata.org/entity/Q853630 slasher film
http://www.wikidata.org/entity/Q224700 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
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`)
state stateLabel borders
http://www.wikidata.org/entity/Q1509 Tennessee 8
http://www.wikidata.org/entity/Q1581 Missouri 8
http://www.wikidata.org/entity/Q1261 Colorado 7
http://www.wikidata.org/entity/Q1603 Kentucky 7
http://www.wikidata.org/entity/Q1400 Pennsylvania 6
http://www.wikidata.org/entity/Q1211 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].