
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  log_unlog_swap.r                                                                       #
  #           Last Updated Funclist  :  08 Feb 2015,  5:12 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   log_unlog_swap     ( x, transform_to=c("unlog", "log"), pat.gets_changed="(units|revenue|pred)"                          #
  #                        , inverse_transform=c("unlog", "log") )                                                             #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

log_unlog_swap <- function(x, transform_to=c("unlog", "log"), pat.gets_changed="(units|revenue|pred)", inverse_transform=c("unlog", "log")) {
## If transform_to is given, use that.
## Otherwise, invert.  However, notice that sometimes it is hard to detect the correct way to invert.  Specifically with DT[, get(colName)].   Therefore, I've added an inverse_transform argument.
## inverse_transform :: Whatever value is given, transform to the opposite. 


  dict.invert <- c("log"="unlog", "unlog"="log")

  if (missing(x)) {
    return(dict.invert[[transform_to]])
  }

  if (!missing(transform_to)) {
    transform_to <- match.arg(transform_to)
  ## OTHERWISE, INVERT
  } else if (!missing(inverse_transform)) {
    inverse_transform <- match.arg(inverse_transform)
    transform_to <- dict.invert[inverse_transform]
  } else {
      ## Check for an attribute which would indicate
      if (!is.null(l.attr <- attr(x, "log_unlog_swap", exact=TRUE)))
          transform_to <- dict.invert[[l.attr]]
      else 
          transform_to <- ifelse (any(grepl("log", c(substitute(x), x))), yes="unlog", no="log")
  }

  ## set attribute for future inversions
  x <- copy(x)
  attr(x, "log_unlog_swap") <- unname(transform_to)


  ## If making it log, numbers are simple. Columns, we need to check them. 
  if (transform_to == "log") {
      ## NUMBERS
      if (is.numeric(x))
          return (log10(x))

      ## CHARACTERS
      if (any(grepl("^log", x)))
          return(x)

      ## ELSE
      x[grepl(pat.gets_changed, x, ignore.case=TRUE)] <- paste0("log.", x[grepl(pat.gets_changed, x, ignore.case=TRUE)])
      return(x)
  } else if (transform_to == "unlog") {
      ## NUMBERS
      if (is.numeric(x))
          return (10^(x))

      ## CHARACTERS
      return(gsub("^log\\.", "", x))
  } else 
    stop("internal error.  transform_to is '", transform_to, "' an invalid value")
}
