@@ -29,7 +29,7 @@ spec_to_metacore <- function(path, quiet = FALSE, where_sep_sheet = TRUE){
2929 out <- suppressWarnings(metacore(ds_spec , ds_vars , var_spec , value_spec , derivations , codelist = code_list , quiet = quiet ))
3030 }
3131 } else {
32- stop (" This specification format is not currently supported. You will need to write your own reader" ,
32+ cli_abort (" This specification format is not currently supported. You will need to write your own reader" ,
3333 call. = FALSE )
3434 }
3535 out
@@ -48,14 +48,14 @@ spec_to_metacore <- function(path, quiet = FALSE, where_sep_sheet = TRUE){
4848spec_type <- function (path ){
4949 sheets <- excel_sheets(path )
5050 if (! any(sheets %> % str_detect(" [D|d]omains?|[D|d]atasets?" ))){
51- stop (" File does not contain a Domain/Datasets tab, which is needed. Please either modify the spec document or write a reader (see documentation for more information)" ,
51+ cli_abort (" File does not contain a Domain/Datasets tab, which is needed. Please either modify the spec document or write a reader (see documentation for more information)" ,
5252 call. = FALSE )
5353 } else if (any(sheets %> % str_detect(" ADSL|DM" ))){
5454 type <- " by_ds"
5555 } else if (any(sheets %> % str_detect(" [V|v]ariables?" ))){
5656 type <- " by_type"
5757 } else {
58- stop (" File in an unknown format. Please either modify the spec document or write a reader (see documentation for more information)" ,
58+ cli_abort (" File in an unknown format. Please either modify the spec document or write a reader (see documentation for more information)" ,
5959 call. = FALSE )
6060 }
6161 type
@@ -102,8 +102,10 @@ spec_type_to_ds_spec <- function(doc, cols = c("dataset" = "[N|n]ame|[D|d]ataset
102102 name_check <- names(cols ) %in% c(" dataset" , " structure" , " label" ) %> %
103103 all()
104104 if (! name_check | is.null(names(cols ))){
105- stop(" Supplied column vector must be named using the following names:
106- 'dataset', 'structure', 'label'" )
105+ cli_abort(c(
106+ " Supplied column vector must be named using the following names:" ,
107+ " 'dataset', 'structure', 'label'"
108+ ))
107109 }
108110 if (! is.null(sheet )){
109111 sheet_ls <- str_subset(names(doc ), sheet )
@@ -159,7 +161,7 @@ spec_type_to_ds_vars <- function(doc, cols = c("dataset" = "[D|d]ataset|[D|d]oma
159161
160162 # Testing for names of vectors
161163 if (any(! name_check , ! name_check_extra , is.null(names(cols )))){
162- stop (" Supplied column vector must be named using the following names:
164+ cli_abort (" Supplied column vector must be named using the following names:
163165 'variable', 'dataset', 'order', 'keep', 'core', 'key_seq', 'supp_flag'" )
164166 }
165167 # Subsetting sheets
@@ -226,10 +228,12 @@ spec_type_to_var_spec <- function(doc, cols = c("variable" = "[N|n]ame|[V|v]aria
226228 " type" , " dataset" , " common" , " format" ) %> %
227229 all()
228230 if (! name_check | is.null(names(cols ))){
229- stop(" Supplied column vector must be named using the following names:
230- 'variable', 'length', 'label', 'type', 'dataset', 'common', 'format'
231- If common is not avaliable it can be excluded and will be automatically filled in.
232- Additionally, dataset is only used to clarify if information differs by domain" )
231+ cli_abort(paste(
232+ " Supplied column vector must be named using the following names:" ,
233+ " 'variable', 'length', 'label', 'type', 'dataset', 'common', 'format'" ,
234+ " If common is not avaliable it can be excluded and will be automatically filled in." ,
235+ " Additionally, dataset is only used to clarify if information differs by domain."
236+ ))
233237 }
234238
235239 # Check if sheet is specified
@@ -245,12 +249,12 @@ spec_type_to_var_spec <- function(doc, cols = c("variable" = "[N|n]ame|[V|v]aria
245249 summarise(n = n(), .groups = " drop" ) %> %
246250 filter(n > 1 )
247251 if (nrow(dups ) > 0 ){
248- dups %> %
249- pull( variable ) % > %
250- paste( collapse = " \n " ) % > %
251- paste0( " The following variables are repeated with different metadata for different datasets: \n " ,
252- . , " \n Please add 'dataset' = [Name of dataset column] to your named cols vector, to correct for this" ) % > %
253- stop( . , call. = FALSE )
252+ x <- dups %> % pull( variable )
253+ cli_abort(c(
254+ col_red( " The following variables are repeated with different metadata for different datasets: " ),
255+ " i " = ansi_collapse( x ) ,
256+ " i " = " Please add 'dataset' = [Name of dataset column] to your named cols vector to correct this. "
257+ ) , call. = FALSE )
254258 }
255259 } else {
256260 if (! " common" %in% names(cols )){
@@ -334,12 +338,12 @@ spec_type_to_value_spec <- function(doc, cols = c("dataset" = "[D|d]ataset|[D|d]
334338 all()
335339
336340 if (! name_check | is.null(names(cols ))){
337- stop( " Supplied column vector must be named using the following names:
338- 'dataset', 'variable', 'origin', 'code_id', 'type', 'where', 'sig_dig', 'derivation_id' ,
339- ' predecessor'
340- If derivation_id is not avaliable it can be excluded and dataset.variable will be used.
341-
342- If the where information is on a seperate sheet, put the column with cross ref as where. " )
341+ cli_abort(c(
342+ " Supplied column vector must be named using the following names: " ,
343+ " i " = " 'dataset', 'variable', 'origin', 'code_id', 'type', 'where', 'sig_dig', 'derivation_id',' predecessor'" ,
344+ " i " = paste( " If derivation_id is not avaliable it can be excluded and dataset.variable will be used." ,
345+ " If the where information is on a seperate sheet, put the column with cross ref as where. " )
346+ ), call = FALSE )
343347 }
344348
345349 # Select a subset of sheets if specified
@@ -401,7 +405,7 @@ spec_type_to_value_spec <- function(doc, cols = c("dataset" = "[D|d]ataset|[D|d]
401405 left_join(where_df , by = c(" where" = " id" )) %> %
402406 select(- where , where = where_new )
403407 } else if (where_sep_sheet ) {
404- warning (" Not able to add where infromation from seperate sheet cause a where column is needed to cross-reference the information" ,
408+ cli_warn (" Not able to add where infromation from seperate sheet cause a where column is needed to cross-reference the information" ,
405409 call. = FALSE )
406410 }
407411
@@ -467,12 +471,12 @@ spec_type_to_codelist <- function(doc, codelist_cols = c("code_id" = "ID",
467471 " version" = " [V|v]ersion" ),
468472 sheets = NULL , simplify = FALSE ){
469473 if (is.null(codelist_cols )){
470- stop (" Codelist column names must be provided" , call. = FALSE )
474+ cli_abort (" Codelist column names must be provided" , call. = FALSE )
471475 } else {
472476 name_check <- names(codelist_cols ) %in% c(" code_id" , " name" , " code" , " decode" ) %> %
473477 all()
474478 if (! name_check | is.null(names(codelist_cols ))){
475- stop (" Supplied column vector for codelist_cols must be named using the following names:
479+ cli_abort (" Supplied column vector for codelist_cols must be named using the following names:
476480 'code_id', 'name', 'code', 'decode'" ,
477481 call. = FALSE
478482 )
@@ -483,7 +487,7 @@ spec_type_to_codelist <- function(doc, codelist_cols = c("code_id" = "ID",
483487 name_check <- names(permitted_val_cols ) %in% c(" code_id" , " name" , " code" ) %> %
484488 all()
485489 if (! name_check ){
486- stop (" Supplied column vector for permitted_val_cols must be named using the following names:
490+ cli_abort (" Supplied column vector for permitted_val_cols must be named using the following names:
487491 'code_id', 'name', 'code'" ,
488492 call. = FALSE )
489493 }
@@ -492,10 +496,10 @@ spec_type_to_codelist <- function(doc, codelist_cols = c("code_id" = "ID",
492496 name_check <- names(dict_cols ) %in% c(" code_id" , " name" , " dictionary" , " version" ) %> %
493497 all()
494498 if (! name_check ){
495- stop( " Supplied column vector for `dict_cols` must be named using the following names:
496- 'code_id', 'name', 'dictionary', 'version' ,
497- If a dictionary sheet isn't avaliable set `dict_cols` to NULL " ,
498- call. = FALSE )
499+ cli_abort(paste0(
500+ " Supplied column vector for `dict_cols` must be named using the following names: " ,
501+ " 'code_id', 'name', 'dictionary', 'version'. If a dictionary sheet isn't avaliable" ,
502+ " set `dict_cols` to NULL " ), call. = FALSE )
499503 }
500504 }
501505
@@ -574,14 +578,15 @@ spec_type_to_derivations <- function(doc, cols = c("derivation_id" = "ID",
574578 name_check <- names(cols ) %in% c(" derivation_id" , " derivation" ) %> %
575579 all()
576580 if (! name_check | is.null(names(cols ))){
577- stop(" Supplied column vector must be named using the following names:
578- 'derivation_id', 'derivation'" )
581+ cli_abort(c(
582+ " Supplied column vector must be named using the following names:" ,
583+ " 'derivation_id', 'derivation'" ))
579584 }
580585
581586 name_check <- names(var_cols ) %in% c(' dataset' , ' variable' , ' origin' , ' predecessor' , ' comment' ) %> %
582587 all()
583588 if (! name_check | is.null(names(var_cols ))){
584- stop (" Supplied variable column vector must be named using the following names:
589+ cli_abort (" Supplied variable column vector must be named using the following names:
585590 'dataset', 'variable', 'origin', 'predecessor', 'comment'" )
586591 }
587592 # Get the predecessor
@@ -683,7 +688,7 @@ create_tbl <- function(doc, cols){
683688 }) %> %
684689 paste0(collapse = " \n " ) %> %
685690 paste0(" Unable to identify a sheet with all columns.\n " , . ) %> %
686- stop (call. = FALSE )
691+ (call. = FALSE )
687692
688693 } else if (length(matches ) == 1 ){
689694 # Check names and write a better warning message if names don't work
@@ -702,26 +707,25 @@ create_tbl <- function(doc, cols){
702707 cols [names(nm_test )] <- cols [names(nm_test )] %> %
703708 paste0(" ^" , . , " $" )
704709 } else {
705- str_c(names(nm_test ), " matches " ,nm_test , " columns" ) %> %
706- str_c(collapse = " \n " ) %> %
707- paste0(" Unable to rename the following columns in " , names(matches [1 ]), " :\n " , . ,
708- " \n Please check your regular expression " ) %> %
709- stop(call. = FALSE )
710+ x <- str_c(names(nm_test ), " matches " ,nm_test , " columns" )
711+ cli_abort(c(
712+ " Unable to rename the following columns in {names(matches[1])}:" ,
713+ " i" = ansi_collapse(x ),
714+ " i" = " Please check your regular expression"
715+ ), call. = FALSE )
710716 }
711717 }
712718
713719 # This needs to be done columnwise to allow for duplicate selection of the same column
714720 select_rename_w_dups(matches [[1 ]], cols )
715721
716722 } else {
717- sheets_mats <- matches %> %
718- names()
719- paste(" Column names are not specific enough to identify a single sheet. The following" ,
720- length(sheets_mats ),
721- " match the criteria set:" , paste(sheets_mats , collapse = " , " )) %> %
722- warning(. , call. = FALSE )
723- matches %> %
724- map(~ select_rename_w_dups(. , cols ))
723+ sheets_mats <- matches %> % names()
724+ cli_warn(c(paste(
725+ " Column names are not specific enough to identify a single sheet." ),
726+ " The following {length(sheets_mats)} match the criteria set:" ),
727+ ansi_collapse(sheets_mats ), call. = FALSE )
728+ matches %> % map(~ select_rename_w_dups(. , cols ))
725729 }
726730}
727731
@@ -741,7 +745,7 @@ yn_to_tf <- function(x){
741745 } else if (is.logical(x )){
742746 x
743747 } else {
744- warning (" Keep column needs to be True or False, please correct before converting to a Metacore object" ,
748+ cli_warn (" Keep column needs to be True or False, please correct before converting to a Metacore object" ,
745749 call. = FALSE )
746750 x
747751 }
0 commit comments