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

New helper function repair_variable_names #318

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I went with old_variables as the argument name, but happy to change this based on feedback.

#' 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."
)
Comment on lines +193 to +195
Copy link
Member Author

@jgabry jgabry Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For simplicity I added an error if all the names don't have a . in it. It would be strange to pass in a vector like c("theta.1", "theta[2]") so I don't think we need to write out the logic to handle mixed cases like that. But let me know if you disagree and I can add that logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic seems very unhelpful if I have some variables which are scalars. I can no longer just pass the entire list to this function

I believe the set of transformations here is idempotent, so if someone passes theta[1], nothing will be changed, so why not allow it

}
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."
)
})

Loading