Arrange results by variable value
Arguments
- .query
a list with elements of the query
- ...
variables by which to arrange (or SPARQL strings escaped with
spq()
, or strings, see examples)- .replace
whether to replace the pre-existing arranging
Examples
# descending length, ascending item_label, "R" syntax
spq_init() %>%
spq_add("?item wdt:P31/wdt:P279* wd:Q4022") %>%
spq_label(item) %>%
spq_add("?item wdt:P2043 ?length") %>%
spq_add("?item wdt:P625 ?location") %>%
spq_arrange(desc(length), item_label) %>%
spq_head(50)
#> PREFIX wd: <http://www.wikidata.org/entity/>
#> PREFIX wdt: <http://www.wikidata.org/prop/direct/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?length ?location
#> WHERE {
#>
#> ?item wdt:P31/wdt:P279* wd:Q4022.
#> OPTIONAL {
#> ?item rdfs:label ?item_labell.
#> FILTER(lang(?item_labell) IN ('en'))
#> }
#>
#> ?item wdt:P2043 ?length.
#> ?item wdt:P625 ?location.
#>
#> }
#> ORDER BY ?item_label DESC(?length)
#> LIMIT 50
# descending length, ascending item_label,
# "R" syntax with quotes e.g. for a loop
variable = "length"
spq_init() %>%
spq_add("?item wdt:P31/wdt:P279* wd:Q4022") %>%
spq_label(item) %>%
spq_add("?item wdt:P2043 ?length") %>%
spq_add("?item wdt:P625 ?location") %>%
spq_arrange(sprintf("desc(%s)", variable), item_label) %>%
spq_head(50)
#> PREFIX wd: <http://www.wikidata.org/entity/>
#> PREFIX wdt: <http://www.wikidata.org/prop/direct/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?length ?location
#> WHERE {
#>
#> ?item wdt:P31/wdt:P279* wd:Q4022.
#> OPTIONAL {
#> ?item rdfs:label ?item_labell.
#> FILTER(lang(?item_labell) IN ('en'))
#> }
#>
#> ?item wdt:P2043 ?length.
#> ?item wdt:P625 ?location.
#>
#> }
#> ORDER BY ?item_label DESC(?length)
#> LIMIT 50
# descending length, ascending item_label, SPARQL syntax
spq_init() %>%
spq_add("?item wdt:P31/wdt:P279* wd:Q4022") %>%
spq_label(item) %>%
spq_add("?item wdt:P2043 ?length") %>%
spq_add("?item wdt:P625 ?location") %>%
spq_arrange(spq("DESC(?length) ?item_label")) %>%
spq_head(50)
#> PREFIX wd: <http://www.wikidata.org/entity/>
#> PREFIX wdt: <http://www.wikidata.org/prop/direct/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?length ?location
#> WHERE {
#>
#> ?item wdt:P31/wdt:P279* wd:Q4022.
#> OPTIONAL {
#> ?item rdfs:label ?item_labell.
#> FILTER(lang(?item_labell) IN ('en'))
#> }
#>
#> ?item wdt:P2043 ?length.
#> ?item wdt:P625 ?location.
#>
#> }
#> ORDER BY DESC(?length) ?item_label
#> LIMIT 50
# descending xsd:integer(mort), R syntax
spq_init() %>%
spq_add("?oeuvre dcterms:creator ?auteur") %>%
spq_add("?auteur bio:death ?mort") %>%
spq_add("?auteur foaf:familyName ?nom") %>%
spq_filter(as.integer(mort) < as.integer("1924")) %>%
spq_group_by(auteur, nom, mort) %>%
spq_arrange(desc(as.integer(mort)))
#> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
#> PREFIX bio: <http://vocab.org/bio/0.1/>
#> PREFIX dcterms: <http://purl.org/dc/terms/>
#> SELECT ?auteur ?mort ?nom ?oeuvre
#> WHERE {
#>
#> ?oeuvre dcterms:creator ?auteur.
#> ?auteur bio:death ?mort.
#> ?auteur foaf:familyName ?nom.
#> FILTER(xsd:integer(?mort)<xsd:integer("1924"))
#> }
#> GROUP BY ?auteur ?mort ?nom
#> ORDER BY DESC(xsd:integer(?mort))
# descending as.integer(mort), SPARQL syntax
spq_init() %>%
spq_add("?oeuvre dcterms:creator ?auteur") %>%
spq_add("?auteur bio:death ?mort") %>%
spq_add("?auteur foaf:familyName ?nom") %>%
spq_filter(as.integer(mort) < as.integer("1924")) %>%
spq_group_by(auteur, nom, mort) %>%
spq_arrange(spq("DESC(xsd:integer(?mort))"))
#> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
#> PREFIX bio: <http://vocab.org/bio/0.1/>
#> PREFIX dcterms: <http://purl.org/dc/terms/>
#> SELECT ?auteur ?mort ?nom ?oeuvre
#> WHERE {
#>
#> ?oeuvre dcterms:creator ?auteur.
#> ?auteur bio:death ?mort.
#> ?auteur foaf:familyName ?nom.
#> FILTER(xsd:integer(?mort)<xsd:integer("1924"))
#> }
#> GROUP BY ?auteur ?mort ?nom
#> ORDER BY DESC(xsd:integer(?mort))
# Usage of the .replace argument
# .replace = FALSE (default)
spq_init() %>%
spq_add("?item wdt:P31/wdt:P279* wd:Q4022") %>%
spq_label(item) %>%
spq_add("?item wdt:P2043 ?length") %>%
spq_add("?item wdt:P625 ?location") %>%
spq_arrange(desc(length)) %>%
spq_arrange(location) %>%
spq_head(50)
#> PREFIX wd: <http://www.wikidata.org/entity/>
#> PREFIX wdt: <http://www.wikidata.org/prop/direct/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?length ?location
#> WHERE {
#>
#> ?item wdt:P31/wdt:P279* wd:Q4022.
#> OPTIONAL {
#> ?item rdfs:label ?item_labell.
#> FILTER(lang(?item_labell) IN ('en'))
#> }
#>
#> ?item wdt:P2043 ?length.
#> ?item wdt:P625 ?location.
#>
#> }
#> ORDER BY DESC(?length) ?location
#> LIMIT 50
# .replace = TRUE
spq_init() %>%
spq_add("?item wdt:P31/wdt:P279* wd:Q4022") %>%
spq_label(item) %>%
spq_add("?item wdt:P2043 ?length") %>%
spq_add("?item wdt:P625 ?location") %>%
spq_arrange(desc(length)) %>%
spq_arrange(location, .replace = TRUE) %>%
spq_head(50)
#> PREFIX wd: <http://www.wikidata.org/entity/>
#> PREFIX wdt: <http://www.wikidata.org/prop/direct/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?length ?location
#> WHERE {
#>
#> ?item wdt:P31/wdt:P279* wd:Q4022.
#> OPTIONAL {
#> ?item rdfs:label ?item_labell.
#> FILTER(lang(?item_labell) IN ('en'))
#> }
#>
#> ?item wdt:P2043 ?length.
#> ?item wdt:P625 ?location.
#>
#> }
#> ORDER BY ?location
#> LIMIT 50
# Mixing syntaxes
spq_init() %>%
spq_add("?item wdt:P31/wdt:P279* wd:Q4022") %>%
spq_label(item) %>%
spq_add("?item wdt:P2043 ?length") %>%
spq_add("?item wdt:P625 ?location") %>%
spq_arrange(desc(length), spq("?location")) %>%
spq_head(50)
#> PREFIX wd: <http://www.wikidata.org/entity/>
#> PREFIX wdt: <http://www.wikidata.org/prop/direct/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?length ?location
#> WHERE {
#>
#> ?item wdt:P31/wdt:P279* wd:Q4022.
#> OPTIONAL {
#> ?item rdfs:label ?item_labell.
#> FILTER(lang(?item_labell) IN ('en'))
#> }
#>
#> ?item wdt:P2043 ?length.
#> ?item wdt:P625 ?location.
#>
#> }
#> ORDER BY DESC(?length) ?location
#> LIMIT 50