@@ -86,38 +86,69 @@ compact_character <- function(x) {
8686 return (FALSE )
8787 }
8888 if (is.list(object )) {
89- return (.is_null_list(object ))
89+ # we check for deeper list objects here, because `as.character()` can be
90+ # very slow for large lists; we only need to check for first level when
91+ # compacting lists.
92+ if (.large_list_depth(object ) > 1 ) {
93+ return (FALSE )
94+ } else {
95+ return (.safe(
96+ any(unlist(as.character(object ), use.names = FALSE ) == " NULL" , na.rm = TRUE ),
97+ FALSE
98+ ))
99+ }
90100 }
91101 .safe(any(as.character(object ) == " NULL" , na.rm = TRUE ), FALSE )
92102}
93103
94104
95- .is_null_list <- function (x ) {
96- if (! is.list(x ) || is.data.frame(x )) {
97- return (FALSE )
98- }
99- if (! length(x )) {
100- return (TRUE )
105+ .large_list_depth <- function (x , depth = 0 ) {
106+ if (! is.list(x ) || is.data.frame(x ) || depth > 1 ) {
107+ return (depth )
108+ } else {
109+ return (max(vapply(x , .large_list_depth , FUN.VALUE = numeric (1 ), depth = depth + 1 )))
101110 }
111+ }
102112
103- for (i in x ) {
104- if (is.null(i )) {
105- next
106- }
107- if (is.character(i ) || is.factor(i )) {
108- if (any(i == " NULL" , na.rm = TRUE )) {
109- next
110- }
111- return (FALSE )
112- }
113- if (is.list(i )) {
114- if (.is_null_list(i )) {
115- next
116- }
117- return (FALSE )
118- }
119- return (FALSE )
120- }
113+ # .is_null_string <- function(object) {
114+ # if (is.character(object) || is.factor(object)) {
115+ # return(any(object == "NULL", na.rm = TRUE))
116+ # }
117+ # if (is.atomic(object)) {
118+ # return(FALSE)
119+ # }
120+ # if (is.list(object)) {
121+ # return(.is_null_list(object))
122+ # }
123+ # .safe(any(as.character(object) == "NULL", na.rm = TRUE), FALSE)
124+ # }
121125
122- TRUE
123- }
126+ # .is_null_list <- function(x) {
127+ # if (!is.list(x) || is.data.frame(x)) {
128+ # return(FALSE)
129+ # }
130+ # if (!length(x)) {
131+ # return(TRUE)
132+ # }
133+
134+ # for (i in x) {
135+ # if (is.null(i)) {
136+ # next
137+ # }
138+ # if (is.character(i) || is.factor(i)) {
139+ # if (any(i == "NULL", na.rm = TRUE)) {
140+ # next
141+ # }
142+ # return(FALSE)
143+ # }
144+ # if (is.list(i)) {
145+ # if (.is_null_list(i)) {
146+ # next
147+ # }
148+ # return(FALSE)
149+ # }
150+ # return(FALSE)
151+ # }
152+
153+ # TRUE
154+ # }
0 commit comments