Skip to content

Commit

Permalink
⚡ retains factor levels (even if unseen) when pasting an existing object
Browse files Browse the repository at this point in the history
  • Loading branch information
jonocarroll committed Jul 23, 2018
1 parent f762241 commit 4ba130d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/dfdt_paste.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ dfdt_construct <- function(input_table, oc = console_context(), class = NULL) {
return(NULL)
}
col_types <- lapply(input_table, base::class) # prevent clobbering by local class variable
factor_levels <- lapply(input_table, levels)
#Store types as characters so the char lengths can be computed
input_table <- as.data.frame(lapply(input_table, as.character), stringsAsFactors = FALSE)
#Store types as characters so the char lengths can be computed
Expand All @@ -119,7 +120,8 @@ dfdt_construct <- function(input_table, oc = console_context(), class = NULL) {
lapply(which(col_types == "factor"), function(x) paste(pad_to(names(cols[x]), charw), "=",
paste0("as.factor(c(",
paste0( unlist(lapply(cols[[x]], render_type, col_types[[x]])), collapse=", "),
"))"
"), levels = c(",
paste0(unlist(lapply(factor_levels[[x]], render_type, col_types[[x]])), collapse = ", "),"))"
)
)
)
Expand Down

0 comments on commit 4ba130d

Please sign in to comment.