Navigating the GSS StatsBank API is like exploring a file system - you start at the root and progressively drill down through databases and topics until you reach the table containing your desired data.
When you reach a table, the response changes from a list of navigation items to detailed metadata including available variables, their codes, and possible values.
To simplify navigation, you can create a helper function that builds URLs incrementally and displays available options at each level:
build_url = function(URL, ...) { path = list(...) req = request(URL) # Add each folder name to the URL path incrementally full_req = purrr::reduce(path, req_url_path_append, .init = req) # Check if the last part of the path ends in ".px" (indicating a table) is_table = FALSE if (length(path) > 0) { if (grepl("\\.px$", path[[length(path)]], ignore.case = TRUE)) { is_table = TRUE } } # Fetch the content to see what is inside (GET request) response = req_perform(full_req) body = resp_body_json(response) if (is_table) { # If it is a table, print the variable names (metadata) message("Endpoint reached: Table found.") message("Available variables:") print(map_chr(body$variables, "code")) } else { # If it is a folder/database, list the children IDs key = if(length(path) == 0) "dbid" else "id" print(map_chr(body, key)) } # Return the request object to be assigned to a variable return(full_req) }
[1] "Annual Household Income and Expenditure Survey (AHIES)"[2] "Education(Admin)"[3] "Ghana Census of Agriculture (GCA)"[4] "GLSS7"[5] "PHC 2021 StatsBank"[6] "PHC2010"[7] "Trade"
Explore Topics in a Database
# List topics in PHC 2021build_url(URL, "PHC 2021 StatsBank")
Output:
[1] "Difficulties in Performing Activities"[2] "Economic Activity"[3] "Education and Literacy"[4] "Fertility and Mortality"[5] "Housing"[6] "Water and Sanitation"
Explore Tables in a Topic
# List tables in Water and Sanitationbuild_url(URL, "PHC 2021 StatsBank", "Water and Sanitation")