Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain factor levels (even if unseen) #75

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
6 changes: 4 additions & 2 deletions 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 @@ -117,9 +118,10 @@ 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 = ", "),"))"
)
)
)
Expand Down