Skip to content

Commit

Permalink
new helper function repair_variable_names
Browse files Browse the repository at this point in the history
closes #317
  • Loading branch information
jgabry committed Nov 17, 2023
1 parent c6ab463 commit 00d32c6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ export(r_scale)
export(rdo)
export(rename_variables)
export(repair_draws)
export(repair_variable_names)
export(resample_draws)
export(reserved_variables)
export(rfun)
Expand Down
33 changes: 30 additions & 3 deletions R/draws-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ remove_reserved_variable_names <- function(variables, reserved) {

#' Set variable names in `draws` objects
#'
#' Set variable names for all variables in a [`draws`] object. Useful
#' when using pipe operators.
#' Set variable names for all variables in a [`draws`] object. Useful when using
#' pipe operators. The additional helper function `repair_variable_names()` can
#' convert variable names using periods (e.g. `"theta.1"`) to the more common
#' square brackets (`"theta[1]"`) that are better supported by the package.
#'
#' @param x (draws) A [`draws`] object.
#' @param variables (character) new variable names.
#' @param variables (character) New variable names.
#' @template args-methods-dots
#'
#' @return Returns a [`draws`] object of the same format as `x`, with
Expand All @@ -173,6 +175,31 @@ set_variables <- function(x, variables, ...) {
return(x)
}

#' @rdname set_variables
#' @param old_variables (character) Variable names to repair. Should be variable
#' names with periods to convert to square brackets (e.g., `"theta.1"` ->
#' `"theta[1]"`, `"theta.1.1"` -> `"theta[1,1]"`).
#' @examples
#' # using repair_variable_names
#' x <- matrix(rnorm(100), ncol = 2)
#' colnames(x) <- c("theta.1", "theta.2")
#' repair_variable_names(colnames(x))
#' x <- set_variables(as_draws(x), repair_variable_names(colnames(x)))
#' variables(x)
#'
#' @export
repair_variable_names <- function(old_variables) {
if (!all(grepl("\\.", old_variables))) {
stop_no_call(
"All names in 'old_variables' must contain at least one '.' in the name."
)
}
repaired_variables <- sub("\\.", "[", old_variables)
repaired_variables <- gsub("\\.", ",", repaired_variables)
repaired_variables[grep("\\[", repaired_variables)] <-
paste0(repaired_variables[grep("\\[", repaired_variables)], "]")
repaired_variables
}

#' @rdname draws-index
#' @export
Expand Down
22 changes: 19 additions & 3 deletions man/set_variables.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ test_that("cannot set duplicate variable names", {
expect_error(set_variables(x, c("a", "a")), "Duplicate variable names are not allowed")
})

test_that("repair_variable_names does the right conversion", {
expect_equal(
repair_variable_names(c("foo.12", "foo.12.13", "foo.12.13.14.15")),
c("foo[12]", "foo[12,13]", "foo[12,13,14,15]")
)
})

test_that("repair_variable_names errors if all names don't contain '.'", {
expect_error(
repair_variable_names(c("foo[12]", "foo.12.13")),
"All names in 'old_variables' must contain at least one '.' in the name."
)
})

0 comments on commit 00d32c6

Please sign in to comment.