Can I gracefully include formatted SQL strings in an R script?

前端 未结 5 1561
清歌不尽
清歌不尽 2020-12-14 04:55

I\'m working in an R script that uses a long SQL string, and I would like to keep the query relatively free of other markup so as to allow copying and pasting between editor

相关标签:
5条回答
  • 2020-12-14 05:35

    A graceful way of "including" a long SQL query is to keep it in a separate .sql file. Preferably somewhere it can be syntax highlighted, a text file in RStudio will do the job. You can then in your main R script read the file into a string and populate it with variables using one of the many "named" sprintf-type solutions, such as infuser.

    .sql

    select *
    from mytable
    where id = {{a}} 
    and somevar = {{b}}
    

    .R

    library(readr)
    library(infuser)
    
    query <- read_file("query.sql") %>%
             infuse(a = 1, b = 2) 
    
    0 讨论(0)
  • 2020-12-14 05:40

    If you're an old C programmer from way back, as I am, you might enjoy just using sprintf().

    Borrowing Ian's example:

    y<-"y1"
    x<-"somethingorother"
    query <- sprintf(
    'SELECT DISTINCT x AS %s,
                     y AS %s,
     FROM tbl
     WHERE id=%%s
     AND num=%%d', x, y)
    

    yields:

    > cat(query,"\n")
    SELECT DISTINCT x AS somethingorother,
                     y AS y1,
     FROM tbl
     WHERE id=%s
     AND num=%d 
    
    0 讨论(0)
  • 2020-12-14 05:43

    I've ended up simply hitting the sql string with sql <- gsub("\n","",sql) and sql <- gsub("\t","",sql) before running it. The string itself can be as long as it needs to be, but stays free of any concatenation markup.

    0 讨论(0)
  • 2020-12-14 05:55

    I'd recommend just using a plain string, and not embedding variable values into it. Use placeholders instead.

    sql <- "SELECT foo FROM bar
        WHERE col1 = ?
        AND col2 = ?
        ORDER BY yomama"
    

    I'm not sure if the double-quote is the best way to embed multi-line strings in R code (is there something like here-docs?), but it does work, unlike in Java.

    Is there some reason you don't want to send "\n" or "\t" to your database? They should be fine in the SQL.

    0 讨论(0)
  • 2020-12-14 05:59

    you can override the %+% operator to have better string concatination syntax:

    '%+%' <- function(x,y) paste(x,y,sep="")
    
    y<-"y1"
    x<-"somethingorother"
    query<-
    'SELECT DISTINCT x AS ' %+% x %+%',\n'    %+%
    '                y AS ' %+% y %+% '\n'    %+%
    ' FROM tbl
     WHERE id=%s
     AND num=%d'
    
    cat(query,"\n")
    

    yields:

    > cat(query,"\n")
    SELECT DISTINCT x AS somethingorother,
                    y AS y1
     FROM tbl
     WHERE id=%s
     AND num=%d 
    
    0 讨论(0)
提交回复
热议问题