R/utils.R

#' Converts object to certain type, preserving the NULL value.
#' @param x Object to cast.
#' @param as.type A casting function for x
#' @return x casted, or NULL.
#' @export
as.type_or_null <- function(x,as.type){
  if(is.null(x)){
    return(NULL)
  }
  else{
    return(as.type(x))
  }
}

#' Converts object to integer, preserving the NULL value.
#' @param x Object to cast.
#' @return x casted, or NULL.
#' @export
as.integer_or_null <- function(x){
  return(as.type_or_null(x,as.integer))
}

#' Converts object to numeric, preserving the NULL value.
#' @param x Object to cast.
#' @return x casted, or NULL.
#' @export
as.numeric_or_null <- function(x){
  return(as.type_or_null(x,as.numeric))
}

#' Converts every numeric item in the list to integer.
#' @param x List.
#' @return A list with every numeric item as integer.
#' @export
numeric.as.integer <- function(x){
  if(is.null(x)) return(NULL)
  return(lapply(x, function(s){if(is.numeric(s) && !is.null(s)){ return(as.integer(s))} else{return(s)}}))
}

#' Console log
#' @param fmt Text to log.
#' @export
console.log <- function(fmt,...) {
  print(paste(format(Sys.time(), "%H:%M:%S"), sprintf(fmt, ...)))
}
jlsuarezdiaz/rDML documentation built on May 24, 2019, 12:35 a.m.