From 4ba130d66afce0ee867b20d02e3dc0162e38f999 Mon Sep 17 00:00:00 2001 From: Jonathan Carroll Date: Mon, 23 Jul 2018 22:09:08 +0930 Subject: [PATCH 1/3] :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 = ", "),"))" ) ) ) From c0cd69a8ccdd9586021eab0ffcd409445f50b5e8 Mon Sep 17 00:00:00 2001 From: Jonathan Carroll Date: Mon, 23 Jul 2018 22:14:22 +0930 Subject: [PATCH 2/3] Added NEWS item --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 4c24699..33a1323 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # datapasta 3.0.1 * Exported `_format` functions * Adds `dt_paste` function for pasting as `data.table` (@jonocarroll, #72, closes #70) +* Factor levels are now preserved (even if unseen) when pasting (@jonocarroll, #75, closes #39) # datapasta 3.0.0 'Colander Helmet' From d6ef2e55ad044181f4e6e594d888a4c74e832555 Mon Sep 17 00:00:00 2001 From: Jonathan Carroll Date: Mon, 23 Jul 2018 22:20:20 +0930 Subject: [PATCH 3/3] bugfix - factor over as.factor to allow levels --- R/dfdt_paste.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dfdt_paste.R b/R/dfdt_paste.R index 2444cab..549c0f3 100644 --- a/R/dfdt_paste.R +++ b/R/dfdt_paste.R @@ -118,7 +118,7 @@ dfdt_construct <- function(input_table, oc = console_context(), class = NULL) { if(any(col_types == "factor")){ list_of_factor_cols <- lapply(which(col_types == "factor"), function(x) paste(pad_to(names(cols[x]), charw), "=", - paste0("as.factor(c(", + paste0("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 = ", "),"))"