diff --git a/DESCRIPTION b/DESCRIPTION index d92c5330..e50b2d48 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -47,7 +47,6 @@ Imports: purrr (>= 0.3), readr (>= 2.1), readxl (>= 1.4.0), - jsonlite (>= 1.8.4), rlang (>= 1.0), sf (>= 1.0), tidyselect (>= 1.1.0), diff --git a/NEWS.md b/NEWS.md index 7f9d889c..f464094e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ # bcdata (development version) * Add `jsonlite::read_json()` as a file read method, so users can now download & read `json` resources in B.C. Data Catalogue records +* Change the `download_audience` default from `Public` to `NULL` in `bcdc_search()` (#315) +* Fix bug where some/all facet values in `bcdc_search()` need to be quoted to generate a valid API query (#315) # bcdata 0.4.0 diff --git a/R/bcdc_search.R b/R/bcdc_search.R index ee224603..76600872 100644 --- a/R/bcdc_search.R +++ b/R/bcdc_search.R @@ -22,6 +22,10 @@ #' @examples #' \donttest{ #' try( +#' bcdc_search_facets("download_audience") +#' ) +#' +#' try( #' bcdc_search_facets("res_format") #' ) #' } @@ -129,7 +133,7 @@ bcdc_list <- function() { #' @param ... search terms #' @param license_id the type of license (see `bcdc_search_facets("license_id")`). #' @param download_audience download audience -#' (see `bcdc_search_facets("download_audience")`). Default `"Public"` +#' (see `bcdc_search_facets("download_audience")`). Default `NULL` (all audiences). #' @param res_format format of resource (see `bcdc_search_facets("res_format")`) #' @param sector sector of government from which the data comes #' (see `bcdc_search_facets("sector")`) @@ -151,8 +155,8 @@ bcdc_list <- function() { #' ) #' } bcdc_search <- function(..., license_id = NULL, - download_audience = "Public", - res_format=NULL, + download_audience = NULL, + res_format = NULL, sector = NULL, organization = NULL, n = 100) { @@ -168,17 +172,27 @@ bcdc_search <- function(..., license_id = NULL, organization = organization )) - lapply(names(facets), function(x) { - facet_vals <- bcdc_search_facets(x) - if (!facets[x] %in% facet_vals$name) { - stop(facets[x], " is not a valid value for ", x, - call. = FALSE) - } - }) - - query <- paste0( - terms, ifelse(nzchar(terms), "+", ""), - paste(names(facets), facets, sep = ":", collapse = "+")) + # build query by collating the terms and any user supplied facets + # if there are no supplied facets (e.g., is_empty(facets) returns TRUE) just use terms) + query <- if (is_empty(facets)) { + paste0(terms) + } else { + #check that the facet values are valid + lapply(names(facets), function(x) { + facet_vals <- bcdc_search_facets(x) + if (!facets[x] %in% facet_vals$name) { + stop(facets[x], " is not a valid value for ", x, + call. = FALSE) + } + }) + + paste0(terms, "+", paste( + names(facets), + paste0("\"", facets, "\""), + sep = ":", + collapse = "+" + )) + } query <- gsub("\\s+", "%20", query) diff --git a/R/utils.R b/R/utils.R index bd4adb0e..2197d9bb 100644 --- a/R/utils.R +++ b/R/utils.R @@ -448,3 +448,7 @@ names_to_lazy_tbl <- function(x) { frame <- as.data.frame(stats::setNames(rep(list(logical()), length(x)), x)) dbplyr::tbl_lazy(frame) } + +is_empty <- function(x) { + length(x) == 0 +} diff --git a/man/bcdc_search.Rd b/man/bcdc_search.Rd index 56bb9e05..ff3f8af7 100644 --- a/man/bcdc_search.Rd +++ b/man/bcdc_search.Rd @@ -7,7 +7,7 @@ bcdc_search( ..., license_id = NULL, - download_audience = "Public", + download_audience = NULL, res_format = NULL, sector = NULL, organization = NULL, @@ -20,7 +20,7 @@ bcdc_search( \item{license_id}{the type of license (see \code{bcdc_search_facets("license_id")}).} \item{download_audience}{download audience -(see \code{bcdc_search_facets("download_audience")}). Default \code{"Public"}} +(see \code{bcdc_search_facets("download_audience")}). Default \code{NULL} (all audiences).} \item{res_format}{format of resource (see \code{bcdc_search_facets("res_format")})} diff --git a/man/bcdc_search_facets.Rd b/man/bcdc_search_facets.Rd index 739b5144..12b3e56d 100644 --- a/man/bcdc_search_facets.Rd +++ b/man/bcdc_search_facets.Rd @@ -22,6 +22,10 @@ Get the valid values for a facet (that you can use in \code{\link[=bcdc_search]{ } \examples{ \donttest{ +try( + bcdc_search_facets("download_audience") +) + try( bcdc_search_facets("res_format") )