Skip to content

Commit

Permalink
Move functions
Browse files Browse the repository at this point in the history
  • Loading branch information
neitmant committed Aug 22, 2022
1 parent 6202784 commit 1ef497c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 75 deletions.
76 changes: 76 additions & 0 deletions R/dev_utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,79 @@ valid_time_units <- function() {
contains_vars <- function(arg) {
inherits(arg, "quosures") && all(map_lgl(arg, quo_is_symbol) | names(arg) != "")
}

#' Turn a List of Quosures into a Character Vector
#'
#' @param quosures A `list` of `quosures` created using [`vars()`]
#'
#' @return A character vector
#'
#' @author Thomas Neitmann
#'
#' @export
#'
#' @keywords remove_utility
#'
#' @examples
#' vars2chr(vars(USUBJID, AVAL))
vars2chr <- function(quosures) {
rlang::set_names(
map_chr(quosures, ~ as_string(quo_get_expr(.x))),
names(quosures)
)
}

#' Negate List of Variables
#'
#' The function adds a minus sign as prefix to each variable.
#'
#' This is useful if a list of variables should be removed from a dataset,
#' e.g., `select(!!!negate_vars(by_vars))` removes all by variables.
#'
#' @param vars List of variables created by `vars()`
#'
#' @return A list of `quosures`
#'
#' @author Stefan Bundfuss
#'
#' @export
#'
#' @keywords remove_utility
#'
#' @examples
#' negate_vars(vars(USUBJID, STUDYID))
negate_vars <- function(vars = NULL) {
assert_vars(vars, optional = TRUE)
if (is.null(vars)) {
NULL
} else {
lapply(vars, function(var) expr(-!!quo_get_expr(var)))
}
}

#' Optional Filter
#'
#' Filters the input dataset if the provided expression is not `NULL`
#'
#' @param dataset Input dataset
#' @param filter A filter condition. Must be a quosure.
#'
#' @return A `data.frame` containing all rows in `dataset` matching `filter` or
#' just `dataset` if `filter` is `NULL`
#'
#' @author Thomas Neitmann
#'
#' @export
#'
#' @keywords remove_utility
#'
filter_if <- function(dataset, filter) {
assert_data_frame(dataset)
assert_filter_cond(filter, optional = TRUE)

if (quo_is_null(filter)) {
dataset
} else {
filter(dataset, !!filter)
}
}
75 changes: 0 additions & 75 deletions R/remove_these.R

This file was deleted.

0 comments on commit 1ef497c

Please sign in to comment.