--- title: "Package Functions" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{package-functions} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{r setup} library(riddle) ``` # ridl ```{r example-ridl} # ridl(action ="package_search", as.list("cbi")) ``` # container_show ```{r example-container_show} # americasdataset <- container_show( id = "americas-regional-dataset") ``` # container_list ```{r example-container_list} # catalog <- container_list() # groups_name <- catalog |> # dplyr::select(groups_name) |> # dplyr::distinct() ``` # find_child_containers ```{r example-find_child_containers} #catalog <- container_list() # containerAmericas <- find_child_containers(parent = "americas", # catalog = catalog) ``` # dataset_metadata ```{r example-dataset_metadata} m <- dataset_metadata(title = "Motor Trend Car Road Tests", name = "mtcars", notes = "The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).", owner_org = "americas", visibility = "public", geographies = "UNSPECIFIED", external_access_level = "open_access", data_collector = "Motor Trend", keywords = keywords[c("Environment", "Other")], unit_of_measurement = "car", data_collection_technique = "oth", archived = "False") m ``` # dataset_tibblify ```{r example-dataset_tibblify} m <- dataset_metadata(title = "Motor Trend Car Road Tests", name = "mtcars", notes = "The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).", owner_org = "americas", ## becarefull- all lower case!!! visibility = "public", geographies = "UNSPECIFIED", external_access_level = "open_access", data_collector = "Motor Trend", keywords = keywords[c("Environment", "Other")], unit_of_measurement = "car", data_collection_technique = "oth", archived = "False") m1 <- dataset_tibblify(m) m1 ``` # dataset ```{r example-dataset} #----- # test search in prod Sys.unsetenv("USE_UAT") # riddle::dataset_show(id = "unhcr-cbi-americas-quarterly-report") # # p <- riddle::dataset_show('rms_v4') # list_of_ressources <- p[["resources"]][[1]] # list_of_ressources #----- # Test create in UAT Sys.setenv(USE_UAT=1) m <- riddle::dataset_metadata(title = "Testing Riddle Interface", name = "riddleapitest", notes = "Making an API test", owner_org = "americas", ## be careful- all lower case!!! visibility = "public", geographies = "UNSPECIFIED", external_access_level = "open_access", data_collector = "Motor Trend", keywords = keywords[c("Environment", "Other")], unit_of_measurement = "car", data_collection_technique = "oth", archived = "False") # ## For the above to work - you need to make sure you have at least editor access # to the corresponding container - i.e. owner_org = "exercise-container" # p <- dataset_create(metadata = m) # The return value is a representation of the dataset we just created in # RIDL that you could inspect like any other R object. # p ## Now deleting this! # dataset_delete(id = p$id) #----- # Test create in prod Sys.unsetenv("USE_UAT") # m1 <- riddle::dataset_metadata(title = "Test", # name = "Test", # notes = "The data was extracted from kobo.", # owner_org = "americas-regional-dataset", # visibility = "public", # geographies = "UNSPECIFIED", # external_access_level = "open_access", # data_collector = "UNHCR", # keywords = keywords[c("Environment", "Other")], # unit_of_measurement = "car", # data_collection_technique = "oth", # archived = "False") # p <- riddle::dataset_create(metadata = m1) ``` # dataset_search ```{r example-dataset_search} #----- # Test search in prod # Sys.unsetenv("USE_UAT") # searching <- "cbi" # p <- dataset_search(q = searching, rows = 30) # p #----- # Test create in UAT Sys.setenv(USE_UAT=1) # p2 <- dataset_search(q = "testedouard2") ``` # resource_metadata ```{r example-resource_metadata} #resource_metadata() m <- riddle::resource_metadata(type = "data", url = "mtcars.csv", name = "mtcars.csv", format = "csv", file_type = "microdata", date_range_start = "1973-01-01", date_range_end = "1973-12-31", version = "1", visibility = "public", process_status = "raw", identifiability = "anonymized_public") m ``` # resource_tibblify ```{r example-resource_tibblify} m <- riddle::resource_metadata(type = "data", url = "mtcars.csv", # upload = httr::upload_file(system.file("extdata/mtcars.csv", package = "readr")), name = "mtcars.csv", format = "csv", file_type = "microdata", date_range_start = "1973-01-01", date_range_end = "1973-12-31", version = "1", visibility = "public", process_status = "raw", identifiability = "anonymized_public") m1 <- riddle::resource_tibblify(m) m1 ``` # resource ```{r example-resource} # ## Full example available with the fetch function.. #----- # ## Test search in prod # Sys.unsetenv("USE_UAT") # p <- dataset_search("rms_v4") # p # list_of_resources <- p[["resources"]][[1]] # knitr::kable(list_of_resources) #----- # ## Test search in uat # Sys.setenv(USE_UAT=1) # p <- dataset_search("tests") # p # ##take the first one # ridlid <- as.character(p[9, c("id")]) #----- # ## Test resource in UAT # Sys.setenv(USE_UAT=1) # m <- riddle::dataset_metadata(title = "Testing Riddle Interface", # name = "riddleapitest", # notes = "Making an API test", # owner_org = "americas", ## be careful- all lower case!!! # visibility = "public", # geographies = "UNSPECIFIED", # external_access_level = "open_access", # data_collector = "myself", # keywords = keywords[c("Environment", "Other")], # unit_of_measurement = "byte", # data_collection_technique = "oth", # archived = "False") # ## For the above to work - you need to make sure you have at least editor access # ## to the corresponding container - i.e. owner_org = "exercise-container" # p <- dataset_create(metadata = m) # p <- dataset_show('riddleapitest') # ## Now testing adding the file "resource.R" as an attachment # new_attachment <- riddle::resource_metadata(type = "attachment", # url = "resourceR", # upload = httr::upload_file(here::here("R","resource.R") ), # name = "Rscript", # format = "R", # file_type = "report", # version = "1", # visibility = "public" ) # r <- resource_create(package_id = p$id, res_metadata = new_attachment ) # resource_create(package_id = p$name, res_metadata = new_attachment ) # ## Like before, the return value is a tibble representation of the resource. # r # ## Another example with a data ressource # m <- riddle::resource_metadata(type = "data", # url = "mtcars.csv", # upload = httr::upload_file(system.file("extdata/mtcars.csv", package = "readr")), # name = "mtcars.csv", # format = "csv", # file_type = "microdata", # date_range_start = "1973-01-01", # date_range_end = "1973-12-31", # version = "1", # visibility = "public", # process_status = "raw", # identifiability = "anonymized_public") # r <- resource_create(package_id = p$id, # res_metadata = m ) # ## let's get again the details of the dataset we want to add the resource in.. # r # ## and now can search for it - checking it is correctly there... # resource_search("name:mtcarsriddle") # ## And once we’re done experimenting with the API, we should take down our # ## toy dataset since we don’t really need it on RIDL. # dataset_delete(p$id) # The return value is a representation of the dataset we just created in # RIDL that you could inspect like any other R object. # p ## Now deleting this! # dataset_delete(id = p$id) ``` # resource_fetch ```{r example-resource_fetch} ## Example 1: with a direct URL #----- # Test search in prod # Sys.unsetenv("USE_UAT") # resource_fetch(url = 'https://ridl.unhcr.org/dataset/a60f4b79-8acc-4893-8fb9-d52f94416b19/resource/daa2b9e4-bf97-4302-86a5-08bb62a5a937/download/df_age_2022.csv', # path = tempfile()) ## Example 2: Let's try to identify a resource - then fetch it locally and update it back... as from here # https://github.com/unhcr-americas/darien_gap_human_mobility/blob/main/report.Rmd#L38 # Sys.unsetenv("USE_UAT") # ## Get the dataset metadata based on its canonical name # p <- riddle::dataset_show('rms_v4') # ## Let's get the fifth resource within this dataset # test_ressources <- p[["resources"]][[1]] |> dplyr::slice(5) # # ## Download the resource locally in a file name file.. # resource_fetch(url = test_ressources$url, path = here::here("file")) # test_ressources$url # # Rebuild the metadata # m <- resource_metadata(type = test_ressources$type, #"data", # url = "df_gender_2020.csv", # upload = httr::upload_file(here::here("file")), # name = test_ressources$name, # "Irregular entries by gender in 2022", # format = test_ressources$format, #"csv", # file_type = test_ressources$file_type, #"microdata", # visibility = test_ressources$visibility, # "public", # date_range_start = test_ressources$date_range_start, # "2022-01-01", # date_range_end = test_ressources$date_range_end, #as.character(floor_date(today('America/Panama'), "month") - days(1)), #end day of last month # version = test_ressources$version, # "0", # process_status = test_ressources$process_status, #"anonymized", # identifiability = test_ressources$identifiability, #"anonymized_public" # ) #r <- resource_update(id = test_ressources$id, res_metadata = m) ``` # riddle_notebook ```{r example-riddle_notebook} ## Time to archive your work once done!! # used in the RIDL_Notebook markdown template in the package # if( params$publish == "yes"){ # namethisfile = basename(rstudioapi::getSourceEditorContext()$path ) # riddle_notebook(ridl = params$ridl, # datafolder = params$datafolder, # namethisfile = namethisfile , # visibility = params$visibility ) } ``` # summary_report ```{r example-summary_report} # summary_report(year = 2022, # region = "Americas") ```