多行注释变通办法?

我(有点)已经知道这个问题的答案。 但我觉得这是在R用户名单上被频繁询问的问题,应该有一个很好的答案。 据我所知,在R中没有多行注释function。那么,有没有人有任何好的解决方法?

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

以前我用引号。 由于string支持换行符,所以运行R脚本

" Here's my multiline comment. " a <- 10 rocknroll.lm <- lm(blah blah blah) ... 

工作正常。 有没有人有更好的解决scheme?

这确实出现在邮件列表中相当有规律, 例如 ,看到这个最新的线程在r – 帮助 。 一致的答案通常是上面显示的那个:鉴于语言没有直接的支持,你必须要么

  • 与具有区域到注释命令的编辑工作,以及最先进的R编辑器
  • 使用前面提出的if (FALSE)结构,但是请注意,它仍然需要完整的parsing,因此必须在语法上是正确的

您可以在RStudio中轻松完成此操作:

select代码并点击CTR + SHIFT + C来评论/取消注释代码。

对于RStudio而言,我刚刚发现的一个简单的技巧就是使用#'创build一个自我扩展的注释部分(当你从这样的一行返回到新的行或者插入新的行到这个部分时它会自动注释)。

[更新]根据评论。

 # 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中。

使用IDE 有效的多行注释是“好东西”,大多数IDE或简单编辑器在简单的注释块中没有突出显示文本; 尽pipe一些作者花时间确保在这里string内parsing。 使用R,我们没有多行注释或这里string,但在RStudio中使用不可见的expression式提供了所有的好处。

只要在想要用于多行注释的部分中没有反引号,这里是string或者未执行的注释块,那么这可能是值得的。

 #### 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!" 

这里是图片

结构化评论

我可以想到两个select。 第一个select是使用允许阻止注释和取消注释的编辑器(例如Eclipse)。 第二种select是使用if语句。 但是这只会让你“评论”正确的R语法。 因此,一个好的编辑器是首选的解决方法。

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

如果觉得不可思议的话,任何语言都不会照顾到这一点。

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

 anything=" first comment line second comment line " 

除了通过安装RStudio来使用过多的注释方式来评论多行代码之外,还可以使用Notepad ++,因为它支持R的语法高亮显示

(select多行) – >编辑 – >注释/取消注释 – >切换块注释

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

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

我使用vim来编辑R脚本。

我们假设R脚本是test.R,分别在三行中包含“Line 1”,“Line 2”和“Line 3”。

我通过键入“vim test.R”在Vim的命令行打开test.R。 然后我去第一行我要注释掉,input“Control-V”,向下箭头到我要注释掉的最后一行,input一个大写字母I即插入“I”,input“#”,然后点击Escape键将“#”添加到我按箭头键select的每一行。 将文件保存在Vim中,然后键入“:wq”退出Vim。 应该在Rstudio中显示更改。

要删除Vim中的注释,从要删除的字符“#”的第一行开始,再次执行“Control-V”,然后向下箭头至要删除“#”的最后一行。 然后input“dd”。 “#”标志应该被删除。

Vim中test.R的变化反映在Rstudio之间有几秒钟的时间间隔。