bcgov / bcdata

An R package for searching & retrieving data from the B.C. Data Catalogue
https://bcgov.github.io/bcdata
Apache License 2.0
81 stars 12 forks source link

Some facet values need to be quoted to make a valid API call using bcdc_search #315

Closed stephhazlitt closed 1 year ago

stephhazlitt commented 1 year ago

Searching for "Metadata for ICBC" through the web interface yields 18 records, e.g.,

Screenshot 2023-03-01 at 9 07 01 PM

Using bcdata::bcdc_search("Metadata for ICBC") returns 10 of the above records, but omits the eight where the record title begins with "Metadata for ICBC" and that all belong to one organization.

bcdata::bcdc_search("Metadata for ICBC")
#> List of B.C. Data Catalogue Records
#> Number of records: 10
#> Titles:
#> 1: BC Finance - 2013 Financial and Economic Review - Insurance
#>  Corporation of BC - Five-Year Income Statement for the Years Ended
#>  December 31 - 2008 to 2012 - Table 3.4 (xlsx)
#>  ID: 42f8d6ca-70f5-4e26-9708-f54b0c4122c4
#>  Name:
#>   bc-finance-2013-financial-and-economic-review-insurance-corporation-of-bc-five-year-income-statement
#> 2: Motor Vehicle Serious Injuries by Region (xlsx, csv)
#>  ID: c336d3ee-c8b3-46b5-a3f7-a934bbc2c3ce
#>  Name: motor-vehicle-serious-injuries-by-region
#> 3: Motor Vehicle Fatalities by Region (xlsx, csv)
#>  ID: f23f5ce0-44d4-4f7e-ab93-a9f5442d26c4
#>  Name: motor-vehicle-fatalities-by-region
#> 4: Unlicensed Driving Prohibitions (csv, xlsx)
#>  ID: 2c35ff92-cee8-4e14-8da7-f1f0b11174a6
#>  Name: unlicensed-driving-prohibitions
#> 5: Motor Vehicle Serious Injuries by Month (xlsx, csv)
#>  ID: 51f68b30-4d2d-4a22-a227-ee9878d0e9ea
#>  Name: motor-vehicle-serious-injuries-by-month
#> 6: Motor Vehicle Fatalities by Month (xlsx, csv)
#>  ID: 640aa193-3c74-4f87-b1c5-215a2d158b71
#>  Name: motor-vehicle-fatalities-by-month
#> 7: Motor Vehicle Serious Injuries with Alcohol and/or Drug Involvement
#>  (xlsx, csv)
#>  ID: e87b3585-c195-4ee4-b531-a919262816ce
#>  Name:
#>   motor-vehicle-serious-injuries-with-alcohol-and-or-drug-involvement
#> 8: Motor Vehicle Fatalities with Alcohol and/or Drug Involvement (xlsx,
#>  csv)
#>  ID: 459c4ded-4687-485e-aa10-f957fabefdd1
#>  Name: motor-vehicle-fatalities-with-alcohol-and-or-drug-involvement
#> 9: Alcohol Driving Prohibitions (xlsx, csv)
#>  ID: 64875edd-962e-4fb7-9c32-dfecd9bada2e
#>  Name: alcohol-driving-prohibitions
#> 10: Vehicle Impoundments (csv, xlsx)
#>  ID: 5ae99a8b-cf0a-4448-8255-77d5df65312a
#>  Name: vehicle-impoundments
#> 
#> Access a single record by calling `bcdc_get_record(ID)` with the ID
#>  from the desired record.

Created on 2023-03-01 with reprex v2.0.2

stephhazlitt commented 1 year ago

This is due to the default setting of download_audience = "Public" in the bcdata::bcdc_search() function. The records above that were not surfaced with bcdata::bcdc_search("Metadata for ICBC") are all download_audience = "Not downloadable" so they are excluded from the query.

Unfortunately, bcdata::bcdc_search("Metadata for ICBC", download_audience = "Not downloadable") does not work.

bcdata::bcdc_search("Metadata for ICBC",`
                    download_audience = "Not downloadable")
#> List of B.C. Data Catalogue Records
#> Number of records: 0
#> Titles:
#> Error in `map2()`:
#> ℹ In index: 1.
#> Caused by error in `x[[.y]]`:
#> ! subscript out of bounds

#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. ├─global `<fn>`(input = base::quote("bad-grub_reprex.R"))
#>  13. │ └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14. │   └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15. │     └─knitr:::process_file(text, output)
#>  16. │       ├─base::withCallingHandlers(...)
#>  17. │       ├─knitr:::process_group(group)
#>  18. │       └─knitr:::process_group.block(group)
#>  19. │         └─knitr:::call_block(x)
#>  20. │           └─knitr:::block_exec(params)
#>  21. │             └─knitr:::eng_r(options)
#>  22. │               ├─knitr:::in_input_dir(...)
#>  23. │               │ └─knitr:::in_dir(input_dir(), expr)
#>  24. │               └─knitr (local) evaluate(...)
#>  25. │                 └─evaluate::evaluate(...)
#>  26. │                   └─evaluate:::evaluate_call(...)
#>  27. │                     ├─evaluate (local) handle(...)
#>  28. │                     │ └─base::try(f, silent = TRUE)
#>  29. │                     │   └─base::tryCatch(...)
#>  30. │                     │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  31. │                     │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  32. │                     │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  33. │                     ├─base::withCallingHandlers(...)
#>  34. │                     ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  35. │                     └─knitr (local) value_fun(ev$value, ev$visible)
#>  36. │                       └─knitr (local) fun(x, options = options)
#>  37. │                         ├─base::withVisible(knit_print(x, ...))
#>  38. │                         ├─knitr::knit_print(x, ...)
#>  39. │                         └─knitr:::knit_print.default(x, ...)
#>  40. │                           └─evaluate (local) normal_print(x)
#>  41. │                             ├─base::print(x)
#>  42. │                             └─bcdata:::print.bcdc_recordlist(x)
#>  43. │                               └─purrr::imap(...) at bcdata/R/utils-classes.R:142:2
#>  44. │                                 └─purrr::map2(.x, vec_index(.x), .f, ...)
#>  45. │                                   └─purrr:::map2_("list", .x, .y, .f, ..., .progress = .progress)
#>  46. │                                     ├─purrr:::with_indexed_errors(...)
#>  47. │                                     │ └─base::withCallingHandlers(...)
#>  48. │                                     ├─purrr:::call_with_cleanup(...)
#>  49. │                                     └─bcdata (local) .f(.x[[i]], .y[[i]], ...)
#>  50. │                                       ├─base::nrow(bcdc_tidy_resources(x[[.y]])) at bcdata/R/utils-classes.R:144:4
#>  51. │                                       └─bcdata::bcdc_tidy_resources(x[[.y]])
#>  52. └─purrr (local) `<fn>`(`<sbscOOBE>`) at bcdata/R/bcdc_search.R:324:2
#>  53.   └─cli::cli_abort(...)
#>  54.     └─rlang::abort(...)

Looking at the API docs, it seems that our resulting {bcdata} call from the above function (https://catalogue.data.gov.bc.ca/api/3/action/package_search?q=Metadata%20for%20ICBC+download_audience:Not%20downloadable) fails because we (now?) need to wrap some values in quotes. For example, this query works in a browser: https://catalogue.data.gov.bc.ca/api/3/action/package_search?q=Metadata%20for%20ICBC+download_audience:"Not%20downloadable".

In addition to fixing the above (the quoting), I wonder if we should set download_audience = NULL as per the other facets. There are now a lot more records in the other categories compared to when this function was first developed.

bcdata::bcdc_search_facets(facet = "download_audience")
#>               facet count                  display_name
#> 1 download_audience  2277                        Public
#> 2 download_audience   226              Not downloadable
#> 3 download_audience    51                   Named users
#> 4 download_audience   174                            NA
#> 5 download_audience     8 Government and Business BCeID
#> 6 download_audience   496                    Government
#>                            name
#> 1                        Public
#> 2              Not downloadable
#> 3                   Named users
#> 4                            NA
#> 5 Government and Business BCeID
#> 6                    Government

Created on 2023-03-01 with reprex v2.0.2

stephhazlitt commented 1 year ago

Depending on what is being used to make the call, changing the : and wrapping the object name in quotes may be needed

from https://bcgov.github.io/data-publication/pages/dps_bcdc_api_w_common_calls.html#common-calls

stephhazlitt commented 1 year ago

Closed with https://github.com/bcgov/bcdata/pull/316.