Skip to content

Commit b0feb5b

Browse files
authored
Merge pull request #138 from atorus-research/135-implement-verbose-argument-to-control-messages
eprecates the quiet argument across the package in favour of verbose that controls the "loudness" of messages in the console. message: permits both messages and warnings warn: permits warnings only collapse: collapses all warnings into a single message. (Similar to previous behaviour of quiet = TRUE in 0.2.1). silent: evaluates silently quiet = TRUE has the same effect as verbose = "silent" (to be removed in a future version). Errors are always printed regardless of the value of verbose.
2 parents 142fde4 + eb67aca commit b0feb5b

25 files changed

Lines changed: 979 additions & 222 deletions

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Depends:
2727
Imports:
2828
cli,
2929
dplyr,
30+
lifecycle,
3031
magrittr,
3132
purrr,
3233
R6,

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ importFrom(dplyr,rowwise)
6969
importFrom(dplyr,select)
7070
importFrom(dplyr,summarise)
7171
importFrom(dplyr,ungroup)
72+
importFrom(lifecycle,deprecate_soft)
73+
importFrom(lifecycle,deprecated)
7274
importFrom(magrittr,"%>%")
7375
importFrom(purrr,compact)
7476
importFrom(purrr,discard)

R/DatasetMeta.R

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ DatasetMeta <- R6::R6Class("DatasetMeta",
1414
.label = NA,
1515
.num_vars = NA,
1616
.key_vars = NA,
17-
.greet = function(quiet) {
18-
cli_par()
19-
cli_alert_success("{private$.name} dataset successfully selected")
20-
if (quiet) {
21-
cli_inform(c(
22-
"i" = col_red("Dataset metadata specification subsetted with suppressed warnings")
23-
))
24-
}
25-
cli_end()
17+
.greet = function(quiet, verbose) {
18+
with_verbosity(
19+
{
20+
cli_alert_success("{private$.name} dataset successfully selected")
21+
},
22+
quiet,
23+
verbose
24+
)
2625
}
2726
),
2827
public = list(
29-
initialize = function(metacore, quiet = FALSE) {
28+
initialize = function(metacore, quiet = deprecated(), verbose = "message") {
3029
super$initialize(
3130
ds_spec = metacore$ds_spec,
3231
ds_vars = metacore$ds_vars,
@@ -43,7 +42,7 @@ DatasetMeta <- R6::R6Class("DatasetMeta",
4342
filter(!is.na(key_seq)) |>
4443
pull(variable)
4544

46-
private$.greet(quiet)
45+
private$.greet(quiet, verbose)
4746
},
4847
print = function() {
4948
tables <- ls(envir = self)

R/metacore-package.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' @keywords internal
2+
#' @family internal
3+
4+
#' @import R6
5+
#' @importFrom tidyr replace_na nest unnest
6+
#' @importFrom tidyselect everything matches
7+
#' @importFrom dplyr right_join n_distinct row_number case_when n if_else arrange
8+
#' rowwise anti_join inner_join bind_rows distinct mutate full_join left_join
9+
#' group_by summarise filter pull select group_split ungroup
10+
#' @importFrom tibble tibble tribble
11+
#' @importFrom stringr str_replace str_count str_remove_all str_remove str_detect
12+
#' str_subset str_length str_replace_na str_replace_all str_extract str_c str_trim
13+
#' str_split str_starts regex
14+
#' @importFrom rlang !! as_string expr set_names sym prim_name inherits_only
15+
#' @importFrom purrr keep discard reduce compact map map_chr map_lgl map_dfr map_int
16+
#' map2_lgl map2_chr pmap_chr safely
17+
#' @importFrom stats var na.omit
18+
#' @importFrom readxl excel_sheets read_excel
19+
#' @importFrom tibble tibble as_tibble
20+
#' @importFrom readxl excel_sheets read_excel
21+
#' @importFrom cli ansi_collapse cli_abort cli_inform cli_alert_success cli_alert_info
22+
#' cli_warn cli_bullets cli_div cli_end cli_par cli_rule cli_text col_red qty
23+
#' @importFrom xml2 read_xml xml_find_all xml_find_first xml_attr xml_ns_strip
24+
#' xml_text
25+
#' @importFrom lifecycle deprecated deprecate_soft
26+
#'
27+
"_PACKAGE"

R/metacore.R

Lines changed: 129 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,30 @@
1111
#' have the same derivation
1212
#' @param code_list contains the code/decode information
1313
#' @param supp contains the idvar and qeval information for supplemental variables
14-
#' @param quiet Option to quietly load in, this will suppress warnings, but not
15-
#' errors. Expects either `TRUE` or `FALSE`. Default behaviour is `FALSE`.
14+
#' @param quiet `r lifecycle::badge("superseded")` Option to quietly load in, this
15+
#' will suppress warnings, but not errors. Expects either `TRUE` or `FALSE`.
16+
#' Default behaviour is `FALSE`.
17+
#' @param verbose A character string specifying the desired verbosity level.
18+
#' Must be one of:
19+
#' \describe{
20+
#' \item{"message"}{(default) Messages and warnings are handled normally.}
21+
#' \item{"warn"}{Messages are suppressed, but warnings are allowed.}
22+
#' \item{"collapse"}{Warnings are collapsed into a single message indicating the
23+
#' number of suppressed warnings.}
24+
#' \item{"silent"}{Both messages and warnings are suppressed.}
25+
#' }
1626
#'
1727
#' @family Metacore
1828
#' @noRd
1929
#'
2030
#' @importFrom stringr str_to_lower
21-
MetaCore_initialize <- function(ds_spec, ds_vars, var_spec, value_spec, derivations, codelist, supp, quiet = FALSE) {
31+
MetaCore_initialize <- function(ds_spec, ds_vars, var_spec, value_spec, derivations, codelist, supp, quiet = FALSE, verbose = "message") {
32+
deprecate_soft(
33+
when = "0.3.0",
34+
what = "MetaCore_initialize(quiet)",
35+
with = "MetaCore_initialize(verbose)"
36+
)
37+
2238
private$.ds_spec <- ds_spec %>%
2339
add_labs(
2440
dataset = "Dataset Name",
@@ -95,12 +111,8 @@ MetaCore_initialize <- function(ds_spec, ds_vars, var_spec, value_spec, derivati
95111
private$.ds_names <- ds_spec %>% pull(dataset)
96112

97113
private$.ds_labels <- ds_spec %>% pull(label)
98-
if (quiet) {
99-
suppressWarnings(self$validate())
100-
} else {
101-
self$validate()
102-
}
103114

115+
self$validate()
104116

105117
if (inherits_only(self, c("Metacore", "R6"))) {
106118
private$.greet(quiet)
@@ -141,7 +153,7 @@ MetaCore_validate <- function() {
141153
nrow(private$.derivations) == 0 &
142154
nrow(private$.codelist) == 0 &
143155
nrow(private$.supp) == 0) {
144-
cli_warn("Other checks were not preformed, because all datasets are empty",
156+
cli_warn("Other checks were not performed, because all datasets are empty",
145157
call. = FALSE
146158
)
147159
} else {
@@ -164,7 +176,7 @@ MetaCore_validate <- function() {
164176
}
165177
}
166178
} else {
167-
cli_warn("Other checks were not preformed, because column names were incorrect",
179+
cli_warn("Other checks were not performed, because column names were incorrect",
168180
call. = FALSE
169181
)
170182
}
@@ -310,11 +322,8 @@ MetaCore <- R6::R6Class("Metacore",
310322
.ds_names = list(),
311323
.ds_labels = list(),
312324
.greet = function(quiet = FALSE) {
313-
cli_par()
314325
cli_alert_success("Metadata successfully imported")
315-
if (quiet) cli_inform(c("i" = col_red("Dataset metadata imported with suppressed warnings")))
316326
cli_inform(c("i" = "To use the {.obj Metacore} object with {.pkg metatools} package, first subset a dataset using {.fn metacore::select_dataset}"))
317-
cli_end()
318327
}
319328
),
320329
active = list(
@@ -338,22 +347,44 @@ MetaCore <- R6::R6Class("Metacore",
338347
#' @param derivations contains derivation, it allows for different variables to have the same derivation
339348
#' @param codelist contains the code/decode information
340349
#' @param supp contains the idvar and qeval information for supplemental variables
341-
#' @param quiet Option to quietly load in, this will suppress warnings, but not
342-
#' errors. Expects either `TRUE` or `FALSE`. Default behaviour is `FALSE`.
350+
#' @param quiet `r lifecycle::badge("superseded")` Option to quietly load in, this
351+
#' will suppress warnings, but not errors. Expects either `TRUE` or `FALSE`.
352+
#' Default behaviour is `FALSE`.
353+
#' @param verbose A character string specifying the desired verbosity level.
354+
#' Must be one of:
355+
#' \describe{
356+
#' \item{"message"}{ (default) Messages and warnings are handled normally.}
357+
#' \item{"warn"}{Messages are suppressed, but warnings are allowed.}
358+
#' \item{"collapse"}{Warnings are collapsed into a single message indicating the
359+
#' number of suppressed warnings.}
360+
#' \item{"silent"}{Both messages and warnings are suppressed.}
361+
#' }
343362
#'
344363
#' @family Metacore
345364
#'
346365
#' @export
347-
#'
348-
metacore <- function(ds_spec = tibble(dataset = character(), structure = character(), label = character()),
366+
metacore <- function(ds_spec = tibble(
367+
dataset = character(),
368+
structure = character(),
369+
label = character()
370+
),
349371
ds_vars = tibble(
350-
dataset = character(), variable = character(), mandatory = logical(),
351-
key_seq = integer(), order = integer(), core = character(),
372+
dataset = character(),
373+
variable = character(),
374+
keep = NULL, # Deprecated in 0.3.0. To be removed in a future version
375+
mandatory = logical(),
376+
key_seq = integer(),
377+
order = integer(),
378+
core = character(),
352379
supp_flag = logical()
353380
),
354381
var_spec = tibble(
355-
variable = character(), label = character(), length = integer(),
356-
type = character(), common = character(), format = character()
382+
variable = character(),
383+
label = character(),
384+
length = integer(),
385+
type = character(),
386+
common = character(),
387+
format = character()
357388
),
358389
value_spec = tibble(
359390
dataset = character(),
@@ -365,12 +396,46 @@ metacore <- function(ds_spec = tibble(dataset = character(), structure = charact
365396
origin = character(),
366397
derivation_id = integer()
367398
),
368-
derivations = tibble(derivation_id = integer(), derivation = character()),
369-
codelist = tibble(code_id = character(), name = character(), type = character(), codes = list()),
370-
supp = tibble(dataset = character(), variable = character(), idvar = character(), qeval = character()),
371-
quiet = FALSE) {
372-
test <- quiet_if_true(
399+
derivations = tibble(
400+
derivation_id = integer(),
401+
derivation = character()
402+
),
403+
codelist = tibble(
404+
code_id = character(),
405+
name = character(),
406+
type = character(),
407+
codes = list()
408+
),
409+
supp = tibble(
410+
dataset = character(),
411+
variable = character(),
412+
idvar = character(),
413+
qeval = character()
414+
),
415+
quiet = deprecated(),
416+
verbose = "message") {
417+
# Check if user has supplied `quiet` instead of `verbose`
418+
if (lifecycle::is_present(quiet)) {
419+
deprecate_soft(when = "0.3.0", what = "metacore(quiet)", with = "metacore(verbose)")
420+
} else {
421+
quiet <- FALSE
422+
} # Else deal with deprecated argument for compatability
423+
424+
with_verbosity(
373425
{
426+
# Signal deprecation warning for ds_vars$keep column. This cannot be handled by
427+
# regular `lifecycle::deprecate_*` functionality as it is a column name of an
428+
# argument that has been changed, not the argument itself.
429+
if ("keep" %in% names(ds_vars)) {
430+
cli_warn(c("The column `ds_vars$keep` in the `ds_vars` table was deprecated
431+
as of 0.3.0 in favour of `ds_vars$mandatory and will be removed in a future release.
432+
The input for the supplied column `keep` has been mapped to the new column `mandatory`."))
433+
434+
ds_vars <- ds_vars %>%
435+
mutate(mandatory = keep) %>%
436+
select(-keep)
437+
}
438+
374439
is_empty_df <- as.list(environment()) %>%
375440
keep(is.null)
376441

@@ -410,13 +475,13 @@ metacore <- function(ds_spec = tibble(dataset = character(), structure = charact
410475
derivations = derivations,
411476
codelist = codelist,
412477
supp = supp,
413-
quiet = quiet
478+
quiet = quiet,
479+
verbose = verbose
414480
)
415481
},
416-
quiet = quiet
482+
quiet,
483+
verbose
417484
)
418-
419-
if (quiet) invisible(test) else test
420485
}
421486

422487

@@ -425,19 +490,36 @@ metacore <- function(ds_spec = tibble(dataset = character(), structure = charact
425490
#' @param .data the metacore object of dataframes
426491
#' @param dataset the specific dataset to subset by
427492
#' @param simplify return a single dataframe
428-
#' @param quiet Option to quietly load in, this will suppress warnings, but not
429-
#' errors. Expects either `TRUE` or `FALSE`. Default behaviour is `FALSE`.
493+
#' @param quiet `r lifecycle::badge("superseded")` Option to quietly load in, this
494+
#' will suppress warnings, but not errors. Expects either `TRUE` or `FALSE`.
495+
#' Default behaviour is `FALSE`.
496+
#' @param verbose A character string specifying the desired verbosity level.
497+
#' Must be one of:
498+
#' \describe{
499+
#' \item{"message"}{(default) Messages and warnings are handled normally.}
500+
#' \item{"warn"}{Messages are suppressed, but warnings are allowed.}
501+
#' \item{"collapse"}{Warnings are collapsed into a single message indicating the
502+
#' number of suppressed warnings.}
503+
#' \item{"silent"}{Both messages and warnings are suppressed.}
504+
#' }
430505
#'
431506
#' @return a filtered subset of the metacore object
432507
#' @export
433-
select_dataset <- function(.data, dataset, simplify = FALSE, quiet = FALSE) {
434-
cl <- .data$clone()
435-
cl$metacore_filter(dataset)
436-
437-
if (simplify) {
438-
test <- quiet_if_true(
439-
{
440-
list(
508+
select_dataset <- function(.data, dataset, simplify = FALSE, quiet = deprecated(), verbose = "message") {
509+
# Check if user has supplied `quiet` instead of `verbose`
510+
if (lifecycle::is_present(quiet)) {
511+
deprecate_soft(when = "0.3.0", what = "select_dataset(quiet)", with = "select_dataset(verbose)")
512+
} else {
513+
quiet <- FALSE
514+
} # Else deal with deprecated argument for compatability
515+
516+
with_verbosity(
517+
{
518+
cl <- .data$clone()
519+
cl$metacore_filter(dataset)
520+
521+
if (simplify) {
522+
test <- list(
441523
cl$ds_vars,
442524
cl$var_spec,
443525
cl$value_spec,
@@ -446,17 +528,13 @@ select_dataset <- function(.data, dataset, simplify = FALSE, quiet = FALSE) {
446528
cl$supp
447529
) %>%
448530
reduce(left_join)
449-
},
450-
quiet = quiet
451-
)
452-
} else {
453-
test <- quiet_if_true(
454-
DatasetMeta$new(metacore = cl, quiet = quiet),
455-
quiet = quiet
456-
)
457-
}
458-
459-
if (quiet) invisible(test) else test
531+
} else {
532+
test <- DatasetMeta$new(metacore = cl, quiet = quiet)
533+
}
534+
},
535+
quiet,
536+
verbose
537+
)
460538
}
461539

462540

0 commit comments

Comments
 (0)