From 4ba130d66afce0ee867b20d02e3dc0162e38f999 Mon Sep 17 00:00:00 2001 From: Jonathan Carroll Date: Mon, 23 Jul 2018 22:09:08 +0930 Subject: [PATCH] :zap: retains factor levels (even if unseen) when pasting an existing object closes #39 --- R/dfdt_paste.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/dfdt_paste.R b/R/dfdt_paste.R index d4860b6..2444cab 100644 --- a/R/dfdt_paste.R +++ b/R/dfdt_paste.R @@ -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 @@ -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 = ", "),"))" ) ) )