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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and `@default` tags to specify the permitted and default values and the
`@caption`, `@info`, and `@code` tags for examples with a caption and a
description. (#484)

- `replace_symbol_in_expr()` and `add_suffix_to_vars()` no longer fail if the
expression contains `NA`. (#490)

## Updates of Existing Functions

## Breaking Changes
Expand Down
20 changes: 17 additions & 3 deletions R/quo.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ replace_values_by_names <- function(expressions) {
#'
#' @param expression Expression
#'
#' @permitted a quoted expression, e.g., created by `expr()`
#'
#' @param target Target symbol
#'
#' @permitted [symbol]
#'
#' @param replace Replacing symbol
#'
#' @permitted [symbol]
#'
#' @author Stefan Bundfuss
#'
#' @return The expression where every occurrence of the symbol `target` is
Expand All @@ -73,12 +79,12 @@ replace_values_by_names <- function(expressions) {
#' @export
#'
#' @examples
#'
#' library(rlang)
#'
#' replace_symbol_in_expr(expr(AVAL), target = AVAL, replace = AVAL.join)
#' replace_symbol_in_expr(expr(AVALC), target = AVAL, replace = AVAL.join)
#' replace_symbol_in_expr(expr(desc(AVAL)), target = AVAL, replace = AVAL.join)
#' replace_symbol_in_expr(expr(if_else(AVAL > 0, AVAL, NA)), AVAL, AVAL.join)
replace_symbol_in_expr <- function(expression,
target,
replace) {
Expand All @@ -91,8 +97,16 @@ replace_symbol_in_expr <- function(expression,
}
} else {
for (i in seq_along(expression)) {
if (expression[[i]] == target) {
expression[[i]] <- replace
if (is.symbol(expression[[i]])) {
if (expression[[i]] == target) {
expression[[i]] <- replace
}
} else if (is.recursive(expression[[i]])) {
expression[[i]] <- replace_symbol_in_expr(
expression[[i]],
!!target,
!!replace
)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion man/replace_symbol_in_expr.Rd

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

1 change: 1 addition & 0 deletions man/roxygen/rdx_meta.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
list(
rdx_permitted_values = list(
char_scalar = "a character scalar, i.e., a character vector of length one",
symbol = "an unquoted symbol, e.g., `AVAL`",
var_list = "list of variables created by `exprs()`, e.g., `exprs(USUBJID, VISIT)`"
)
)
20 changes: 16 additions & 4 deletions tests/testthat/test-quo.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,21 @@ test_that("replace_symbol_in_expr Test 9: symbol in expression is replaced", {
)
})

## Test 10: works recursive and with NA ----
test_that("replace_symbol_in_expr Test 10: works recursive and with NA", {
expect_equal(
expected = expr(if_else(AVAL.join > 0, AVAL.join, NA)),
object = replace_symbol_in_expr(
expr(if_else(AVAL > 0, AVAL, NA)),
target = AVAL,
replace = AVAL.join
)
)
})

# add_suffix_to_vars ----
## Test 10: with single variable ----
test_that("add_suffix_to_vars Test 10: with single variable", {
## Test 11: with single variable ----
test_that("add_suffix_to_vars Test 11: with single variable", {
expect_equal(
expected = exprs(ADT, desc(AVAL.join), AVALC),
object = add_suffix_to_vars(
Expand All @@ -127,8 +139,8 @@ test_that("add_suffix_to_vars Test 10: with single variable", {
)
})

## Test 11: with more than one variable ----
test_that("add_suffix_to_vars Test 11: with more than one variable", {
## Test 12: with more than one variable ----
test_that("add_suffix_to_vars Test 12: with more than one variable", {
expect_equal(
expected = exprs(ADT, desc(AVAL.join), AVALC.join),
object = add_suffix_to_vars(
Expand Down
Loading