Skip to contents

ridl

# ridl(action ="package_search", as.list("cbi"))

container_show

# americasdataset <- container_show( id = "americas-regional-dataset")

container_list


# catalog <- container_list()
# groups_name <- catalog |>
#                   dplyr::select(groups_name) |>
#                   dplyr::distinct()

find_child_containers

#catalog <- container_list()
# containerAmericas <- find_child_containers(parent = "americas",
#                                            catalog = catalog)

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
#> $title
#> [1] "Motor Trend Car Road Tests"
#> 
#> $name
#> [1] "mtcars"
#> 
#> $notes
#> [1] "The data was extracted from the 1974 Motor Trend \n                      US magazine, and comprises fuel consumption and 10 aspects\n                      of automobile design and performance for 32 automobiles \n                      (1973–74 models)."
#> 
#> $owner_org
#> [1] "americas"
#> 
#> $visibility
#> [1] "public"
#> 
#> $external_access_level
#> [1] "open_access"
#> 
#> $data_collector
#> [1] "Motor Trend"
#> 
#> $keywords
#> Environment       Other 
#>        "11"        "54" 
#> 
#> $unit_of_measurement
#> [1] "car"
#> 
#> $geographies
#> [1] "UNSPECIFIED"
#> 
#> $data_collection_technique
#> [1] "oth"
#> 
#> $archived
#> [1] "False"

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
#> # A tibble: 1 × 12
#>   title    name  notes owner_org visibility external_access_level data_collector
#>   <chr>    <chr> <chr> <chr>     <chr>      <chr>                 <chr>         
#> 1 Motor T… mtca… "The… americas  public     open_access           Motor Trend   
#> # ℹ 5 more variables: keywords <list>, unit_of_measurement <chr>,
#> #   geographies <chr>, data_collection_technique <chr>, archived <chr>

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)
#-----
# 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

#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
#> $type
#> [1] "data"
#> 
#> $url
#> [1] "mtcars.csv"
#> 
#> $name
#> [1] "mtcars.csv"
#> 
#> $format
#> [1] "csv"
#> 
#> $file_type
#> [1] "microdata"
#> 
#> $date_range_start
#> [1] "1973-01-01"
#> 
#> $date_range_end
#> [1] "1973-12-31"
#> 
#> $visibility
#> [1] "public"
#> 
#> $version
#> [1] "1"
#> 
#> $process_status
#> [1] "raw"
#> 
#> $identifiability
#> [1] "anonymized_public"

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
#> # A tibble: 1 × 11
#>   type  url    name  format file_type date_range_start date_range_end visibility
#>   <chr> <chr>  <chr> <chr>  <chr>     <chr>            <chr>          <chr>     
#> 1 data  mtcar… mtca… csv    microdata 1973-01-01       1973-12-31     public    
#> # ℹ 3 more variables: version <chr>, process_status <chr>,
#> #   identifiability <chr>

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


## 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

## 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

# summary_report(year = 2022,
#                                    region = "Americas")