0

In R / RStudio, a "comment" is defined by the # character.

I would like to "comment" using ' instead of # in R.

Is it possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

3 Answers3

3

The comment character is determined by the R parser and that's not something you can control I'm afraid. You'd have to build some pipeline to take your code with single quote comments and translate that to pound sign comments before running which is kind of how Rmarkdown documents work (but that would really be overkill for such a change).

Mark
  • 7,785
  • 2
  • 14
  • 34
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you for your answer. I was indeed searching for an easy simple way. I think # is a poor choice, but who am I to judge ? – Madan Daniel Jul 15 '21 at 02:01
2

You can do this. If there are any single quote characters within the comment they will have to be escaped as usual. You can also use double quotes instead of single quotes or raw quotes r"{...}" as described in ?Quotes . If you want to add the comment to the end of a code line then it will need to be separated from the prior code on that line with a semicolon and in any case should not be the last line in the function.

f <- function(x) {
  'A function
   that returns its argument'
  x
}

f(9)
## [1] 9
   
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • 2
    I'm sorry but I don't understand your answer. How are you achieving the desired result ? Can I write : ' blabla and get a comment with it ? I apologize if I missed anything. – Madan Daniel Jul 15 '21 at 02:10
  • Exactly what is it you don't understand??? Is it not obvious that the answer exhibits a function that uses '...' as a comment which is what the question asked for. – G. Grothendieck Jul 15 '21 at 02:14
  • To access #, I need to press TWO keys -> [Alt] + [ 3 ] I want to reduce it to ONE key -> [ ' ] Your solution requires me to press [ f ] + [Shift] + [8] + [Shift] + [8] Should I continue my argument ? – Madan Daniel Jul 15 '21 at 21:31
  • # is shift 3 on a normal PC keyboard and # is on the upper row but a single quote is on the main row and does not require a shift. The complex set of characters you are typing in is presumably related to your editor and has nothing really to do with the question as stated. If you are only concerned about keystrokes any decent editor will let you remap them so there is no reason to avoid # on that account. Alternately you could use a utility that works at the OS level to remap keys. On a PC [autohotkey](https://www.autohotkey.com/) is one such utility. – G. Grothendieck Jul 16 '21 at 01:54
  • 1
    Regardless whether it's [shift] or [alt] + [3] , it's two keystrokes. I'm asking you if there's a simple, built-in feature to do it in one keystroke. Clearly, there's none. Your solution completely missed the point. Not to mention your answer is aggressive and exasperated. "Exactly what is it that you don't understand ??? Is it not obvious that [...]" makes you look like a petulant child. I'm a new member, you are a very active one. Your solution does not allow for a "1-keystroke" comment, so it's worthless. I recommend a more humble and friendlier approach. After all, it's a help forum. – Madan Daniel Jul 16 '21 at 19:58
0

You can read about it in An Introduction to R, and there is an official statement regarding Comments:

Comments can be put almost anywhere, starting with a hash mark (‘#’), everything to the end of the line is a comment.

I think this is a built-in representation, and I don't think you can easily change it to other symbols.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81