Skip to content

Commit

Permalink
fix data_write for new haven
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 20, 2023
1 parent 7cb3d61 commit 4978f3b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions R/data_write.r
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,30 @@ data_write <- function(data,
insight::format_alert("Preparing data file: converting variable types.")
}
x[] <- lapply(x, function(i) {
# make sure we have labelled class for labelled data
value_labels <- attr(i, "labels", exact = TRUE)
variable_label <- attr(i, "label", exact = TRUE)
# factor requires special preparation to save levels as labels
# haven:::vec_cast_named requires "x" and "labels" to be of same type
if (is.factor(i)) {
# factor requires special preparation to save levels as labels
haven::labelled(
x = as.numeric(i),
labels = stats::setNames(seq_along(levels(i)), levels(i)),
label = variable_label
)
} else if (!is.null(value_labels) || !is.null(variable_label)) {
# make sure we have labelled class for labelled data
haven::labelled(x = i, labels = value_labels, label = variable_label)
# character requires special preparation to save levels as labels
# haven:::vec_cast_named requires "x" and "labels" to be of same type
if (is.character(i)) {
haven::labelled(
x = i,
labels = stats::setNames(as.character(value_labels), names(value_labels)),
label = variable_label
)
} else {
# this should work for the remaining types...
haven::labelled(x = i, labels = value_labels, label = variable_label)
}
} else {
# non labelled data can be saved "as is"
i
Expand Down

0 comments on commit 4978f3b

Please sign in to comment.