Multiline Comment Workarounds?

2019-01-10 05:26发布

I (sort of) already know the answer to this question. But I figured it is one that gets asked so frequently on the R Users list, that there should be one solid good answer. To the best of my knowledge there is no multiline comment functionality in R. So, does anyone have any good workarounds?

While quite a bit of work in R usually involves interactive sessions (which casts doubt on the need for multiline comments), there are times when I've had to send scripts to colleagues and classmates, much of which involves nontrivial blocks of code. And for people coming from other languages it is a fairly natural question.

In the past I've used quotes. Since strings support linebreaks, running an R script with

"
Here's my multiline comment.

"
a <- 10
rocknroll.lm <- lm(blah blah blah)
 ...

works fine. Does anyone have a better solution?

标签: r comments r-faq
10条回答
孤傲高冷的网名
2楼-- · 2019-01-10 05:45

I can think of two options. The first option is to use an editor that allows to block comment and uncomment (eg. Eclipse). The second option is to use an if statement. But that will only allow you to 'comment' correct R syntax. Hence a good editor is the prefered workaround.

if(FALSE){
     #everything in this case is not executed

}
查看更多
不美不萌又怎样
3楼-- · 2019-01-10 05:50

[Update] Based on comments.

# An empty function for Comments
Comment <- function(`@Comments`) {invisible()}

#### Comments ####
Comment( `

  # Put anything in here except back-ticks.

  api_idea <- function() {
    return TRUE
  }

  # Just to show api_idea isn't really there...
  print( api_idea )

`)
####

#### Code. ####
foo <- function() {
  print( "The above did not evaluate!")
}
foo()

[Original Answer]

Here's another way... check out the pic at the bottom. Cut and paste the code block into RStudio.

Multiline comments that make using an IDE more effective are a "Good Thing", most IDEs or simple editors don't have highlighting of text within simple commented -out blocks; though some authors have taken the time to ensure parsing within here-strings. With R we don't have multi-line comments or here-strings either, but using invisible expressions in RStudio gives all that goodness.

As long as there aren't any backticks in the section desired to be used for a multiline comments, here-strings, or non-executed comment blocks then this might be something worth-while.

#### Intro Notes & Comments ####
invisible( expression( `
{ <= put the brace here to reset the auto indenting...

  Base <- function()
  {      <^~~~~~~~~~~~~~~~ Use the function as a header and nesting marker for the comments
         that show up in the jump-menu.
         --->8---
  }

  External <- function()
  {
    If we used a function similar to:
      api_idea <- function() {

        some_api_example <- function( nested ) {
          stopifnot( some required check here )
        }

        print("Cut and paste this into RStudio to see the code-chunk quick-jump structure.")
        return converted object
      }

    #### Code. ####
    ^~~~~~~~~~~~~~~~~~~~~~~~~~ <= Notice that this comment section isnt in the jump menu!
                                  Putting an apostrophe in isn't causes RStudio to parse as text
                                  and needs to be matched prior to nested structure working again.
    api_idea2 <- function() {

    } # That isn't in the jump-menu, but the one below is...

    api_idea3 <- function() {

    }

  }

    # Just to show api_idea isn't really there...
    print( api_idea )
    }`) )
####

#### Code. ####
foo <- function() {
  print( "The above did not evaluate and cause an error!")
}

foo()

## [1] "The above did not evaluate and cause an error!"

And here's the pic...

Structured Comments

查看更多
仙女界的扛把子
4楼-- · 2019-01-10 05:54

If find it incredible that any language would not cater for this.

This is probably the cleanest workaround:

anything="
first comment line
second comment line
"
查看更多
姐就是有狂的资本
5楼-- · 2019-01-10 05:56

A neat trick for RStudio I've just discovered is to use #' as this creates an self-expanding comment section (when you return to new line from such a line or insert new lines into such a section it is automatically comment).

查看更多
一夜七次
6楼-- · 2019-01-10 05:57

Apart from using the overkilled way to comment multi-line codes just by installing RStudio, you can use Notepad++ as it supports the syntax highlighting of R

(Select multi-lines) -> Edit -> Comment/Uncomment -> Toggle Block Comment

Note that you need to save the code as a .R source first (highlighted in red)

Note that you need to save the code as a .R source first (highlighted in red)

查看更多
萌系小妹纸
7楼-- · 2019-01-10 06:00

You can do this easily in RStudio:

select the code and click CTR+SHIFT+C to comment/uncomment code.

查看更多
登录 后发表回答