tensor_predictors/tensorPredictors/R/multi_assign.R

36 lines
1.1 KiB
R
Raw Normal View History

2022-03-22 15:26:24 +00:00
#' Multi-Value assign operator.
2020-06-10 14:35:27 +00:00
#'
#' @param lhs vector of variables (or variable names) to assign values.
#' @param rhs object that can be coersed to list of the same length as
#' \code{lhs} storing the values of the variables defined in \code{lhs}.
#'
#' @details The parameter names \code{lhs} and \code{rhs} stand for "Left Hand
#' Side" and "Right Hand Side", respectively.
#'
2021-11-04 12:05:15 +00:00
#' @examples \dontrun{
2020-06-10 14:35:27 +00:00
#' c(a, b) %<-% list(1, 2)
#' # is equivalent to
#' ## a <- 1
#' ## b <- 2
#'
#' # Switching the values of a, b could be done by.
#' c(a, b) %<-% list(b, a)
2021-11-04 12:05:15 +00:00
#' # note the usage of 'list' on the right side, otherwise an exraction of the
#' # first two values of the concatenated object is performed. See next:
2020-06-10 14:35:27 +00:00
#'
#' # Extract values.
#' c(d1, d2, d3) %<-% 1:10
2021-11-04 12:05:15 +00:00
#' # extracting the first three valus from the vector of length 10.
#' }
2020-06-10 14:35:27 +00:00
#'
2021-10-29 16:16:40 +00:00
#' @keywords internal
2020-06-10 14:35:27 +00:00
"%<-%" <- function(lhs, rhs) {
var.names <- make.names(as.list(substitute(lhs))[-1])
values <- as.list(rhs)
env <- parent.frame()
for (i in seq_along(var.names)) {
assign(var.names[i], values[[i]], envir = env)
}
2021-11-04 12:05:15 +00:00
lhs
2020-06-10 14:35:27 +00:00
}