Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep all escape characters in a SQL string for querying a POSTGRES DB using R?

I am using R to query data from a Postgres database. The query I created in pgAdmin3 has lots of escape characters in it, and I'd to pull in the entire SQL string without having to deal with escape characters again, something like Python's triple quotes """.

SELECT brand
,regexp_replace(upper(hybridtype), '\(.*\)|\/|-|TM|\s','','g') as hybrid
FROM seeds

How can I get that SQL text into R retaining all the characters?

like image 470
JessieinAg Avatar asked Jun 03 '26 12:06

JessieinAg


1 Answers

in PGAdmin, copy the text into your clipboard. Then in R:

  sql_qry <- clipPaste()

Where clipPaste is defined as follows:

clipPaste <- function(flat=TRUE) {
    con <- pipe("pbpaste", open = "rb")
    ret <- readLines(con, warn = FALSE)
    if (flat)
        ret <- paste0(ret, collapse = "\n")
    close(con)
    return(ret)
}

All characters will be appropriately escaped.

like image 195
Ricardo Saporta Avatar answered Jun 06 '26 06:06

Ricardo Saporta