R/tableCheckRead.R

#' peak/ co-variate table error handling and/ or read from character string
#' @param tmpTable  either a data.frame, full file path as a character string to a text file (.csv/.txt/.tsv) of a table 
#' If argument is not supplied a GUI file selection window will open and a text file (.csv/.txt/.tsv) can be selected.
#' @param type description of table type as character string for gui windows and error messages. 
#' @param ... additional arguments to \code{\link{fread}}.
tableCheckRead <- function(tmpTable=NULL, type="peak-picker", ...){
  
if(is.null(tmpTable)){
  message("file chooser window opened...")
  flush.console()
  tmpTable <- tcltk::tclvalue(tcltk::tkgetOpenFile(filetypes = "{{comma delimited text file} {.csv}} {{All files} *}",
                                                    title=paste0("Select your ", type, " table")))
  # read in table if necessary
  tmpTable <- as.data.frame(data.table::fread(tmpTable, header=TRUE, ...), 
                            stringsAsFactors=FALSE)   
} else if(is.character(tmpTable)){
  # read in table from character string if necessary
  tmpTable <- as.data.frame(data.table::fread(tmpTable, header=TRUE, ...), 
                            stringsAsFactors=FALSE)  
} 
if(!is.data.frame(tmpTable)){
  stop(paste0(type, " argument is not a data.frame"))
}
  # check first column is integer i.e. eic number
  if(!is.integer(tmpTable[, 1])){
    stop('The first column of the peak table must be a unique identifying integer (i.e. EIC number)')
  }
return(tmpTable)
}
WMBEdmands/MetMSLine documentation built on May 9, 2019, 10:03 p.m.