-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #154 --------- Signed-off-by: Marcin <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Dawid Kałędkowski <[email protected]> Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
- Loading branch information
1 parent
af6453e
commit 0a46f56
Showing
16 changed files
with
282 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ Depends: | |
Imports: | ||
checkmate, | ||
digest, | ||
formatters (>= 0.3.1), | ||
lifecycle, | ||
logger (>= 0.2.0), | ||
methods, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#' Get Label Attributes of Variables in a \code{data.frame} | ||
#' | ||
#' Variable labels can be stored as a \code{label} attribute for each variable. | ||
#' This functions returns a named character vector with the variable labels | ||
#' (empty sting if not specified) | ||
#' | ||
#' @param x a \code{data.frame} object | ||
#' @param fill boolean in case the \code{label} attribute does not exist if | ||
#' \code{TRUE} the variable names is returned, otherwise \code{NA} | ||
#' | ||
#' @source This function was taken 1-1 from | ||
#' \href{https://cran.r-project.org/web/packages/formatters/index.html}{formatters} package, to reduce the complexity of | ||
#' the dependency tree. | ||
#' | ||
#' @seealso [col_relabel()] [`col_labels<-`] | ||
#' | ||
#' @return a named character vector with the variable labels, the names | ||
#' correspond to the variable names | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' x <- iris | ||
#' col_labels(x) | ||
#' col_labels(x) <- paste("label for", names(iris)) | ||
#' col_labels(x) | ||
col_labels <- function(x, fill = FALSE) { | ||
stopifnot(is.data.frame(x)) | ||
if (NCOL(x) == 0) { | ||
return(character()) | ||
} | ||
|
||
y <- Map(function(col, colname) { | ||
label <- attr(col, "label") | ||
|
||
if (is.null(label)) { | ||
if (fill) { | ||
colname | ||
} else { | ||
NA_character_ | ||
} | ||
} else { | ||
if (!is.character(label) && !(length(label) == 1)) { | ||
stop("label for variable ", colname, "is not a character string") | ||
} | ||
as.vector(label) | ||
} | ||
}, x, colnames(x)) | ||
|
||
labels <- unlist(y, recursive = FALSE, use.names = TRUE) | ||
|
||
if (!is.character(labels)) { | ||
stop("label extraction failed") | ||
} | ||
|
||
labels | ||
} | ||
|
||
#' Set Label Attributes of All Variables in a \code{data.frame} | ||
#' | ||
#' Variable labels can be stored as a \code{label} attribute for each variable. | ||
#' This functions sets all non-missing (non-NA) variable labels in a \code{data.frame} | ||
#' | ||
#' @inheritParams col_labels | ||
#' @param value new variable labels, \code{NA} removes the variable label | ||
#' | ||
#' @source This function was taken 1-1 from | ||
#' \href{https://cran.r-project.org/web/packages/formatters/index.html}{formatters} package, to reduce the complexity of | ||
#' the dependency tree. | ||
#' | ||
#' @seealso [col_labels()] [col_relabel()] | ||
#' | ||
#' @return modifies the variable labels of \code{x} | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' x <- iris | ||
#' col_labels(x) | ||
#' col_labels(x) <- paste("label for", names(iris)) | ||
#' col_labels(x) | ||
#' | ||
#' if (interactive()) { | ||
#' View(x) # in RStudio data viewer labels are displayed | ||
#' } | ||
`col_labels<-` <- function(x, value) { | ||
stopifnot( | ||
is.data.frame(x), | ||
is.character(value), | ||
ncol(x) == length(value) | ||
) | ||
|
||
theseq <- if (!is.null(names(value))) names(value) else seq_along(x) | ||
# across columns of x | ||
for (j in theseq) { | ||
attr(x[[j]], "label") <- if (!is.na(value[j])) { | ||
value[j] | ||
} else { | ||
NULL | ||
} | ||
} | ||
|
||
x | ||
} | ||
|
||
#' Copy and Change Variable Labels of a \code{data.frame} | ||
#' | ||
#' Relabel a subset of the variables | ||
#' | ||
#' @inheritParams col_labels<- | ||
#' @param ... name-value pairs, where name corresponds to a variable name in | ||
#' \code{x} and the value to the new variable label | ||
#' | ||
#' @return a copy of \code{x} with changed labels according to \code{...} | ||
#' | ||
#' @source This function was taken 1-1 from | ||
#' \href{https://cran.r-project.org/web/packages/formatters/index.html}{formatters} package, to reduce the complexity of | ||
#' the dependency tree. | ||
#' | ||
#' @seealso [col_labels()] [`col_labels<-`] | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' x <- col_relabel(iris, Sepal.Length = "Sepal Length of iris flower") | ||
#' col_labels(x) | ||
#' | ||
col_relabel <- function(x, ...) { | ||
stopifnot(is.data.frame(x)) | ||
if (missing(...)) { | ||
return(x) | ||
} | ||
dots <- list(...) | ||
varnames <- names(dots) | ||
stopifnot(!is.null(varnames)) | ||
|
||
map_varnames <- match(varnames, colnames(x)) | ||
|
||
if (any(is.na(map_varnames))) { | ||
stop("variables: ", paste(varnames[is.na(map_varnames)], collapse = ", "), " not found") | ||
} | ||
|
||
if (any(vapply(dots, Negate(is.character), logical(1)))) { | ||
stop("all variable labels must be of type character") | ||
} | ||
|
||
for (i in seq_along(map_varnames)) { | ||
attr(x[[map_varnames[[i]]]], "label") <- dots[[i]] | ||
} | ||
|
||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ iteratively | |
pre | ||
repo | ||
reproducibility | ||
formatters |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.