How to deal with multiple quotes in R?

Asked

Viewed 647 times

5

My problem is using quotes in a query within a R function.

I have to call a list of

select * from probes."probes_90_2018-05"

For that I do:

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

Then my problem begins, because R now thinks that probes_97_2018-06 is a variable. Already try using \" and /", but I did not succeed. So how is the escape of R?

Continuing: in the formed

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

worked, however I was curious to know why so Fucina 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"
  • 2

    And do single quotes inside double quotes work? type dbGetQuery(rabbit,"select * from Probes. 'probes_97_2018-06' ")

  • Have you tried the back Votes? (Sorry about the English.)

  • 1

    Thanks!!! In format dbGetQuery(coelho,"select * from probes.'probes_97_2018-06' LIMIT 2") does not work, but in the graduate dbGetQuery(coelho,'select * from probes."probes_97_2018-06" LIMIT 2') worked.

  • Now I was curious to know why so Lucina 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"

  • 1

    If you do so should work too, using the back-Slash in single quote. dbGetQuery(coelho,"select * from probes.\'probes_97_2018-06\' ") But note that from the documentation it seems that there is a concatenation of Quotation Marks that should be followed from the first Quotation Marks! The following are references taken from the documentation. https://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html

  • 1

    In Postgresql quotes(") have a special meaning: they indicate a delimited identifier or quoted Identifier. It is formed by placing an arbitrary string of characters between quotation marks and is interpreted as an identified and

Show 1 more comment

1 answer

3


In a sentence: to use multiple quotes in R, escapes the quotation marks of the text or wraps the text in quotation marks other than the quotation marks used in the text.

In more detail

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

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

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

'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 quotes, but for this we need to escape the quotes that go inside the string. Otherwise, the R would think that we are finishing the string and would expect to find an interpretable code in the following part (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"

The equality between the two types of external quotation marks can be observed with

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

Finally, we can have all kinds of quotation marks 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'"
  • Unfortunately, I won’t be able to test the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.