Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacement of vars() with quos() #165

Draft
wants to merge 5 commits into
base: devel
Choose a base branch
from
Draft
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
20 changes: 10 additions & 10 deletions R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ assert_inherits_class <- function(x, should_have) {
#' Assert that variables not passed as strings are present in target dataset
#'
#' @param quo_list A variable that can be a string, variable, or combination of
#' those using dplyr::vars
#' those using rlang::quos
#' @param vnames Variable names of the target dataset to check against
#' @param envir Environment containing the dataset \code{target} from which names will be checked against
#' @param allow_character Whether or not character strings are allows in an entry
Expand All @@ -114,10 +114,10 @@ assert_quo_var_present <- function(quo_list, vnames=NULL, envir=NULL, allow_char

if (allow_character) {
allow <- c('name', 'character')
allow_str <- "`. Submit either a string, a variable name, or multiple variable names using `dplyr::vars`."
allow_str <- "`. Submit either a string, a variable name, or multiple variable names using `rlang::quos`."
} else {
allow <- "name"
allow_str <- "`. Submit either a variable name or multiple variable names using `dplyr::vars`."
allow_str <- "`. Submit either a variable name or multiple variable names using `rlang::quos`."
}

# Global definition warning
Expand Down Expand Up @@ -155,7 +155,7 @@ assert_quo_var_present <- function(quo_list, vnames=NULL, envir=NULL, allow_char
#' Assert that an argument is passed using vars as appropriate
#'
#' @param quo_list A variable that can be a string, variable, or combination of
#' those using dplyr::vars
#' those using rlang::quos
#' @param allow_character Whether or not character strings are allows in an entry
#'
#' @return Unpacked quosure.
Expand All @@ -169,20 +169,20 @@ unpack_vars <- function(quo_list, allow_character=TRUE) {
param <- enexpr(quo_list)

if (allow_character) {
allow_str <- "`. Submit either a string, a variable name, or multiple variable names using `dplyr::vars`."
allow_str <- "`. Submit either a string, a variable name, or multiple variable names using `rlang::quos`."
} else {
allow_str <- "`. Submit either a variable name or multiple variable names using `dplyr::vars`."
allow_str <- "`. Submit either a variable name or multiple variable names using `rlang::quos`."
}

# Unpack the `quo_list` group to ensure that the type is `list_of<quosures>`
# It had to be a 1 item list, so check if that element is a `call`
# The only valid use of a `call` is to provide multiple variables using `vars`
# The only valid use of a `call` is to provide multiple variables using `quos`
c <- quo_get_expr(quo_list[[1]])
if (is.call(c)) {
# If it's a call, we need to pull it out a level
quo_list <- tryCatch({
# If it's in here, the call has to be to dplyr::vars
if (call_name(c) != "vars") abort("Multiple variables should be using dplyr::vars")
# If it's in here, the call has to be to rlang::quos
if (call_name(c) != "quos") abort("Multiple variables should be using rlang::quos")

# Evaluate the quosure sort_vars getting the expression
eval(c, envir=caller_env())
Expand All @@ -192,7 +192,7 @@ unpack_vars <- function(quo_list, allow_character=TRUE) {
abort(message = paste0("Invalid input to `", param, allow_str))
})
} else {
if (is.null(c)) return(vars())
if (is.null(c)) return(quos())
}
quo_list
}
Expand Down
6 changes: 5 additions & 1 deletion R/count_bindings.R
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ set_outer_sort_position <- function(e, outer_sort_position) {
set_denom_where <- function(e, denom_where) {
denom_where <- enquo(denom_where)

if (!(inherits(e, 'tplyr_layer') | inherits(e, 'tplyr_table'))) {
stop('Object type should be either "tplyr_layer" or "tplyr_table"', call.=FALSE)
}

assert_that(is_logical_or_call(denom_where),
msg = "The `where` parameter must contain subsetting logic (enter without quotes)")

Expand All @@ -587,7 +591,7 @@ set_denom_where <- function(e, denom_where) {
#' @export
#' @noRd
set_denoms_by.count_layer <- function(e, ...) {
dots <- vars(...)
dots <- quos(...)
dots_chr <- map_chr(dots, as_name)

# Pull these variables to make sure the denoms used make sense
Expand Down
2 changes: 1 addition & 1 deletion R/denom.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ this_denom <- function(.data, header_n, treat_var) {
#' @examples
#' library(dplyr)
#'
#' t <- tplyr_table(mtcars, gear, cols = vars(cyl, am))
#' t <- tplyr_table(mtcars, gear, cols = quos(cyl, am))
#'
#' get_header_n_value(t, 3, 6, 0)
#' # Returns the number of cars that have 3 gears, 6 cylinders, and auto transmission
Expand Down
6 changes: 3 additions & 3 deletions R/desc.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ process_formatting.desc_layer <- function(x, ...) {
if (stats_as_columns) {
form_sums[[i]] <- trans_sums[[i]] %>%
pivot_wider(id_cols=c(!!treat_var, match_exact(by)), # Keep row_label and the by variables
names_from = match_exact(vars(row_label, !!!cols)), # Pull the names from treatment and cols argument
names_from = match_exact(quos(row_label, !!!cols)), # Pull the names from treatment and cols argument
names_prefix = paste0('var', i, "_"), # Prefix with the name of the target variable
values_from = display_string # Use the created display_string variable for values
)

} else {
form_sums[[i]] <- trans_sums[[i]] %>%
pivot_wider(id_cols=c('row_label', match_exact(by)), # Keep row_label and the by variables
names_from = match_exact(vars(!!treat_var, !!!cols)), # Pull the names from treatment and cols argument
names_from = match_exact(quos(!!treat_var, !!!cols)), # Pull the names from treatment and cols argument
names_prefix = paste0('var', i, "_"), # Prefix with the name of the target variable
values_from = display_string # Use the created display_string variable for values
)
Expand All @@ -173,7 +173,7 @@ process_formatting.desc_layer <- function(x, ...) {
# formatted_data <- formatted_data %>%
# rowwise() %>%
# # Replace NA values with the proper empty strings
# mutate_at(vars(starts_with('var')), ~ replace_na(.x, format_strings[[row_label]]$empty))
# mutate_at(quos(starts_with('var')), ~ replace_na(.x, format_strings[[row_label]]$empty))


# Clean up
Expand Down
4 changes: 2 additions & 2 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' either "counts" for categorical counts, "desc" for descriptive statistics,
#' or "shift" for shift table counts
#' @param by A string, a variable name, or a list of variable names supplied
#' using \code{dplyr::vars}
#' using \code{rlang::quos}
#' @param target_var Symbol. Required, The variable name on which the summary is
#' to be performed. Must be a variable within the target dataset. Enter
#' unquoted - i.e. target_var = AEBODSYS.
Expand Down Expand Up @@ -63,7 +63,7 @@
#' @examples
#' tab <- tplyr_table(iris, Sepal.Width)
#'
#' l <- group_count(tab, by=vars('Label Text', Species),
#' l <- group_count(tab, by=quos('Label Text', Species),
#' target_var=Species, where= Sepal.Width < 5.5,
#' cols = Species)
#'
Expand Down
14 changes: 7 additions & 7 deletions R/layer_bindings.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ set_target_var <- function(layer, target_var) {
#' iris$Species2 <- iris$Species
#' lay <- tplyr_table(iris, Species) %>%
#' group_count(Species) %>%
#' set_by(vars(Species2, Sepal.Width))
#' set_by(quos(Species2, Sepal.Width))
get_by <- function(layer) {
env_get(layer, "by")
}

#' @param by A string, a variable name, or a list of variable names supplied
#' using \code{dplyr::vars}.
#' using \code{rlang::quos}.
#'
#' @export
#' @rdname by
Expand Down Expand Up @@ -111,15 +111,15 @@ set_where.tplyr_layer <- function(obj, where) {
#' library(magrittr)
#' lay <- tplyr_table(mtcars, gear) %>%
#' add_layer(
#' group_desc(mpg, by=vars(carb, am)) %>%
#' group_desc(mpg, by=quos(carb, am)) %>%
#' set_precision_by(carb)
#' )
get_precision_by <- function(layer) {
env_get(layer, "precision_by")
}

#' @param precision_by A string, a variable name, or a list of variable names supplied
#' using \code{dplyr::vars}.
#' using \code{rlang::quos}.
#'
#' @export
#' @rdname precision_by
Expand Down Expand Up @@ -155,15 +155,15 @@ set_precision_by <- function(layer, precision_by) {
#' library(magrittr)
#' lay <- tplyr_table(mtcars, gear) %>%
#' add_layer(
#' group_desc(vars(mpg, disp), by=vars(carb, am)) %>%
#' group_desc(quos(mpg, disp), by=quos(carb, am)) %>%
#' set_precision_on(disp)
#' )
get_precision_on <- function(layer) {
env_get(layer, "precision_on")
}

#' @param precision_on A string, a variable name, or a list of variable names
#' supplied using \code{dplyr::vars}.
#' supplied using \code{rlang::quos}.
#'
#' @export
#' @rdname precision_on
Expand Down Expand Up @@ -229,7 +229,7 @@ set_precision_data <- function(layer, prec, default = c("error", "auto")) {
precision_by_syms <- map(precision_by, sym)

# Insert the by variables in the layer and let set_precision_by validate
set_precision_by(layer, vars(!!!precision_by_syms))
set_precision_by(layer, quos(!!!precision_by_syms))

# Checks
# max_int and max_dec are both on precision dataset
Expand Down
12 changes: 6 additions & 6 deletions R/layering.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ add_layers <- function(parent, ...) {
#' Count layers are also capable of producing counts of nested relationships.
#' For example, if you want to produce counts of an overall outside group, and
#' then the subgroup counts within that group, you can specify the target
#' variable as vars(OutsideVariable, InsideVariable). This allows you to do
#' variable as quos(OutsideVariable, InsideVariable). This allows you to do
#' tables like Adverse Events where you want to see the Preferred Terms within
#' Body Systems, all in one layer. Further control over denominators is
#' available using the function \code{\link{set_denoms_by}} and distinct
Expand All @@ -153,7 +153,7 @@ add_layers <- function(parent, ...) {
#' shift layer displays an endpoint's 'shift' throughout the duration of the
#' study. It is an abstraction over the count layer, however we have provided
#' an interface that is more efficient and intuitive. Targets are passed as
#' named symbols using \code{dplyr::vars}. Generally the baseline is passed
#' named symbols using \code{rlang::quos}. Generally the baseline is passed
#' with the name 'row' and the shift is passed with the name 'column'. Both
#' counts (n) and percentages (pct) are supported and can be specified with
#' the \code{\link{set_format_strings}} function. To allow for flexibility
Expand Down Expand Up @@ -190,22 +190,22 @@ add_layers <- function(parent, ...) {
#'
#' t <- tplyr_table(mtcars, am) %>%
#' add_layer(
#' group_shift(vars(row=gear, column=carb), by=cyl)
#' group_shift(quos(row=gear, column=carb), by=cyl)
#' )
group_count <- function(parent, target_var, by=vars(), where=TRUE, ...) {
group_count <- function(parent, target_var, by=quos(), where=TRUE, ...) {
tplyr_layer(parent, type='count', by=enquos(by), target_var=enquos(target_var), where=enquo(where), ...)
}

#' @rdname layer_constructors
#' @family Layer Construction Functions
#' @export
group_desc <- function(parent, target_var, by=vars(), where=TRUE, ...) {
group_desc <- function(parent, target_var, by=quos(), where=TRUE, ...) {
tplyr_layer(parent, type='desc', by=enquos(by), target_var=enquos(target_var), where=enquo(where), ...)
}

#' @rdname layer_constructors
#' @family Layer Construction Functions
#' @export
group_shift <- function(parent, target_var, by=vars(), where=TRUE, ...) {
group_shift <- function(parent, target_var, by=quos(), where=TRUE, ...) {
tplyr_layer(parent, type='shift', by=enquos(by), target_var=enquos(target_var), where=enquo(where), ...)
}
14 changes: 7 additions & 7 deletions R/meta_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,25 @@ get_meta_result.data.frame <- function(x, row_id, column, ...) {
#'
#' dat <- t %>% build(metadata = TRUE)
#'
#' get_meta_subset(t, 'd1_1', 'var1_4', add_cols = dplyr::vars(carb))
#' get_meta_subset(t, 'd1_1', 'var1_4', add_cols = rlang::quos(carb))
#'
#' m <- t$metadata
#' dat <- t$target
#'
#' get_meta_subset(t, 'd1_1', 'var1_4', add_cols = dplyr::vars(carb), target = target)
get_meta_subset <- function(x, row_id, column, add_cols = vars(USUBJID), ...) {
#' get_meta_subset(t, 'd1_1', 'var1_4', add_cols = rlang::quos(carb), target = target)
get_meta_subset <- function(x, row_id, column, add_cols = quos(USUBJID), ...) {
UseMethod("get_meta_subset")
}

#' @export
#' @rdname get_meta_subset
get_meta_subset.data.frame <- function(x, row_id, column,
add_cols = vars(USUBJID), target = NULL, ...) {
add_cols = quos(USUBJID), target = NULL, ...) {
# Get the metadata object ready
m <- get_meta_result(x, row_id, column)

if (!inherits(add_cols, 'quosures')) {
stop("add_cols must be provided using `dplyr::vars()`", call.=FALSE)
stop("add_cols must be provided using `rlang::quos()`", call.=FALSE)
}

# Subset and return the data
Expand All @@ -159,13 +159,13 @@ get_meta_subset.data.frame <- function(x, row_id, column,

#' @export
#' @rdname get_meta_subset
get_meta_subset.tplyr_table <- function(x, row_id, column, add_cols = vars(USUBJID), ...) {
get_meta_subset.tplyr_table <- function(x, row_id, column, add_cols = quos(USUBJID), ...) {

# Get the metadata object ready
m <- get_meta_result(x, row_id, column)

if (!inherits(add_cols, 'quosures')) {
stop("add_cols must be provided using `dplyr::vars()`", call.=FALSE)
stop("add_cols must be provided using `rlang::quos()`", call.=FALSE)
}

# Subset and return the data
Expand Down
8 changes: 4 additions & 4 deletions R/nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ process_nested_count_target <- function(x) {
}

first_layer <- process_summaries(group_count(current_env(), target_var = !!target_var[[1]],
by = vars(!!!by), where = !!where))
by = quos(!!!by), where = !!where))

second_layer <- process_summaries(group_count(current_env(), target_var = !!target_var[[2]],
by = vars(!!target_var[[1]], !!!by), where = !!where) %>%
by = quos(!!target_var[[1]], !!!by), where = !!where) %>%
set_count_row_prefix(indentation) %>%
set_denoms_by(!!!second_denoms_by))

Expand Down Expand Up @@ -82,8 +82,8 @@ process_nested_count_target <- function(x) {
target_var_saved <- target_var
is_built_nest <- TRUE

by <- vars(!!target_var[[1]], !!!by)
target_var <- vars(!!target_var[[2]])
by <- quos(!!target_var[[1]], !!!by)
target_var <- quos(!!target_var[[2]])


}, envir = x)
Expand Down
4 changes: 2 additions & 2 deletions R/process_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ process_metadata.desc_layer <- function(x, ...) {
# Transpose the metadata identical to the summary
form_meta[[i]] <- meta_sums[[i]] %>%
pivot_wider(id_cols=c(!!treat_var, match_exact(by)),
names_from = match_exact(vars(row_label, !!!cols)),
names_from = match_exact(quos(row_label, !!!cols)),
names_prefix = paste0('var', i, "_"),
values_from = meta
)
} else {
form_meta[[i]] <- meta_sums[[i]] %>%
pivot_wider(id_cols=c('row_label', match_exact(by)),
names_from = match_exact(vars(!!treat_var, !!!cols)),
names_from = match_exact(quos(!!treat_var, !!!cols)),
names_prefix = paste0('var', i, "_"),
values_from = meta
)
Expand Down
8 changes: 4 additions & 4 deletions R/set_format_strings.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
#' # In a shift layer
#' tplyr_table(mtcars, am) %>%
#' add_layer(
#' group_shift(vars(row=gear, column=carb), by=cyl) %>%
#' group_shift(quos(row=gear, column=carb), by=cyl) %>%
#' set_format_strings(f_str("xxx (xx.xx%)", n, pct))
#' ) %>%
#' build()
Expand Down Expand Up @@ -157,9 +157,9 @@ set_format_strings.desc_layer <- function(e, ..., cap=getOption('tplyr.precision

env_bind(e,
format_strings = format_strings,
summary_vars = vars(!!!summary_vars),
keep_vars = vars(!!!keep_vars),
trans_vars = vars(!!!trans_vars),
summary_vars = quos(!!!summary_vars),
keep_vars = quos(!!!keep_vars),
trans_vars = quos(!!!trans_vars),
row_labels = row_labels,
max_length = max_format_length,
need_prec_table = need_prec_table,
Expand Down
8 changes: 4 additions & 4 deletions R/shift_bindings.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
#' # and by variables sum to 1
#' tplyr_table(mtcars, am) %>%
#' add_layer(
#' group_shift(vars(row=gear, column=carb), by=cyl) %>%
#' group_shift(quos(row=gear, column=carb), by=cyl) %>%
#' set_format_strings(f_str("xxx (xx.xx%)", n, pct))
#' ) %>%
#' build()
#'
#' tplyr_table(mtcars, am) %>%
#' add_layer(
#' group_shift(vars(row=gear, column=carb), by=cyl) %>%
#' group_shift(quos(row=gear, column=carb), by=cyl) %>%
#' set_format_strings(f_str("xxx (xx.xx%)", n, pct)) %>%
#' set_denoms_by(cyl, gear) # Row % sums to 1
#' ) %>%
#' build()
#'
#' tplyr_table(mtcars, am) %>%
#' add_layer(
#' group_shift(vars(row=gear, column=carb), by=cyl) %>%
#' group_shift(quos(row=gear, column=carb), by=cyl) %>%
#' set_format_strings(f_str("xxx (xx.xx%)", n, pct)) %>%
#' set_denoms_by(cyl, gear, am) # % within treatment group sums to 1
#' ) %>%
Expand All @@ -47,7 +47,7 @@ set_denoms_by <- function(e, ...) {
#' @noRd
set_denoms_by.shift_layer <- function(e, ...) {

dots <- vars(...)
dots <- quos(...)
dots_chr <- map_chr(dots, as_name)

# Pull these variables to make sure the denoms used make sense
Expand Down
Loading
Loading