@@ -1195,7 +1195,61 @@ time_type_unit_abbr <- function(time_type) {
11951195 maybe_unit_abbr
11961196}
11971197
1198+ # ' Extract singular element of a length-1 unnamed list (validated)
1199+ # '
1200+ # ' Inverse of `list(elt)`.
1201+ # '
1202+ # ' @param x a length-1 list
1203+ # ' @return x[[1L]], if x actually was a length-1 list; error otherwise
1204+ # '
1205+ # ' @keywords internal
11981206unwrap <- function (x ) {
11991207 checkmate :: assert_list(x , len = 1L , names = " unnamed" )
12001208 x [[1L ]]
12011209}
1210+
1211+ # ' Check that a unique key is indeed unique in a tibble (TRUE/str)
1212+ # '
1213+ # ' A `checkmate`-style check function.
1214+ # '
1215+ # ' @param x a tibble, with no particular row or column order (if you have a
1216+ # ' guaranteed row order based on the ukey you can probably do something more
1217+ # ' efficient)
1218+ # ' @param ukey_names character vector; subset of column names of `x` denoting a
1219+ # ' unique key.
1220+ # ' @param end_cli_message optional character vector, a cli message format
1221+ # ' string/vector; information/advice to tack onto any error messages.
1222+ # ' @return `TRUE` if no ukey is duplicated (i.e., `x[ukey_names]` has no
1223+ # ' duplicated rows); string with an error message if there are errors.
1224+ # '
1225+ # ' @keywords internal
1226+ check_ukey_unique <- function (x , ukey_names , end_cli_message = character ()) {
1227+ assert_tibble(x ) # to not have to think about `data.table` perf, xface
1228+ assert_false(is_grouped_df(x )) # to not have to think about `grouped_df` perf, xface
1229+ assert_character(ukey_names )
1230+ assert_subset(ukey_names , names(x ))
1231+ #
1232+ if (nrow(x ) < = 1L ) {
1233+ TRUE
1234+ } else {
1235+ # Fast check, slow error message.
1236+ arranged_ukeys <- arrange(x [ukey_names ], across(all_of(ukey_names )))
1237+ if (! any(vec_equal(arranged_ukeys [- 1L , ], arranged_ukeys [- nrow(arranged_ukeys ), ]))) {
1238+ TRUE
1239+ } else {
1240+ bad_data <- x %> %
1241+ group_by(across(all_of(ukey_names ))) %> %
1242+ filter(dplyr :: n() > 1 ) %> %
1243+ ungroup()
1244+ lines <- c(
1245+ cli :: format_error("
1246+ There cannot be more than one row with the same combination of
1247+ {format_varnames(ukey_names)}. Problematic rows:
1248+ " ),
1249+ capture.output(bad_data ),
1250+ cli :: format_message(end_cli_message )
1251+ )
1252+ paste(collapse = " \n " , lines )
1253+ }
1254+ }
1255+ }
0 commit comments