-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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." | ||
) | ||
Comment on lines
+193
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
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 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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.