Madan Daniel
Madan Daniel

Reputation: 27

Is there a way to use a different character than [#] to comment your code?

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

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

Is it possible?

Upvotes: 0

Views: 206

Answers (3)

MrFlick
MrFlick

Reputation: 206197

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).

Upvotes: 3

ThomasIsCoding
ThomasIsCoding

Reputation: 101337

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.

Upvotes: 0

G. Grothendieck
G. Grothendieck

Reputation: 269556

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
   

Upvotes: 2

Related Questions