In R, why do style guides recommend that one only use double quotes?
Asked Answered
G

1

6

The lintr package has a check for single-quoted strings, and single quotes are discouraged elsewhere (e.g. ?Quotes). Single and double quoted strings work identically in R, so is there a reason why single quotes are considered bad practice?

Citations to canonical documents especially welcome. To be clear: I'm asking about the reasons given by the R core team for discouraging single quotes; not about people's own opinions on the topic.

Grating answered 24/3, 2017 at 23:33 Comment(4)
I suspect this is to align with C/C++, where single quotes are used for single characters ('x'), and double quotes are used for strings. This makes the transition more "natural' if you're coming from those languages.Lengthwise
Because ?Quotes says so, if you like, but it will never affect code (aside from what you need to escape), so I'll likely continue to use single quotes to save on those extra shift keystrokes.Millrace
yes, I think single quotes can be easier to read tooGrating
I don't think this is primarily opinion-based. I am asking for the reasons why the R maintainers discourage single quotes. This is a matter of fact. I am not asking for people's personal opinions of whether single quotes are good.Grating
Q
4

I guess because R by default prints using double quotes. This way the 'other quote character' in a string gets

a) handled normally if its a single quote
b) escaped when its a double quote

when printing. Using double-quotes discourages the use of double-quotes in a string.

Take the following example:

Double Quoted

"It's my car"

Prints

"It's my car"

Single Quoted

'It"s my car'

Prints

"It\"s my car"
Quentinquercetin answered 24/3, 2017 at 23:48 Comment(2)
so the point here is that apostrophes are more common than double quotes when writing text?Grating
That on the one hand and if you need a quote-sign within a string the (default) print-output is nicer if the quote-sign inside this string is a single-quote-sign.Quentinquercetin

© 2022 - 2024 — McMap. All rights reserved.