Skip to content

Instantly share code, notes, and snippets.

@JoFrhwld
Created May 23, 2014 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoFrhwld/26c42b35a5b4fc5a5bb8 to your computer and use it in GitHub Desktop.
Save JoFrhwld/26c42b35a5b4fc5a5bb8 to your computer and use it in GitHub Desktop.
#' SQL Load
#'
#' This is function meant to be used along with ldply to read data in using sqldf.
#'
#' @param x the path to a file to be read
#' @param selection the columns to return. Defaults to \code{"*"}
#' @param condition conditions defining which data rows to load in SQL
#' @param file.format an argument to be passed to \code{sqldf}.
#' Defaults to assume a tab-delimited file with a header row.
#' See \code{?sqldf} for more info
#'
#' @return A data frame
#'
#' @export
#' @import sqldf
sql_load <- function(x, selection = "*", condition, file.format = list(header = TRUE, sep = "\t")){
fi <- file(x)
df <- sqldf(paste("select", selection, "from fi", condition, sep = " "),
file.format = file.format)
close(fi)
if(nrow(df)>0){
df$File <- basename(x)
}
return(df)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment