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

前端 未结 5 1571
清歌不尽
清歌不尽 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条回答
  •  萌比男神i
    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.

提交回复
热议问题