5

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.

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • 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. – Kolichikov Mar 24 '17 at 23:37
  • 1
    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. – alistaire Mar 24 '17 at 23:56
  • yes, I think single quotes can be easier to read too –  Mar 25 '17 at 00:18
  • 1
    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. –  Mar 25 '17 at 09:59

1 Answers1

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"
Rentrop
  • 20,979
  • 10
  • 72
  • 100
  • so the point here is that apostrophes are more common than double quotes when writing text? –  Mar 25 '17 at 00:16
  • 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. – Rentrop Mar 25 '17 at 00:21