Skip to content

Instantly share code, notes, and snippets.

@sudevschiz
Created December 7, 2018 06:47
Show Gist options
  • Save sudevschiz/87306615edffd4716aa36b2a22614d94 to your computer and use it in GitHub Desktop.
Save sudevschiz/87306615edffd4716aa36b2a22614d94 to your computer and use it in GitHub Desktop.
How to read directly from .sql files to R. #https://stackoverflow.com/a/44886192/3498721
#Function found in the stackoverflow question.
getSQL <- function(filepath){
con = file(filepath, "r")
sql.string <- ""
while (TRUE){
line <- readLines(con, n = 1)
if ( length(line) == 0 ){
break
}
line <- gsub("\\t", " ", line)
if(grepl("--",line) == TRUE){
line <- paste(sub("--","/*",line),"*/")
}
sql.string <- paste(sql.string, line)
}
close(con)
return(sql.string)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment