
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  DT_funcs.r                                                                             #
  #           Last Updated Funclist  :  08 Feb 2015,  5:11 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  stringr                                                                                #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   colquote           ( colNamesAsStrings )                                                                                 #
  #   uniqueRows         ( DT )                                                                                                #
  #   tbls               ( envir=.GlobalEnv )                                                                                  #
  #   getdotsWithEval    (  )                                                                                                  #
  #   setkeyE            ( x, ..., verbose=getOption("datatable.verbose") )                                                    #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

## 2015-01-20 NOTE:  Most of these are old
##   Written when I first started using data.table
##   Most of these I've learned better ways to execute
## 
## The only one that may still be useful is colquote()


        # --------------------------------------- #
        #                                         #
                  #------------------# 
                  # DATA TABLE UTILS #
                  #------------------# 
        #_________________________________________#






colquote <- function(colNamesAsStrings) {
  # Converts a vector of strings to a quoted (expression) list. 
  #  eg:  converts:   c("colName1", "colName2")
  #       to:         quote(list(colName1, colName2))
     
  as.call(lapply(c("list", colNamesAsStrings), as.symbol))
}


uniqueRows <- function(DT) { 
  # IF DT IS KEYED, FUNCTION ACTS SIMILAR TO unique.data.frame(.)

  # If not keyed (or not a DT), use regular unique(DT)
  if (!haskey(DT) ||  !is.data.table(x) )
    return(unique(DT))

  .key <- key(DT) 
  setkey(DT, NULL)
  setkeyE(unique(DT), eval(.key))
}   

            #---------------# 

.tbls <- function(envir=.GlobalEnv)  
  # shorter tables() summary with column count
  tables(env=envir, silent=TRUE)[,list(NAME, MB, NROW, NCOL= 1 + stringr::str_count(COLS, ","), KEY)]  


getdotsWithEval <- function () {
    dots <- 
      as.character(match.call(sys.function(-1), call = sys.call(-1), 
          expand.dots = FALSE)$...)

    if (grepl("^eval\\(", dots) && grepl("\\)$", dots))
      return(eval(parse(text=dots)))
    return(dots)
}

setkeyE <- function (x, ..., verbose = getOption("datatable.verbose")) {
  # SAME AS setkey(.) WITH ADDITION THAT 
  # IF KEY IS WRAPPED IN eval(.) IT WILL BE PARSED
    if (is.character(x)) 
        stop("x may no longer be the character name of the data.table. The possibility was undocumented and has been removed.")
    #** THIS IS THE MODIFIED LINE **#
    # OLD**:  cols = getdots()
    cols <- getdotsWithEval()
    if (!length(cols)) 
        cols = colnames(x)
    else if (identical(cols, "NULL")) 
        cols = NULL
    setkeyv(x, cols, verbose = verbose)
}

