R/as-term.R

Defines functions as_term.mcmcrs as_term.mcmcr as_term.mcmcarray

#' @importFrom term as.term
#' @export
term::as.term

#' @importFrom term as_term
#' @export
term::as_term

#' @export
as_term.mcmcarray <- function(x, ...) {
  x <- pdims(x)

  if (identical(x, 1L)) {
    return(as_term("parameter"))
  }

  if (identical(length(x), 1L)) {
    return(as_term(paste0("parameter[", 1:x, "]")))
  }

  x <- lapply(x, function(x) 1:x)
  x <- expand.grid(x)
  x <- as.matrix(x)
  x <- apply(x, 1, function(x) paste(x, collapse = ","))
  x <- paste0("parameter[", x, "]")
  as_term(x)
}

#' @export
as_term.mcmcr <- function(x, ...) {
  parameters <- names(x)
  x <- lapply(x, as_term)
  x <- mapply(x, parameters,
    FUN = function(x, y) {
      sub("parameter", y, x, fixed = TRUE)
    },
    SIMPLIFY = FALSE
  )
  as_term(unname(unlist(x)))
}

#' @export
as_term.mcmcrs <- function(x, ...) as_term(x[[1]])

Try the mcmcr package in your browser

Any scripts or data that you put into this service are public.

mcmcr documentation built on Sept. 7, 2021, 1:07 a.m.