Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recordlist print #288

Merged
merged 3 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
17 changes: 13 additions & 4 deletions R/utils-classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,16 @@ 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)

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"))
Expand Down Expand Up @@ -532,3 +535,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)
}
13 changes: 12 additions & 1 deletion tests/testthat/test-search.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})