|
| 1 | +--- |
| 2 | +title: "WikidataQueryServiceR" |
| 3 | +output: |
| 4 | + github_document: |
| 5 | + toc: true |
| 6 | + toc_depth: 3 |
| 7 | +--- |
| 8 | + |
| 9 | +```{r setup, include=FALSE} |
| 10 | +knitr::opts_chunk$set(echo = TRUE) |
| 11 | +# install.packages("printr", type = "source", repos = c("https://yihui.name/xran", CRAN = "https://cran.rstudio.com")) |
| 12 | +library(printr) |
| 13 | +``` |
| 14 | + |
| 15 | +[](http://www.repostatus.org/#active) |
| 16 | +[](https://cran.r-project.org/package=WikidataQueryServiceR) |
| 17 | +[](https://cran.r-project.org/package=WikidataQueryServiceR) |
| 18 | +[](https://opensource.org/licenses/MIT) |
| 19 | + |
| 20 | +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 https://query.wikidata.org/). It is written in and for R, and was inspired by Oliver Keyes' [WikipediR](https://github.com/Ironholds/WikipediR) and [WikidataR](https://github.com/Ironholds/WikidataR) packages. |
| 21 | + |
| 22 | +__Author:__ Mikhail Popov (Wikimedia Foundation)<br/> |
| 23 | +__License:__ [MIT](http://opensource.org/licenses/MIT)<br/> |
| 24 | +__Status:__ Active |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +```R |
| 29 | +install.packages("WikidataQueryServiceR") |
| 30 | +``` |
| 31 | + |
| 32 | +To install the development version: |
| 33 | + |
| 34 | +```R |
| 35 | +# install.packages(c("devtools", "httr", "dplyr", "jsonlite")) |
| 36 | +devtools::install_github("bearloga/WikidataQueryServiceR") |
| 37 | +``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +```{r load} |
| 42 | +library(WikidataQueryServiceR) |
| 43 | +``` |
| 44 | + |
| 45 | +You submit SPARQL queries using the `query_wikidata()` function. |
| 46 | + |
| 47 | +### Example: fetching genres of a particular movie |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +```{r wdqs_example, cache=TRUE} |
| 52 | +query_wikidata('SELECT DISTINCT |
| 53 | + ?genre ?genreLabel |
| 54 | +WHERE { |
| 55 | + ?film wdt:P31 wd:Q11424. |
| 56 | + ?film rdfs:label "The Cabin in the Woods"@en. |
| 57 | + ?film wdt:P136 ?genre. |
| 58 | + SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } |
| 59 | +}') |
| 60 | +``` |
| 61 | + |
| 62 | +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). |
| 63 | + |
| 64 | +`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. |
| 65 | + |
| 66 | +### Fetching queries from Wikidata's examples page |
| 67 | + |
| 68 | +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). |
| 69 | + |
| 70 | +```{r get_examples, cache=TRUE} |
| 71 | +sparql_query <- get_example(c("Cats", "Horses", "Largest cities with female mayor")) |
| 72 | +``` |
| 73 | +```{r, eval=FALSE} |
| 74 | +sparql_query[["Largest cities with female mayor"]] |
| 75 | +``` |
| 76 | +```{r, echo=FALSE, results='asis'} |
| 77 | +cat("```SPARQL\n", sparql_query[["Largest cities with female mayor"]], "\n```") |
| 78 | +``` |
| 79 | + |
| 80 | +Now we can run all three extracted SPARQL queries and get back three data.frames: |
| 81 | + |
| 82 | +```{r run_examples, cache=TRUE, dependson='get_examples'} |
| 83 | +results <- query_wikidata(sparql_query) |
| 84 | +results$`Largest cities with female mayor`[, c("cityLabel", "mayorLabel")] |
| 85 | +``` |
| 86 | + |
| 87 | +## Links for learning SPARQL |
| 88 | + |
| 89 | +- [A beginner-friendly course for SPARQL](https://www.wikidata.org/wiki/Wikidata:A_beginner-friendly_course_for_SPARQL) |
| 90 | +- Building a SPARQL query: [Museums on Instagram](https://www.wikidata.org/wiki/Help:SPARQL/Building_a_query/Museums_on_Instagram) |
| 91 | +- [SPARQL Query Examples](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples) for WDQS |
| 92 | +- [Using SPARQL to access Linked Open Data](http://programminghistorian.org/lessons/graph-databases-and-SPARQL) by Matthew Lincoln |
| 93 | +- Interesting or illustrative [SPARQL queries](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries) for Wikidata |
| 94 | +- Wikidata [2016 SPARQL Workshop](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/2016_SPARQL_Workshop) |
| 95 | +- [Wikidata SPARQL Query video tutorial](https://www.youtube.com/watch?v=1jHoUkj_mKw) by Navino Evans |
| 96 | +- _[Learning SPARQL](http://www.learningsparql.com/)_ by Bob DuCharme |
| 97 | +- [WDQS User Manual](https://www.mediawiki.org/wiki/Wikidata_query_service/User_Manual) |
| 98 | + |
| 99 | +## Additional Information |
| 100 | + |
| 101 | +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. |
0 commit comments