#' @name set_values
#' @title Set multiple values useing one function call
#' @param mat a matrix object
#' @param ... formulae; left hand values should be two-element interger vectors and right-hand: a single-value numeric
#' @return matrix
#' @description
#' This functions allows to set multiple elements of a matrix
#' instead of using annoying step-by-step assignment by
#' mat[1,2] <- 2
#' mat[2,3] <- 0.5
#' etc.
#' @examples
#' mat <- matrix(0, 4, 5)
#' set_values(mat, c(1,1) ~ 5, c(3, 4) ~ 0.3)
#' @export
set_values<- function(mat, ...){
exprs <- list(...)
is.formula <- sapply(exprs, function(x) inherits(x, 'formula'))
if(!all(is.formula))
stop(paste0("Following arguments are not formulae: ", exprs[!is.formula]))
for(expr in exprs){
args <- strsplit(as.character(expr), "~", fixed = TRUE)
args <- args[args != ""]
lh <- eval(parse(text = args[[1]]))
rh <- as.numeric(args[[2]])
mat[lh[1], lh[2]] <- rh
}
mat
}
#' @rdname set_values
#' @export
sv <- set_values
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.