
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  fUtils.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                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   fRowcount          ( file, header=FALSE, dont.execute=FALSE, showWarnings=TRUE, verbose=FALSE )                          #
  #   fHead              ( file, n=10, dont.execute=FALSE, showWarnings=TRUE, verbose=FALSE )                                  #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

fRowcount <- function(file, header=FALSE, dont.execute=FALSE, showWarnings=TRUE, verbose=FALSE) {
## file can be vector
## header can be TRUE/FALSE or a numeric for number of lines to skip (vector okay)
## dont.execute :: if TRUE, returns just the cmd

  ## Error check
  # old:  if (!length(file))
  # old:    return (file)
  # old:  if (!is.character(file))
  # old:    stop ("'file' should be a character")
  is.char_of_length1(file, fail.if.not=TRUE)

  file.clean <- shellClean(file)
  cmd <- ifelse(grepl("\\.gz$", file)
              , yes = sprintf("zcat %s | wc -l", file.clean)
              , no  = sprintf("wc -l %s", file.clean)
              )

  if (verbose)
    cat(cmd, fill=TRUE)

  if (dont.execute)
    return(cmd)

  ## if any not found, iterate over each cmd
  if (any(notfound <- !file.exists(file) | is.na(file))) {
    verboseMsg(showWarnings, "file(s) not found:\n    ", paste0("'", file[notfound], "'", collapse="\n    "))
    if (all(notfound))
      return(rep(NA_integer_, length(file)))
    ret <- sapply(seq_along(file), function(i) if (notfound[[i]]) NA_integer_ else as.integer(system(cmd[[i]], intern=TRUE)), USE.NAMES=FALSE)
  } else {
    if (length(cmd) > 1)
      cmd <- pasteC(cmd, C=";")

    ret <- system(cmd, intern=TRUE) %>% removeText(" .*", .)

    ## safety check.  Sometimes we introduce NAs
    if (any(is.na(as.num.nowarn(ret)) & !is.na(ret))) {
      warning ("output from cmd cannot be converted to numeric without introducing NAs.\nOriginal values: ")
      print(ret)
    } else {
      ret <- as.numeric(ret)
    }
  }

  ## Subtract number of header rows
  if (any(as.logical(header)))
    ret <- (ret - header)

  ## ELSE
  return(ret)
}

fHead <- function(file, n=10, dont.execute=FALSE, showWarnings=TRUE, verbose=FALSE) {

  ## Error check
  if (!length(file))
    return (file)
  if (!is.character(file))
    stop ("'file' should be a character")

  file.clean <- shellClean(file)
  cmd <- ifelse(grepl("\\.gz$", file)
              , yes = sprintf("head %s -n %i", file.clean, n)
              , no  = sprintf("zcat %s | head -n %i", file.clean, n)
              )


  ## if any not found, iterate over each cmd
  if (any(notfound <- !file.exists(file) | is.na(file))) {
    verboseMsg(showWarnings, "file(s) not found:\n    ", paste0("'", file[notfound], "'", collapse="\n    "))
    if (all(notfound))
      return(rep(NA_character_, length(file)))
    ret <- lapply(seq_along(file), function(i) if (notfound[[i]]) NULL else system(cmd[[i]], intern=TRUE))
  } else {
    if (length(cmd) > 1)
      cmd <- pasteC(cmd, C=";")

    ret <- system(cmd, intern=TRUE)
  }

  return (ret)
}
