R/helpers.R

my_round <- function(x, base, ...){
  if(nargs() > 2){
    warning("Warning: mar_round will only use the first two arguments")
  }
  if(base != round(base)){
    warning("Warning: mar_round has not been fully tested for non-integer bases")
  }
  if(nargs() < 2){
    stop("Error: mar_round requires an x and a base input")
  }
  if(!(typeof(x) %in% c("integer", "numeric", "double"))){
    stop("Error: x must be numeric")
  }
  if(!(typeof(base) %in% c("integer", "numeric", "double"))){
    stop("Error: base must be numeric")
  }

  x.rounded <- base * round(x/base)

  x.rounded
}

#' Function for getting the mode of a vector
#'
#' @source #' https://www.tutorialspoint.com/r/r_mean_median_mode.htm
my_mode <- function(v, na.rm = T, ...){
  if(nargs() > 2){
    warning("Warning: if more than 2 arguments are supplied,\n mar_mode will use the first numeric argument as the vector")
  }

  if(na.rm){
    v <- v[!is.na(v)]
  }
  uniqv <- unique(v)
  uniqv[which.max(tabulate(match(v, uniqv)))]
}

#' Function for saving tables to the clipboard.
#' Very useful for moving quickly to excel
#'
#' @param x Input table to copy
#' @param row.names Row names of input table
#' @param col.names Col names of input table
#' @author David Hesslink
my_copy <- function(x, row.names=FALSE, col.names=TRUE, ...){
  write.table(x, "clipboard-16384",
              sep ="\t",
              row.names = row.names,
              col.names = col.names, ...)
}
forrestdiamond/foRest documentation built on June 1, 2019, 3:56 a.m.