多行注释工作区? ?

我(在某种程度上)已经知道这个问题的答案了。但是我认为这是一个在 R 用户列表中被问得如此频繁的问题,应该有一个可靠的好答案。据我所知,R 中没有多行注释功能,那么,有人有什么好的解决办法吗?

尽管 R 中的大量工作通常涉及到交互式会话(这让人怀疑是否需要多行注释) ,但有时我不得不向同事和同学发送脚本,其中大部分涉及到大量代码块。对于来自其他语言的人来说,这是一个很自然的问题。

过去我使用过引号,因为字符串支持换行符,所以使用

"
Here's my multiline comment.


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

works fine. Does anyone have a better solution?

61399 次浏览

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


}

这确实经常出现在邮件列表中,请参阅 例如最近关于 r-help 的线程。一致的答案通常是上面显示的那个: 考虑到语言没有直接的支持,您也必须这样做

  • 使用具有“区域到注释”命令的编辑器,大多数高级 R 编辑器都具有这种功能
  • 使用前面建议的 if (FALSE)构造,但注意它仍然需要完整的解析,因此必须在语法上正确

如果发现任何语言都不能满足这种需求,那真是令人难以置信。

这可能是最干净的解决办法:

anything="
first comment line
second comment line
"

[更新]基于评论。

# 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()

[原始答案]

这是另一种方法... 检查底部的图片。剪切和粘贴到 RStudio 的代码块。

Multiline comments that make using an IDE 更多 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!"

这是照片。

Structured Comments

You can do this easily in 工作室:

选择代码并单击 CTR + SHIFT + C 注释/取消注释代码。

我刚刚发现的 RStudio 的一个巧妙的技巧是使用 #',因为它创建了一个自我扩展的注释部分(当你从这样的一行返回到新行或者在这样的一个部分中插入新行时,它会自动注释)。

除了通过安装 RStudio 来使用多行代码注释的方法,你还可以使用 记事本 + + ,因为它支持 R 的语法突显

(选择多行)-> 编辑-> 评论/取消评论-> 切换块评论

请注意,您需要首先将代码保存为.R 源(用红色突出显示)

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

我使用 vim 编辑 R 脚本。

假设 R 脚本是 test.R,在3个不同的行上包含“ Line 1”、“ Line 2”和“ Line 3”。

I open test.R on the command line with Vim by typing "vim test.R". 然后我转到第一行,输入“ Control-V”,向下箭头到最后一行,输入大写字母 I,即“ I”表示插入,输入“ #”,然后按下 Escape 键,通过向下箭头向我选择的每一行添加“ #”。在 Vim 中保存文件,然后键入“ : wq”退出 Vim。Rstudio 应该出现变化。

要删除 Vim 中的评论,从你想删除的字符“ #”顶部的第一行开始,再次执行“ Control-V”,然后向下箭头指向你想删除的最后一行“ #”。然后输入“ dd”。应该删除“ #”标志。

There's seconds-worth of lag time between when changes to test.R in Vim are reflected in Rstudio.

工作室中,一个简单的方法就是写注释,一旦你使用 CTRL + SHIFT + C 来注释你的代码行,然后使用 CTRL + SHIFT +/来重放你的注释到多行以便于阅读。

在 RStudio 中,你可以使用一个英镑符号,引用如下:

#' This is a comment

Now, every time you hit return you don't need to add the #', RStudio will automatically put that in for you.

顺便说一句,为了添加返回的参数和项目,为了标准化,如果在这些注释字符串中键入@符号,RStudio 将自动向您显示与这些注释参数相关联的代码列表:

#' @param tracker_df Dataframe of limit names and limits
#' @param invoice_data Dataframe of invoice data
#' @return return_list List of scores for each limit and rejected invoice rows

Now there is a workaround, by using package ARTofR or 横幅评论

例如:

enter image description here