Build functions making use of glitter + {reference to R object}
Let's say I have a list of plants
plants=c("wd:Q15537225","wd:Q109995","wd:Q148097","wd:Q159748")
I would like to apply the following function (which returns the plants' vernacular names and associated languages) to these Wikidata IDs:
get_vernac=function(wd_id){ result=spq_init() %>% spq_add("{wd_id} wdt:P1843 ?statement") %>% spq_mutate(c("?lang"="LANG(?statement)")) %>% send() return(result) } purrr::map(plants, get_vernac)
It would be a step towards being able to vectorize queries... But right now I can't manage to correctly work with environments (I guess) so that the lines above return an error (wd_id not found) ...
The use of "{R_object_name}" inside a query only works when the glitter functions are called directly from the global environment...
spq() from #41 would help with this.
Actually the snipped above did not include glue() nor sprintf(). The code below works (with the version that's on the default branch):
library("glitter")
get_vernac = function(wd_id){
result = spq_init() %>%
spq_add(sprintf("%s wdt:P1843 ?statement", wd_id)) %>%
spq_mutate(c("?lang"="LANG(?statement)")) %>%
spq_perform()
result$wd_id <- wd_id
result
}
plants = c("wd:Q15537225","wd:Q109995","wd:Q148097","wd:Q159748")
purrr::map_df(plants, get_vernac)
#> # A tibble: 113 × 3
#> statement lang wd_id
#> <chr> <chr> <chr>
#> 1 kokoška červenavá cs wd:Q15537225
#> 2 pwrs-y-bugail gwridog cy wd:Q15537225
#> 3 rusolutukka fi wd:Q15537225
#> 4 Rood herderstasje nl wd:Q15537225
#> 5 Pucele wa wd:Q109995
#> 6 Snowdrop en wd:Q109995
#> 7 snowdrop en wd:Q109995
#> 8 perce-neige fr wd:Q109995
#> 9 Zvonček sl wd:Q109995
#> 10 Lliri de neu ca wd:Q109995
#> # … with 103 more rows
Created on 2022-02-25 by the reprex package (v2.0.1)
With #40 the code will become
library("glitter")
get_vernac = function(wd_id){
result = spq_init() %>%
spq_add(sprintf("%s wdt:P1843 ?statement", wd_id)) %>%
spq_mutate(lang = lang(statement)) %>%
spq_perform()
result$wd_id <- wd_id
result
}
plants = c("wd:Q15537225","wd:Q109995","wd:Q148097","wd:Q159748")
purrr::map_df(plants, get_vernac)
library("glitter")
get_vernac = function(wd_id){
result = spq_init() %>%
spq_add(sprintf("%s wdt:P1843 ?statement", wd_id)) %>%
spq_mutate(lang = lang(statement)) %>%
spq_perform()
result$wd_id <- wd_id
result
}
plants = c("wd:Q15537225","wd:Q109995","wd:Q148097","wd:Q159748")
purrr::map_df(plants, get_vernac)
#> # A tibble: 118 × 3
#> statement lang wd_id
#> <chr> <chr> <chr>
#> 1 kokoška červenavá cs wd:Q15537225
#> 2 pwrs-y-bugail gwridog cy wd:Q15537225
#> 3 rusolutukka fi wd:Q15537225
#> 4 Rood herderstasje nl wd:Q15537225
#> 5 Pucele wa wd:Q109995
#> 6 Snowdrop en wd:Q109995
#> 7 snowdrop en wd:Q109995
#> 8 perce-neige fr wd:Q109995
#> 9 Zvonček sl wd:Q109995
#> 10 Lliri de neu ca wd:Q109995
#> # ℹ 108 more rows
Created on 2023-07-06 with reprex v2.0.2
@lvaudor can we close this? or is an example needed in the docs? if so where?
Yes I'd like such an example in the documentation... We should maybe re-think the vignettes we wish to make? For now it'd go in the first (Wikidata) vignette.