Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

Pass .sql file into DBI::dbGetQuery with comments as the first line into R / RStudio

$
0
0

I am trying to pass a .sql file into DBI::dbGetQuery, but I run into problems whenever there is comment in the first line. For example:

runs_fine.sql

SELECT * FROM mytable

does_not_work.sql

-- Some comment
SELECT * FROM mytable

Then in R:

library(odbc)
library(tidyverse)

con <- dbConnect(odbc(), "my_connection")
dbGetQuery(con, read_file("sql/runs_fine.sql")) # success
dbGetQuery(con, read_file("sql/does_not_work.sql")) # fails

Motivation: I would like to leverage the convenient sql preview function from RStudio, but this requires the first line be the following for any .sql file:

-- !preview conn=con

...which causes the dbGetQuery to fail. I suppose a workaround would be to use some sort of read.file function that can skip lines or avoid comment lines.

NOTE: If a comment line is in the middle of a .sql file, dbGetQuery succeeds as expected and avoids the appropriate commented lines.


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>