Skip to content

Instantly share code, notes, and snippets.

@klmr
Created September 29, 2014 12:51
Show Gist options
  • Save klmr/4f093bb49dcf1aa72b7a to your computer and use it in GitHub Desktop.
Save klmr/4f093bb49dcf1aa72b7a to your computer and use it in GitHub Desktop.
Auto-detect file format in read.table
read.table = function (file, ..., text) {
args = list(...)
if (missing(file))
return(do.call(utils::read.table, c(args, text = text)))
if (! ('sep' %in% names(args))) {
separators = list('.csv' = ',',
'.tsv' = '\t',
'.txt' = '\t')
extension = stringr::str_match(file, '(\\.\\w+)$')[, 2]
args$sep = separators[[extension]]
}
args$file = file
do.call(utils::read.table, args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment