提问人:HamiltonUlmer 提问时间:8/5/2009 最后编辑:Omar WagihHamiltonUlmer 更新时间:10/5/2023 访问量:66733
多行注释解决方法?
Multiline Comment Workarounds?
问:
我(有点)已经知道这个问题的答案了。但我认为这是一个在 R 用户列表中经常被问到的问题,应该有一个可靠的好答案。据我所知,R 中没有多行注释功能。那么,有人有什么好的解决方法吗?
虽然 R 中的相当多的工作通常涉及交互式会话(这让人怀疑是否需要多行注释),但有时我不得不将脚本发送给同事和同学,其中大部分涉及重要的代码块。对于来自其他语言的人来说,这是一个相当自然的问题。
过去我使用过引号。由于字符串支持换行符,因此使用
"
Here's my multiline comment.
"
a <- 10
rocknroll.lm <- lm(blah blah blah)
...
工作正常。有人有更好的解决方案吗?
答:
我能想到两个选择。第一种选择是使用允许阻止评论和取消评论的编辑器(例如。日食)。第二种选择是使用 if 语句。但这只允许你“注释”正确的 R 语法。因此,一个好的编辑器是首选的解决方法。
if(FALSE){
#everything in this case is not executed
}
评论
这确实经常出现在邮件列表中,例如,请参阅r-help上最近的这个帖子。共识答案通常是如上所示的:鉴于该语言没有直接支持,您必须
- 使用具有区域到注释命令的编辑器,大多数高级 R 编辑器都可以这样做
- 使用前面建议的构造,但请注意,它仍然需要完整的解析,因此在语法上必须正确
if (FALSE)
评论
大多数编辑器都采用某种快捷方式来注释掉代码块。默认编辑器使用命令或控件之类的东西,以及单引号来注释掉选定的代码行。在 RStudio 中,它是 或 +。签入您的编辑器。CommandControl/
它仍然逐行注释,但它们也取消注释选定的行。对于 Mac RGUI,它是命令选项(我想象 windows 是控制选项)。对于 Rstudio 来说,它只是或 + + 再次。CommandControlShiftC
随着编辑器的更新,这些快捷方式可能会随着时间的推移而改变,并且不同的软件成为最受欢迎的 R 编辑器。您必须查找它以查找您拥有的任何软件。
评论
C-x r t
"## "
M-x comment-region
C-x r
一种块注释使用 if 语句:
if(FALSE) {
all your code
}
它有效,但我几乎总是使用我的编辑器(RStudio、Kate、Kwrite)的块注释选项。
将其包装在一个未使用的函数中:
.f = function() {
## unwanted code here:
}
我使用 RStudio 或 Emacs,并且始终使用可用于评论区域的编辑器快捷方式。如果这是不可能的,那么你可以使用 Paul 的答案,但这只有在你的代码在语法上正确时才有效。
这是我想出的另一种肮脏的方法,将其包裹起来并删除结果。它确实将注释存储在内存中一小段时间,因此它可能不适用于非常大的注释。最好的办法是把标志放在每一行的前面(可能使用编辑器快捷方式)。scan()
#
foo <- scan(what="character")
These are comments
These are still comments
Can also be code:
x <- 1:10
One line must be blank
rm(foo)
评论
"#" <- function() invisible(scan(what = character()))
"#"()
#
我在 talkstats.com 帖子 94、101 和 103 中处理了这个问题:分享你的代码。正如其他人所说,Rstudio 可能是更好的选择。我将这些函数存储在我的 .Rprofile,并实际使用它们来快速自动阻止代码行。
没有你希望的那么好,但可能是一种方法。
如果觉得难以置信,任何语言都无法满足这一点。
这可能是最干净的解决方法:
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 中。
使 IDE 使用更有效的多行注释是一件“好事”,大多数 IDE 或简单的编辑器在简单的注释掉块中没有突出显示文本;尽管一些作者花时间确保在 here-strings 中解析。使用 R,我们也没有多行注释或 here-strings,但在 RStudio 中使用不可见的表达式可以带来所有这些好处。
只要希望用于多行注释、here-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!"
这是图片......
评论
comment=function(z){invisible(expression(z))}
Comments<-function(`@Comments`)rm(`@Comments`)
Comments=function(x){}
x
Comment <- function(`@Comments`) {invisible()}
variable names are limited to 10000 bytes
可以在 RStudio 中轻松执行此操作:
选择代码,然后单击“++”以注释/取消注释代码。CTRSHIFTC
我刚刚发现的 RStudio 的一个巧妙技巧是使用它,因为它会创建一个自扩展的注释部分(当您从这样的行返回到新行或将新行插入到这样的部分时,它会自动注释)。#'
评论
除了通过安装 RStudio 使用矫枉过正的方式注释多行代码外,您还可以使用 Notepad++,因为它支持 R 的语法突出显示
(选择多行) -> 编辑 -> 注释/取消注释 -> 切换块注释
请注意,您需要将代码另存为 .R 源优先(以红色突出显示)
我使用 vim 来编辑 R 脚本。
假设 R 脚本是 test。R,在 3 个单独的行上包含“第 1 行”、“第 2 行”和“第 3 行”。
我打开测试。R 在命令行中使用 Vim,键入“vim test.R". 然后我转到我要注释掉的第一行,键入“Control-V”,向下箭头到我要注释掉的最后一行,键入大写字母 I,即插入的“I”,键入“#”,然后按 Escape 键将“#”添加到我通过向下箭头选择的每一行。将文件保存在 Vim 中,然后键入 “:wq” 退出 Vim。更改应显示在 Rstudio 中。
要删除 Vim 中的注释,请从要删除的字符“#”顶部的第一行开始,再次执行“Control-V”,然后向下箭头到要删除“#”的最后一行。然后键入“dd”。应删除“#”符号。
在更改测试之间有几秒钟的延迟时间。Vim 中的 R 反映在 Rstudio 中。
在 RStudio 中,一个简单的方法是编写注释,一旦使用 CTRL + Shift + C 注释代码行,然后使用 CTRL + SHIFT + / 将注释重排到多行上,以便于阅读。
在 RStudio 中,可以使用井号和引号,如下所示:
#' This is a comment
现在,每次点击回车键时,您都不需要添加 #',RStudio 会自动为您添加 #'。
顺便说一句,为了添加返回的参数和项,如果在这些注释字符串中键入 @ 符号,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
评论
#'
@
现在有一种解决方法,即使用包 ARTofR 或 bannerCommenter
示例如下:
评论