knitr - How to extract the content of SQL-Files using R? -


i have of folder/directory of 1 of colleagues full of sqlstatements. folder updated him daily well. document these sqlstatements futures colleagues. however, i'm looking way "automate" process. thought use crontab once week , run r-markdown file automatically update existing r-markdown file.

my approach follows:

path = "c:/sql_files/" out.file<-"" file.names <- dir(path, pattern =".sql") # here changed `.txt` `.sql` for(i in 1:length(file.names)){ file <- read.csv2.sql(file.names[i],header=true, sep=";",    stringsasfactors=false)   out.file <- rbind(out.file, file) }   # second approach comes close, generates `.txt` first #`.sql` file in directory error:     error in match.names(clabs, names(xi)) :     names not match previous names  

where files are:

 [1] "c:/sql_files/first.sql"                                              [2] "c:/sql_files/second.sql"        path = "c:/sql_files/"  out.file<-"" files <- list.files(path=path, pattern="*.sql", full.names=t, recursive=false) for(i in 1:length(files)){   file <- read.table(files[i],header=true, sep=";", stringsasfactors=false)   out.file <- rbind(out.file, file) } 

the loop extracts content of .sql doesnt seem capture content @ all(in first example) or captures content of first file in directory (second example). question. there way extract content sql text file (.sql)? may result in .txt/.rmd follows: (but doesnt have to):

output of first loop: my_sql_statement.sql

output of second loop: select * data

this rmd file generates markdown/html document listing meta data , content of files specified:

--- title: "collection of sql files" author: "sqlcollectr" date: "`r format(sys.time(), '%y-%m-%d')`" output:    html_document:      keep_md: yes ---  ```{r setup, echo = false} library(knitr) path <- "files/" extension <- "sql" ```  document contains code files extension ``r extension`` in ``r paste0(getwd(), "/", path)``.  ```{r, results = "asis", echo = false} filenames <- list.files(path, pattern = sprintf(".*%s$", extension)) fileinfos <- file.info(paste0(path, filenames))  (filename in filenames) {   filepath <- paste0(path, filename)   cat(sprintf("## file `%s` \n\n### meta data \n\n", filename))   cat(sprintf(     "| size (kb) | mode | modified |\n|---|---|---|\n %s | %s | %s\n\n",      round(fileinfos[filepath, "size"]/1024, 2),      fileinfos[filepath, "mode"],     fileinfos[filepath, "mtime"]))   cat(sprintf("### content\n\n```\n%s\n```\n\n", paste(readlines(filepath), collapse = "\n"))) }  ``` 

all work done in for loop iterates on files in path names end in extension. each file, table "meta data" printed, followed actual file content. meta data retrieved using file.info , consists of file size, mode , last modified timestamp.

the cat(sprintf(... constructs containing markdown make code complicated, fact simple.

sample output

using sql files sql statements this answer, rmd file above generates following output (using html output format):

sample output


Comments