From 83bad8583fef2082b795074865930591f8d0e55f Mon Sep 17 00:00:00 2001 From: Andy Teucher Date: Thu, 28 Oct 2021 15:29:43 -0700 Subject: [PATCH 1/3] [ method for recordlist --- NAMESPACE | 1 + R/utils-classes.R | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/NAMESPACE b/NAMESPACE index 93d9c5a9..2191ee65 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method("[",bcdc_recordlist) S3method(as_tibble,bcdc_promise) S3method(bcdc_describe_feature,bcdc_record) S3method(bcdc_describe_feature,character) diff --git a/R/utils-classes.R b/R/utils-classes.R index 9ac18522..00c1e14f 100644 --- a/R/utils-classes.R +++ b/R/utils-classes.R @@ -136,7 +136,7 @@ print.bcdc_recordlist <- function(x, ...) { cat_line_wrap("Titles:") x <- purrr::set_names(x, NULL) - purrr::imap(x[1:n_print], ~ { + purrr::imap(unclass(x)[1:n_print], ~ { if (!nrow(bcdc_tidy_resources(x[[.y]]))) { cat_line_wrap(.y, ": ",purrr::pluck(.x, "title")) @@ -532,3 +532,9 @@ cat_line_wrap <- function(..., indent = 0, exdent = 1, col = NULL, background_co txt <- strwrap(paste0(..., collapse = ""), indent = indent, exdent = exdent) cat_line(txt, col = col, background_col = background_col, file = file) } + +#' @export +"[.bcdc_recordlist" <- function(x, i, j, ..., drop = FALSE) { + out <- unclass(x)[i] + as.bcdc_recordlist(out) +} From b0cab9d391208dc2d371315bf1b36435266c25b8 Mon Sep 17 00:00:00 2001 From: Andy Teucher Date: Thu, 28 Oct 2021 15:52:12 -0700 Subject: [PATCH 2/3] Increase max print for bcdc_recordlist to 50, add message --- R/utils-classes.R | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/R/utils-classes.R b/R/utils-classes.R index 00c1e14f..4835bd64 100644 --- a/R/utils-classes.R +++ b/R/utils-classes.R @@ -130,9 +130,12 @@ record_print_helper <- function(r, n, print_avail = FALSE) { print.bcdc_recordlist <- function(x, ...) { cat_line_wrap("List of B.C. Data Catalogue Records") len <- length(x) - n_print <- min(10, len) - cat_line_wrap("Number of records: ", len) - if (n_print < len) cat_line(" (Showing the top 10)") + n_print <- min(50, len) + cat_line_wrap(cli::col_blue("Number of records: ", len)) + if (n_print < len) { + cat_line_wrap(cli::col_blue("Showing the top 50 results. You can assign the output of bcdc_search, to an object and subset with `[` to see other results in the set.")) + cat_line("") + } cat_line_wrap("Titles:") x <- purrr::set_names(x, NULL) From da31edd57cad4782e1b55ee5457ccb050ddcd1fc Mon Sep 17 00:00:00 2001 From: Andy Teucher Date: Mon, 1 Nov 2021 14:46:06 -0700 Subject: [PATCH 3/3] tests and NEWS item for bcdc_search #288 --- NEWS.md | 2 ++ tests/testthat/test-search.R | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 209d713c..5dc82ad9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # bcdata (development version) +* Results from `bcdc_search()` (objects of class `"bcdc_recordlist"`) now print 50 records by default, instead of 10. In addition, there is a new `[` method for `"bcdc_recordlist"` objects, allowing you to subset these lists and still have a nice printout (#288). + # bcdata 0.3.0 * The [BC Data Catalogue and its API have been updated](https://www2.gov.bc.ca/gov/content?id=8A553CABCCDD434D8614D1CA92B03400), requiring changes to the `bcdata` package, most of which are internal only (#283). These should be mostly invisible to the user, except for the removal of the `type` search facet in `bcdc_search()` and `bcdc_search_facets()`. If you use an API key (authorized catalogue editors only), you will need to login to the new catalogue and get your updated key and set the value your `BCDC_KEY` environment variable to the new key. diff --git a/tests/testthat/test-search.R b/tests/testthat/test-search.R index 90e4c3a9..65e6a301 100644 --- a/tests/testthat/test-search.R +++ b/tests/testthat/test-search.R @@ -19,8 +19,19 @@ test_that('bcdc_search works', { output_path <- tempfile() suppressWarnings( verify_output(output_path, { - bcdc_search("Container") + bcdc_search("Container", n = 5) }) ) expect_false(any(grepl("Error", readLines(output_path)))) }) + +test_that('bcdc_search subsetting works', { + skip_on_cran() + skip_if_net_down() + rec_list <- bcdc_search("Container", n = 5) + expect_is(rec_list, "bcdc_recordlist") + expect_length(rec_list, 5) + shorter <- rec_list[3:5] + expect_is(shorter, "bcdc_recordlist") + expect_length(shorter, 3) +})