How to handle multiple quotes in R?

6

My problem is to use the quotation marks in a query within an R function.

I have to call a list of

select * from probes."probes_90_2018-05"

For this I do:

coelho<-dbConnect("PostgreSQL", fnord)
dbGetQuery(coelho,"select *
from probes."probes_97_2018-06"")
dbDisconnect(coelho)

So my problem starts, because R now finds that probes_97_2018-06 is a variable. I already tried using \" and /" , but I was not successful. So how is the escape from R?

Continuing: not formed

dbGetQuery(coelho,'select * from probes."probes_97_2018-06" LIMIT 2') 

It worked, though I was curious to know why it fucked and more when I do:

> 'select * from probes."probes_97_2018-06" LIMIT 2'

returns

[1] "select * from probes.\"probes_97_2018-06\" LIMIT 2"
    
asked by anonymous 11.07.2018 / 23:17

1 answer

4

In a sentence: To use multiple quotation marks in R , escape the quotation marks of the text or enclose the text in quotation marks other than the quotation marks used in the text.

In greater detail

does not differentiate double or single quotes to designate vector of class character .

"Um texto" # duplas
# [1] "Um texto"
'Um texto' # simples
# [1] "Um texto"

The thing changes, however, within string . In this case, R seeks to preserve past information (as the question itself demonstrates).

'select * from probes."probes_97_2018-06" LIMIT 2'
# [1] "select * from probes.\"probes_97_2018-06\" LIMIT 2"

The same result can be obtained with double quotation marks, but for this we need to escape the quotes that go within string . Otherwise, R would think that we are ending the string and would expect to find an interpretable code in the part that follows ( probes_97_2018-06 ).

# sem escapar as aspas
"select * from probes."probes_97_2018-06" LIMIT 2"
# Erro: unexpected symbol in ""select * from probes."probes_97_2018"

This can be avoided by escaping the quotation marks

# aspas de dentro escapadas
"select * from probes.\"probes_97_2018-06\" LIMIT 2"
# [1] "select * from probes.\"probes_97_2018-06\" LIMIT 2"

Equality between the two types of external quotes can be observed with

"aspas" == 'aspas'
# [1] TRUE

Finally, we can have all types of quotes inside a string .

"string em aspas duplas com aspas \"duplas\" e 'simples'"
# [1] "string em aspas duplas com aspas \"duplas\" e 'simples'"
'string em aspas simples com aspas "duplas" e \'simples\''
# [1] "string em aspas simples com aspas \"duplas\" e 'simples'"
    
04.01.2019 / 03:18