Skip to content

Commit

Permalink
Merge pull request #147 from pharmaverse/enhancement/add-labels
Browse files Browse the repository at this point in the history
Enhancement: add labels to ADNCA
  • Loading branch information
js3110 authored Jan 2, 2025
2 parents fb38102 + 5ca2661 commit 411c49c
Show file tree
Hide file tree
Showing 15 changed files with 561 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ inst/guidelines
inst/templates
inst/shiny/data
!inst/shiny/data/DummyRO_ADNCA.csv
!inst/shiny/data/adnca_labels.csv
.Rprofile
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(anonymize_pk_data)
export(apply_filters)
export(apply_labels)
export(as_factor_preserve_label)
export(calculate_summary_stats)
export(create_conc)
Expand Down
45 changes: 45 additions & 0 deletions R/label_operators.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
#' Apply Labels to a dataset
#'
#' This function adds "label" attributes to all columns in a dataset
#'
#' @param data The dataset to which labels will be applied.
#' @param labels_file A data frame with two columns: Variable and Label,
#' for the dataset you are applying it .
#'
#' @return The same dataset with label attributes applied to all columns.
#' If a column is not present in the labels list, it will be assigned the name of the col.
#'
#' @examples
#' \dontrun{
#' # Example usage:
#' data <- data.frame(USUBJID = c(1, 2, 3), AVAL = c(4, 5, 6))
#' labels <- data.frame(
#' Variable = c("USUBJID", "AVAL"),
#' Label = c("Unique Subject Identifier", "Analysis Value")
#' )
#' data <- apply_labels(data, labels)
#' print(attr(data$A, "label"))
#' }
#'
#' @export
apply_labels <- function(data, labels_file) {

# Create the label_ADNCA named vector from labels_app
label_adnca <- setNames(labels_file$Label, labels_file$Variable)

for (col in colnames(data)) {
if (col %in% names(label_adnca)) {
attr(data[[col]], "label") <- label_adnca[[col]]
} else {
attr(data[[col]], "label") <- col
}

# Check if the column is a factor and keep the levels order
if (is.factor(data[[col]])) {
data[[col]] <- as_factor_preserve_label(data[[col]])
}
}

data
}

#' Convert to Factor While Preserving Label
#'
#' This function converts a vector to a factor while preserving its "label" attribute.
Expand Down
6 changes: 4 additions & 2 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ADNCA
ADPC
ADPP
AFRLT
AUClast
Expand All @@ -25,8 +26,8 @@ analyte
analytes
anonymized
anonymizes
cheatsheet
cmax
codebase
csv
customizable
ggplot
Expand All @@ -36,12 +37,13 @@ pknca
plotly
pptest
pre
reactable
reupload
ruleset
rulesets
sas
summarization
timepoint
tooltip
visualizable
xpt
codebase
Loading

0 comments on commit 411c49c

Please sign in to comment.