Skip to content

Commit 00d32c6

Browse files
committed
new helper function repair_variable_names
closes #317
1 parent c6ab463 commit 00d32c6

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ export(r_scale)
463463
export(rdo)
464464
export(rename_variables)
465465
export(repair_draws)
466+
export(repair_variable_names)
466467
export(resample_draws)
467468
export(reserved_variables)
468469
export(rfun)

R/draws-index.R

+30-3
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ remove_reserved_variable_names <- function(variables, reserved) {
145145

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

178+
#' @rdname set_variables
179+
#' @param old_variables (character) Variable names to repair. Should be variable
180+
#' names with periods to convert to square brackets (e.g., `"theta.1"` ->
181+
#' `"theta[1]"`, `"theta.1.1"` -> `"theta[1,1]"`).
182+
#' @examples
183+
#' # using repair_variable_names
184+
#' x <- matrix(rnorm(100), ncol = 2)
185+
#' colnames(x) <- c("theta.1", "theta.2")
186+
#' repair_variable_names(colnames(x))
187+
#' x <- set_variables(as_draws(x), repair_variable_names(colnames(x)))
188+
#' variables(x)
189+
#'
190+
#' @export
191+
repair_variable_names <- function(old_variables) {
192+
if (!all(grepl("\\.", old_variables))) {
193+
stop_no_call(
194+
"All names in 'old_variables' must contain at least one '.' in the name."
195+
)
196+
}
197+
repaired_variables <- sub("\\.", "[", old_variables)
198+
repaired_variables <- gsub("\\.", ",", repaired_variables)
199+
repaired_variables[grep("\\[", repaired_variables)] <-
200+
paste0(repaired_variables[grep("\\[", repaired_variables)], "]")
201+
repaired_variables
202+
}
176203

177204
#' @rdname draws-index
178205
#' @export

man/set_variables.Rd

+19-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-variables.R

+14
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,17 @@ test_that("cannot set duplicate variable names", {
109109
expect_error(set_variables(x, c("a", "a")), "Duplicate variable names are not allowed")
110110
})
111111

112+
test_that("repair_variable_names does the right conversion", {
113+
expect_equal(
114+
repair_variable_names(c("foo.12", "foo.12.13", "foo.12.13.14.15")),
115+
c("foo[12]", "foo[12,13]", "foo[12,13,14,15]")
116+
)
117+
})
118+
119+
test_that("repair_variable_names errors if all names don't contain '.'", {
120+
expect_error(
121+
repair_variable_names(c("foo[12]", "foo.12.13")),
122+
"All names in 'old_variables' must contain at least one '.' in the name."
123+
)
124+
})
125+

0 commit comments

Comments
 (0)