R 中可以有多行注释吗?

发布于 2024-10-01 11:10:29 字数 283 浏览 1 评论 0原文

我发现这个旧线程 (一年多前),这解释了为什么 R 不支持多行注释(例如 PHP 的 /* comment */ )。

我想知道这个问题在过去的一年里是否得到了解决,或者是否还有其他替代方案? (例如,在带有npptor的notepad++中,您可以标记一堆行,然后按ctrl+q将它们全部标记为注释,其他IDE是否有类似的解决方案?)

I found this old thread (from over a year ago), which explains how come R doesn't support a multi-line comments (like /* comment */ of PHP, for example).

I am wondering if this has been resolved in the past year, or if there are other alternatives? (For example, in notepad++ with npptor, you can mark a bunch of lines and press ctrl+q to mark them all as comments, are there similar solutions for other IDE's ?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

望她远 2024-10-08 11:10:29

R Studio(和 Eclipse + StatET):

在 Windows 上,突出显示文本并使用 CTRL+SHIFT+C 注释多行。

对于 macOS,请使用 command+SHIFT+C

R Studio (and Eclipse + StatET):

On Windows, highlight the text and use CTRL+SHIFT+C to comment multiple lines.

For macOS, use command+SHIFT+C.

心碎的声音 2024-10-08 11:10:29

如果您愿意,您可以使用独立的字符串进行多行注释 - 我一直认为这比 if (FALSE) { } 块更漂亮。该字符串将被评估然后被丢弃,因此只要它不是函数中的最后一行,就不会发生任何事情。

"This function takes a value x, and does things and returns things that
 take several lines to explain"
doEverythingOften <- function(x) {
     # Non! Comment it out! We'll just do it once for now.
     "if (x %in% 1:9) {
          doTenEverythings()
     }"
     doEverythingOnce()
     ...
     return(list(
         everythingDone = TRUE, 
         howOftenDone = 1
     ))
}

主要的限制是,当你注释掉一些内容时,你必须注意你的引号:如果你里面有一种引号,你就必须使用另一种引号;如果您在该块内有类似“带'撇号的字符串”之类的内容,那么这种方法就不是一个好主意。但仍然存在 if (FALSE) 块。

两种方法都有的另一个限制是,您只能在表达式在语法上有效的地方使用此类块 - 例如,不能注释掉列表的部分内容。

关于在哪个 IDE 中做什么:我是 Vim 用户,我发现
NERD Commenter 一个非常优秀的工具,用于快速注释或取消注释多行。非常用户友好,文档非常齐全。

最后,在 R 提示符下(至少在 Linux 下),有可爱的 Alt-Shift-# 来注释当前行。如果您正在写一行行,然后意识到您需要先进行准备步骤,那么将一行行“暂停”是非常好的。

You can, if you want, use standalone strings for multi-line comments — I've always thought that prettier than if (FALSE) { } blocks. The string will get evaluated and then discarded, so as long as it's not the last line in a function nothing will happen.

"This function takes a value x, and does things and returns things that
 take several lines to explain"
doEverythingOften <- function(x) {
     # Non! Comment it out! We'll just do it once for now.
     "if (x %in% 1:9) {
          doTenEverythings()
     }"
     doEverythingOnce()
     ...
     return(list(
         everythingDone = TRUE, 
         howOftenDone = 1
     ))
}

The main limitation is that when you're commenting stuff out, you've got to watch your quotation marks: if you've got one kind inside, you'll have to use the other kind for the comment; and if you've got something like "strings with 'postrophes" inside that block, then there's no way this method is a good idea. But then there's still the if (FALSE) block.

The other limitation, one that both methods have, is that you can only use such blocks in places where an expression would be syntactically valid - no commenting out parts of lists, say.

Regarding what do in which IDE: I'm a Vim user, and I find
NERD Commenter an utterly excellent tool for quickly commenting or uncommenting multiple lines. Very user-friendly, very well-documented.

Lastly, at the R prompt (at least under Linux), there's the lovely Alt-Shift-# to comment the current line. Very nice to put a line 'on hold', if you're working on a one-liner and then realise you need a prep step first.

月依秋水 2024-10-08 11:10:29

Eclipse + StatET 和 Rstudio 中的 CTRL+SHIFT+C

CTRL+SHIFT+C in Eclipse + StatET and Rstudio.

唯憾梦倾城 2024-10-08 11:10:29
if(FALSE) {
...
}

阻止执行多行。然而,这些行在语法上仍然必须是正确的,即不能是正确意义上的注释。但对某些情况仍然有帮助。

if(FALSE) {
...
}

precludes multiple lines from being executed. However, these lines still have to be syntactically correct, i.e., can't be comments in the proper sense. Still helpful for some cases though.

无声无音无过去 2024-10-08 11:10:29

从 2.12 版开始,R 中没有多行注释,并且不太可能更改。在大多数环境中,您可以通过突出显示和切换注释来注释块。在 emacs 中,这是“Mx;”。

No multi-line comments in R as of version 2.12 and unlikely to change. In most environments, you can comment blocks by highlighting and toggle-comment. In emacs, this is 'M-x ;'.

じее 2024-10-08 11:10:29

将以下内容放入您的 ~/.Rprofile 文件中:

exclude <-  function(blah) {
    "excluded block"
}

现在,您可以排除块,如下所示:

stuffiwant

exclude({
    stuffidontwant
    morestuffidontwant
})

Put the following into your ~/.Rprofile file:

exclude <-  function(blah) {
    "excluded block"
}

Now, you can exclude blocks like follows:

stuffiwant

exclude({
    stuffidontwant
    morestuffidontwant
})
北音执念 2024-10-08 11:10:29

不幸的是,R 中仍然没有多行注释。

如果您的文本编辑器支持列模式,那么可以使用它一次添加一堆#。如果您使用 UltraEdit,Alt+c 将使您进入列模式。

Unfortunately, there is still no multi-line commenting in R.

If your text editor supports column-mode, then use it to add a bunch of #s at once. If you use UltraEdit, Alt+c will put you in column mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文