Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
37 changes: 33 additions & 4 deletions R/Utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,19 @@ TADA_CheckColumns <- function(.data, expected_cols) {
#' converted to an average of the two numbers. Result values
#' containing any other text or non-numeric characters become NA in
#' the newly created "TADA.COLUMN NAME" and labeled accordingly in "TADA.COLUMN
#' NAME DataTypes.Flag".
#' NAME DataTypes.Flag". When clean = TRUE, rows that cannot be converted to
#' numeric are removed. When clean = FALSE, no rows are removed. Default is
#' clean = FALSE. When flaggedonly = TRUE, data frame is filtered to show only
#' rows with non-numeric result values. Default is flaggedonly = FALSE.
#'
#'
#' @param .data A TADA profile object
#' @param col A character column to be converted to numeric
#'
#' @param clean Boolean argument; removes non-numeric result values from the
#' data frame when clean = TRUE. Default is clean = FALSE.
#' @param flaggedonly Boolean argument; filters dataframe to show only
#' non-numeric result values when flaggedonly = TRUE. Default is flaggedonly
#' = FALSE.
#' @param percent.ave Boolean argument; default is percent.ave = TRUE. When
#' clean = TRUE, any percent range values will be averaged. When
#' percent.ave = FALSE, percent range values are not averaged, but are flagged.
Expand Down Expand Up @@ -229,11 +236,17 @@ TADA_CheckColumns <- function(.data, expected_cols) {
#' )
#' unique(HandleSpecialChars_DetLimMeasureValue$
#' TADA.DetectionQuantitationLimitMeasure.MeasureValueDataTypes.Flag)
TADA_ConvertSpecialChars <- function(.data, col, percent.ave = TRUE) {
TADA_ConvertSpecialChars <- function(.data, col, percent.ave = TRUE,
clean = FALSE, flaggedonly = FALSE) {
if (!col %in% names(.data)) {
stop("Suspect column name specified for input dataset.")
}

# check that clean and flaggedonly are not both TRUE
if (clean == TRUE & flaggedonly == TRUE) {
stop("Function not executed because clean and flaggedonly cannot both be TRUE")
}

# Define new column names
numcol <- paste0("TADA.", col)
flagcol <- paste0("TADA.", col, "DataTypes.Flag")
Expand Down Expand Up @@ -353,7 +366,23 @@ TADA_ConvertSpecialChars <- function(.data, col, percent.ave = TRUE) {

clean.data <- TADA_OrderCols(clean.data)

return(clean.data)
if (flaggedonly == FALSE) {
if (clean == TRUE) {
clean.data <- clean.data %>%
dplyr::filter(!(!!rlang::sym(flagcol)) %in% c("NA - Not Available", "Text"))

return(clean.data)
}

if (clean == FALSE) {
return(clean.data)
}
}

if (flaggedonly == TRUE) {
clean.data <- clean.data %>%
dplyr::filter(!!rlang::sym(flagcol) %in% c("NA - Not Available", "Text"))
}
}

#' Substitute Preferred Characteristic Name for Deprecated Names
Expand Down
70 changes: 17 additions & 53 deletions R/autoFilter.R
Original file line number Diff line number Diff line change
Expand Up @@ -412,21 +412,14 @@ TADA_AnalysisDataFilter <- function(.data,

#' AutoFilter
#'
#' This function removes rows where the result value is not numeric to
#' prepare a dataframe for quantitative analyses. Ideally, this function should
#' be run after other data cleaning, QA/QC, and harmonization steps are
#' completed using other TADA package functions, or manually. Specifically,
#' this function removes rows with "Text" and "NA - Not Available"
#' in the TADA.ResultMeasureValueDataTypes.Flag column, or NA in the
#' TADA.ResultMeasureValue column. In addition, this function removes results
#' with QA/QC ActivityTypeCode's. This function also removes any columns not
#' required for TADA workflow where all values are equal to NA. It provides a
#' warning message identifying any TADA required columns containing only NA
#' values.
#' This function also removes any columns not required for the TADA workflow
#' where all values are equal to NA. It provides a warning message identifying
#' any TADA required columns containing only NA values.
#'
#' @param .data TADA dataframe
#'
#' @return .data with rows non-quantitative and QA/QC results removed
#' @return .data with any non-required columns containing only NA values
#' removed.
#'
#' @export
#'
Expand All @@ -436,41 +429,18 @@ TADA_AutoFilter <- function(.data) {
# check .data is data.frame
TADA_CheckType(.data, "data.frame", "Input object")

TADA_CheckColumns(.data, c(
"ActivityTypeCode", "MeasureQualifierCode",
"TADA.ResultMeasureValueDataTypes.Flag",
"TADA.ResultMeasureValue", "TADA.ActivityMediaName",
"ActivityTypeCode"
))

# keep track of starting and ending number of rows
start <- dim(.data)[1]

# run TADA_FindQCActivities if needed
if (("TADA.ActivityType.Flag" %in% colnames(.data)) == TRUE) {
.data <- .data
}

if (("TADA.ActivityType.Flag" %in% colnames(.data)) == FALSE) {
.data <- TADA_FindQCActivities(.data, clean = FALSE, flaggedonly = FALSE)
}

# remove text, NAs and QC results
.data <- dplyr::filter(.data, TADA.ResultMeasureValueDataTypes.Flag != "Text" &
TADA.ResultMeasureValueDataTypes.Flag != "NA - Not Available" &
TADA.ActivityType.Flag == "Non_QC" & # filter out QA/QC ActivityTypeCode's
!is.na(TADA.ResultMeasureValue))

# remove columns that are not required for TADA workflow
print("TADA_Autofilter: removing columns not required for TADA workflow if they contain only NAs.")
print("TADA_AutoFilter: removing columns not required for TADA workflow if they contain only NAs.")

# create list of columns containing all NA values.
na.cols <- .data %>%
purrr::keep(~ all(is.na(.x))) %>%
names()

# create list of columns to be removed by comparing columns containing all NA values to required columns.
# any required columns with all NA values will be excluded from the list of columns to remove.
# create list of columns to be removed by comparing columns containing all NA
# values to required columns.
# any required columns with all NA values will be excluded from the list of
# columns to remove.
remove.cols <- setdiff(na.cols, require.cols)

# remove not required columns containing all NA values from dataframe.
Expand All @@ -489,33 +459,27 @@ TADA_AutoFilter <- function(.data) {
# create character string for list of removed columns
remove.paste <- stringi::stri_replace_last_fixed(paste(as.character(remove.cols), collapse = ", ", sep = ""), ", ", " and ")

# print list of columns removed from dataframe
# print list of columns removed from data frame
if (length(remove.cols) > 0) {
print(paste0("The following column(s) were removed as they contained only NAs: ", remove.paste, "."))
print(paste0("The following column(s) were removed as they contained only NAs ",
"and are not required for the TADA workflow: ", remove.paste, "."))
} else {
print("All columns contained some non-NA values and were retained in the dataframe.")
}

# remove columns that are not required for TADA workflow
print("TADA_Autofilter: checking required columns for non-NA values.")
print("TADA_AutoFilter: checking required columns for non-NA values.")

# if some required columns contain only NA values print a warning message.
if (length(req.check) > 0) {
print(paste0("TADA Required column(s) ", req.paste, " contain only NA values. This may impact other TADA functions."))
print(paste0("TADA_AutoFilter: TADA Required column(s) ", req.paste,
" contain only NA values. This may impact other TADA functions."))
} else {
print("All TADA Required columns contain some non-NA values.")
print("TADA_AutoFilter: All TADA Required columns contain some non-NA values.")
}

# remove intermediate objects
rm(req.paste, remove.cols, remove.paste, req.check)

end <- dim(.data)[1]

# print number of results removed
if (!start == end) {
net <- start - end
print(paste0("Function removed ", net, " results. These results are either text or NA and cannot be plotted or represent quality control activities (not routine samples or measurements)."))
}

return(.data)
}
17 changes: 5 additions & 12 deletions man/TADA_AutoFilter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions man/TADA_ConvertSpecialChars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading