Closed BBetz907 closed 1 year ago
I think you want get_outtable()
. This should fetch an entire table for you. From that, you can use the different columns country_lvl
, facility_lvl
, community_lvl
, psnu_lvl
to pass to the get_ouorguids()
function. See below as an example for Zambia. Hopefully this is not too far off from what you need?
library(grabr)
library(tidyverse)
load_secrets()
# Fetch the OU table that identifies levels for country, facility, community, psnu
df_ou <- get_outable()
ou <- "Zambia"
# Filter down just to Zambia
df_ou_zmb <-
df_ou %>%
filter(operatingunit == ou)
ou_uid <- df_ou_zmb %>% pull(operatingunit_uid)
# Fetch a dataframe of PSNU uids
df_zmb_psnu <-
get_ouorguids(ouuid = ou_uid,
level = df_ou_zmb$psnu_lvl) %>%
as_tibble() %>%
rename(psnuuid = value)
# Fetch a dataframe of facility UIDS
df_zmb_facilites <-
get_ouorguids(ouuid = ou_uid,
level = df_ou_zmb$facility_lvl) %>%
as_tibble() %>%
rename(facilityuid = value)
Here is another example:
org_url <- "https://www.datim.org/api/sqlViews/DataExchOUs/data?format=json"
df_ous <- grabr::get_outable(
username = glamr::datim_user(),
password = glamr::datim_pwd()
)
list_orgs <- df_ous %>%
filter(country %in% c("Zambia")) %>%
pull(country_iso) %>%
paste0(org_url, "&var=OU:", .) %>%
httr::GET(httr::authenticate(user = glamr::datim_user(),
password = glamr::datim_pwd())) %>%
httr::content("text") %>%
jsonlite::fromJSON(flatten=TRUE)
df_orgs <- tibble::as_tibble(list_orgs$listGrid$rows) %>%
setNames(list_orgs$listGrid$headers$name) %>%
rename_with(.cols = contains("internal_id"),
.fn = ~str_replace(., "internal_id", "uid"))
df_orgs %>% glimpse()
Thank you both! @baboyma your example here gets me exactly what I was hoping to obtain! Thank you so much!!
@achafetz @baboyma @tessam30 is there a way to pull orgunituids for multiple levels at once using grabr? The get_ouuids() function is doing me right as I try to match orgunit_names and levels using specific levels (e.g. 6 or 7) but I run into problems for OUs like Tanzania where multiple level 5 orgunits have orgunits with the same name for level 6 (e.g. Arusha CC, Kiteto DC, etc. have an orgunit named Kaloleni). If I were to use the datim metadata I would have no trouble since that links orgunits to their parent levels but I wasn't sure how to read in this table without downloading first. (I'm doing this for like 10 OUs otherwise I'd just use the downloaded metadata csv file). Any suggestions on how to proceed with grabr or to code in my DATIM credentials into a read_csv() step? I thought maybe one of the datim_dimensions() or datimdim...() functions might yield an answer but no luck. Thanks!