@@ -44,9 +44,6 @@ new_tibble <- function(x, ..., nrow, class = NULL, subclass = NULL) {
4444 cnd_signal(error_new_tibble_must_be_list())
4545 }
4646
47- # ' The `...` argument allows adding more attributes to the subclass.
48- x <- update_tibble_attrs(x , ... )
49-
5047 # ' An `nrow` argument is required.
5148 if (missing(nrow )) {
5249 cnd <- error_new_tibble_needs_nrow()
@@ -63,22 +60,54 @@ new_tibble <- function(x, ..., nrow, class = NULL, subclass = NULL) {
6360 # ' equal to this value.
6461 # ' (But this is not checked by the constructor).
6562 # ' This takes the place of the "row.names" attribute in a data frame.
66- if (! is_integerish(nrow , 1 )) {
63+ if (is_integerish(nrow , 1 )) {
64+ nrow <- as.integer(nrow )
65+ } else {
6766 cnd_signal(error_new_tibble_needs_nrow())
6867 }
6968
69+ args <- attributes(x )
70+
71+ if (is.null(args )) {
72+ args <- list ()
73+ }
74+
75+ new_attrs <- pairlist2(... )
76+ nms <- names(new_attrs )
77+
78+ for (i in seq_along(nms )) {
79+ nm <- nms [[i ]]
80+
81+ if (nm == " " ) {
82+ next
83+ }
84+
85+ args [[nm ]] <- new_attrs [[i ]]
86+ }
87+
7088 # ' `x` must have names (or be empty),
7189 # ' but the names are not checked for correctness.
7290 if (length(x ) == 0 ) {
7391 # Leaving this because creating a named list of length zero seems difficult
74- names( x ) <- character ()
75- } else if (is.null(names( x ) )) {
92+ args [[ " names" ]] <- character ()
93+ } else if (is.null(args [[ " names" ]] )) {
7694 cnd_signal(error_names_must_be_non_null())
7795 }
7896
79- attr(x , " row.names" ) <- .set_row_names(nrow )
80- class(x ) <- c(class [! class %in% tibble_class ], tibble_class )
81- x
97+ if (is.null(class )) {
98+ class <- tibble_class_no_data_frame
99+ } else {
100+ class <- c(class [! class %in% tibble_class ], tibble_class_no_data_frame )
101+ }
102+
103+ slots <- c(" x" , " n" , " class" )
104+ args [slots ] <- list (x , nrow , class )
105+
106+ # `new_data_frame()` restores compact row names
107+ args [[" row.names" ]] <- NULL
108+
109+ # do.call() is faster than exec() in this case
110+ do.call(new_data_frame , args )
82111}
83112
84113# ' @description
@@ -131,11 +160,8 @@ validate_nrow <- function(names, lengths, nrow) {
131160 }
132161}
133162
134- update_tibble_attrs <- function (x , ... ) {
135- .Call(`tibble_update_attrs` , x , pairlist2(... ))
136- }
137-
138163tibble_class <- c(" tbl_df" , " tbl" , " data.frame" )
164+ tibble_class_no_data_frame <- c(" tbl_df" , " tbl" )
139165
140166# Errors ------------------------------------------------------------------
141167
0 commit comments