unhcr / koboloadeR

deprecated please use {kobocruncher} - https://edouard-legoupil.github.io/kobocruncher/
https://unhcr.github.io/koboloadeR/docs/
28 stars 28 forks source link

function to load indicator tabulation to HDX #12

Open Edouard-Legoupil opened 6 years ago

Edouard-Legoupil commented 6 years ago

If indicators is not classified as sensitive, the value should be pushed to an on line repository (CKAN based) such as http://data.humdata.org/

This probably requires to:

  1. tag the variable according to HXL: http://hxlstandard.org/
  2. upload it through the API: Replicating or embedding the python work here https://github.com/OCHA-DAP/hdx-python-api
dickoa commented 6 years ago

@Edouard-Legoupil thanks for the suggestion and I would be happy to make a PR to add this functionality. As suggested on option is to wrap the Python API, see below:

library(reticulate)
library(tidyverse)

### Example from the github page https://github.com/OCHA-DAP/hdx-python-api#getting-started
hdx <- import("hdx")
conf <- hdx$hdx_configuration$Configuration
conf$create(hdx_site = "demo", hdx_read_only = FALSE) ### demo

### You need to follow for your CKAN API Key https://github.com/OCHA-DAP/hdx-python-api#obtaining-your-api-key
config <- conf$read()
key <- config$get_api_key()

### Function to create dataset and resource
dataset <- hdx$data$dataset$Dataset
resource <- hdx$data$resource$Resource

### Create a dataset
meta <- list(name = "iris_flower",
            title = "Iris flower data set",
            notes = "Typical test case for classification algorithm",
            methodology = "Other",
            methodology_other = "Collected by Ronald Fisher",
            dataset_source = "Gondwana",
            subnational = 1,
            license_id = "hdx_other",
            private = FALSE,
            url = "http://dickoa.gitlab.io")

### We create the dataset from the dict
df <- dataset(r_to_py(meta))

### Need to add a maintainer, set org and other metadata
df$set_maintainer(key)
df$set_organization('5a63012e-6c41-420c-8c33-e84b277fdc90') ## Innago
df$set_dataset_date(as.character(Sys.Date()))
df$set_expected_update_frequency("Every two weeks")
df$add_country_location("MLI")
df$add_tags(r_to_py(list("Rstats", "test")))

### Create a resource and add it to the dataset
tmp_file <- tempfile(fileext = ".csv")
write_csv(iris, path = tmp_file)
res <- list(name = "iris.csv", format = "csv", description = "iris data csv")
res <- resource(r_to_py(res))
res$set_file_to_upload(tmp_file)

### add to the dataset
df$add_update_resource(r_to_py(res))

### Push
df$create_in_hdx()

screen_hdx