You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 12, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+38-37Lines changed: 38 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,41 @@
1
1
# WikidataQueryServiceR
2
2
3
+
[](http://www.repostatus.org/#active)
[](https://cran.r-project.org/package=WikidataQueryServiceR)
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.
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.
|http://www.wikidata.org/entity/Q1342372|monster film |
35
39
36
40
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).
37
41
@@ -43,8 +47,8 @@ This package does not rely on the [rvest](https://cran.r-project.org/package=rve
43
47
44
48
```R
45
49
# install.packages(c("rvest", "urltools"))
46
-
sparql_query<- scrape_example("Largest cities with female mayor")
47
-
cat(sparql_query)
50
+
sparql_query<- scrape_example(c("Cats", "Horses", "Largest cities with female mayor"))
51
+
cat(sparql_query[["Largest cities with female mayor"]])
48
52
```
49
53
50
54
```SPARQL
@@ -75,8 +79,14 @@ ORDER BY DESC(?population)
75
79
LIMIT 10
76
80
```
77
81
82
+
Now we can run all three scraped SPARQL queries and get back three data.frames:
83
+
78
84
```R
79
-
query_wikidata(sparql_query) # 10 rows were returned by WDQS
85
+
results<- query_wikidata(sparql_query)
86
+
# 113 rows were returned by WDQS
87
+
# 6677 rows were returned by WDQS
88
+
# 10 rows were returned by WDQS
89
+
results$`Largest cities with female mayor`
80
90
```
81
91
82
92
|city |cityLabel |mayor |mayorLabel |
@@ -92,22 +102,13 @@ query_wikidata(sparql_query) # 10 rows were returned by WDQS
If you are planning on scraping multiple queries, please provide them all as a single vector instead of `s`/`v`/`l`-`apply`ing `scrape_example` because behind the scenes the function scrapes the examples page once and then it can really quickly extract all the queries from a single cache.
105
+
## Installation
96
106
97
-
**Note** that since `query_wikidata` can also operate on a vector of queries, you can easily create chains:
107
+
This R package depends on [httr](https://cran.r-project.org/package=httr), [dplyr](https://cran.r-project.org/package=dplyr), and [jsonlite](https://cran.r-project.org/package=jsonlite) R packages (and their dependencies).
98
108
99
109
```R
100
-
# Results will be a named list of data frames
101
-
# the names being the original example names.
102
-
library(magrittr)
103
-
results<- c("Cats", "Horses", "Largest cities with female mayor") %>%
104
-
scrape_example %>%
105
-
query_wikidata
110
+
install.packages("WikidataQueryServiceR")
106
111
```
107
-
108
-
## Installation
109
-
110
-
This R package depends on [httr](https://cran.r-project.org/package=httr), [dplyr](https://cran.r-project.org/package=dplyr), and [jsonlite](https://cran.r-project.org/package=jsonlite) R packages (and their dependencies).
0 commit comments