我(在某种程度上)已经知道这个问题的答案。但我认为这是一个在R用户列表上被问得如此频繁的问题,应该有一个可靠的好答案。据我所知,R中没有多行注释功能。那么,有没有人有什么好的解决办法呢?
虽然在R中有相当多的工作通常涉及交互式会话(这让人怀疑是否需要多行注释),但有时我不得不向同事和同学发送脚本,其中很多都涉及到非常重要的代码块。对于来自其他语言的人来说,这是一个相当自然的问题。
在过去,我使用过引号。由于字符串支持换行符,因此使用
"
Here's my multiline comment.
"
a <- 10
rocknroll.lm <- lm(blah blah blah)
...
工作正常。有人有更好的解决方案吗?
发布于 2009-08-05 13:09:37
这确实经常出现在邮件列表中,请参阅for example this recent thread on r-help。一致的答案通常是上面所示的:鉴于该语言没有直接支持,您必须选择
if (FALSE)
构造,但请注意,它仍然需要完整的解析,因此必须是语法正确的发布于 2013-06-04 17:42:33
我刚刚发现的RStudio的一个巧妙技巧是使用#'
,因为这会创建一个自我展开的注释部分(当您从这样的行返回到新行或向这样的部分插入新行时,它会自动进行注释)。
发布于 2012-11-22 09:31:05
根据评论进行更新。
# 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或简单编辑器都不会突出显示简单注释的-out块中的文本;尽管有些作者花时间确保在here字符串中进行解析。对于R,我们也没有多行注释或here字符串,但是在RStudio中使用不可见的表达式提供了所有这些好处。
只要部分中没有任何用于多行注释、here-strings或未执行的注释块的反标记,那么这可能是值得的。
#### 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!"
这是照片..。
https://stackoverflow.com/questions/1231195
复制相似问题