@@ -44,34 +44,46 @@ fortify.grouped_df <- function(model, data, ...) {
4444# There are a lot of ways that dim(), colnames(), or as.data.frame() could
4545# do non-sensical things (they are not even guaranteed to work!) hence the
4646# paranoid mode.
47- .prevalidate_data_frame_like_object <- function (data ) {
47+ check_data_frame_like <- function (data ) {
4848 orig_dims <- dim(data )
49- if (! vec_is(orig_dims , integer(), size = 2 ))
50- cli :: cli_abort(paste0(" {.code dim(data)} must return " ,
51- " an {.cls integer} of length 2." ))
52- if (anyNA(orig_dims ) || any(orig_dims < 0 )) # extra-paranoid mode
53- cli :: cli_abort(paste0(" {.code dim(data)} can't have {.code NA}s " ,
54- " or negative values." ))
49+ if (! vec_is(orig_dims , integer(), size = 2 )) {
50+ cli :: cli_abort(
51+ " {.code dim(data)} must return an {.cls integer} of length 2."
52+ )
53+ }
54+ if (anyNA(orig_dims ) || any(orig_dims < 0 )) { # extra-paranoid mode
55+ cli :: cli_abort(
56+ " {.code dim(data)} can't have {.code NA}s or negative values."
57+ )
58+ }
5559 orig_colnames <- colnames(data )
56- if (! vec_is(orig_colnames , character (), size = ncol(data )))
57- cli :: cli_abort(paste0(" {.code colnames(data)} must return a " ,
58- " {.cls character} of length {.code ncol(data)}." ))
60+ if (! vec_is(orig_colnames , character (), size = ncol(data ))) {
61+ cli :: cli_abort(
62+ " {.code colnames(data)} must return a {.cls character} of length {.code ncol(data)}."
63+ )
64+ }
65+ invisible ()
5966}
60- .postvalidate_data_frame_like_object <- function (df , data ) {
67+ check_data_frame_conversion <- function (new , old ) {
6168 msg0 <- " {.code as.data.frame(data)} must "
62- if (! is.data.frame(df ))
69+ if (! is.data.frame(new )) {
6370 cli :: cli_abort(paste0(msg0 , " return a {.cls data.frame}." ))
64- if (! identical(dim(df ), dim(data )))
71+ }
72+ if (! identical(dim(new ), dim(old ))) {
6573 cli :: cli_abort(paste0(msg0 , " preserve dimensions." ))
66- if (! identical(colnames(df ), colnames(data )))
74+ }
75+ if (! identical(colnames(new ), colnames(old ))) {
6776 cli :: cli_abort(paste0(msg0 , " preserve column names." ))
77+ }
78+ invisible ()
6879}
6980validate_as_data_frame <- function (data ) {
70- if (is.data.frame(data ))
81+ if (is.data.frame(data )) {
7182 return (data )
72- .prevalidate_data_frame_like_object(data )
83+ }
84+ check_data_frame_like(data )
7385 df <- as.data.frame(data )
74- .postvalidate_data_frame_like_object (df , data )
86+ check_data_frame_conversion (df , data )
7587 df
7688}
7789
0 commit comments