R/utils.fable.align.R

Defines functions .fable.check.align is.valid.fable.align fable.align.options .fable.align.check fable.set.align fable.set.align.cell fable.set.align.row fable.set.align.col fable.add.align.row fable.add.align.column

#...............................................................................
#...............................................................................
# ADD ALIGN
#...............................................................................
#...............................................................................

#' @export
fable.add.align.column <- function(x, pos = 0, align = "center", DEBUG = FALSE) {
  .fable.align.check(x, align)
  attr(x,"ALIGN") <- add.col(attr(x,"ALIGN"),col = align, pos = pos, DEBUG = DEBUG)
  x
}

#' @export
#' @rdname fable.add.align.column
fable.add.align.col <- fable.add.align.column


#' @export
fable.add.align.row <- function(x, pos = 0, align = "center", DEBUG = FALSE) {
  .fable.align.check(x, align)
  attr(x,"ALIGN") <- add.row(attr(x,"ALIGN"), row = align, pos = pos, DEBUG = DEBUG)
  x
}




#...............................................................................
#...............................................................................
# SET ALIGN
#...............................................................................
#...............................................................................

#' @export
fable.set.align.col <- function(x,pos,align, DEBUG = FALSE) {
  if (is.null(x)) stop("x must be set")
  .fable.align.check(x, align)

  attr(x,"ALIGN") <- set.col(attr(x,"ALIGN"), pos = pos, col = align, DEBUG = DEBUG )
  x
}

#' @export
fable.set.align.row <- function(x,pos,align, DEBUG = FALSE) {
  if (is.null(x)) stop("x must be set")
  .fable.align.check(x, align)

  attr(x,"ALIGN") <- set.row(attr(x,"ALIGN"), pos = pos, row = align, DEBUG = DEBUG )
  x
}



#...............................................................................
#...............................................................................
# SET ALIGN --- SINGLE CELL
#...............................................................................
#...............................................................................

#' @export
fable.set.align.cell <- function(x,row,col,align, DEBUG = F) {
  if (is.null(x)) stop("x must be set")
  .fable.align.check(x, align)

  attr(x,"ALIGN") <- set.cell(attr(x,"ALIGN"), row = row, col = col, item = align, DEBUG = DEBUG )
  x

}


#...............................................................................
#...............................................................................
# SET ALIGN --- WHOLE MATRIX
#...............................................................................
#...............................................................................

#' @export
fable.set.align <- function(x,align = "right", DEBUG = FALSE) {
  if (is.null(x)) stop("x must be set")
  .fable.align.check(x, align)

  attr(x,"ALIGN") <- fill.matrix(attr(x,"ALIGN") , item = align, DEBUG = DEBUG)
  x
}


#...............................................................................
#...............................................................................
# ALIGN OPTIONS & CHECKS
#...............................................................................
#...............................................................................

.fable.align.check <- function(x, align) {
  .fable.check(x)
  .fable.check.align(align)
}


#' @export
fable.align.options <- function() {
  return(c("left","right","center"))
}

#' @export
is.valid.fable.align <- function(align) {
  if (is.null(align)) return(FALSE)
  if (all(align %in% fable.align.options())) return(TRUE)
  return(FALSE)
}


.fable.check.align <- function(align, stop.on.error = TRUE) {
  if (!is.valid.fable.align(align)) {
    m <- paste0("align must be one of: ",paste0(fable.align.options(), collapse = ","))
    if (stop.on.error) stop(m)
    message(m)
  }
}
feranpre/feR documentation built on Nov. 22, 2022, 2:29 a.m.