R 中可以有多行注释吗?
我发现这个旧线程 (一年多前),这解释了为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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.
如果您愿意,您可以使用独立的字符串进行多行注释 - 我一直认为这比
if (FALSE) { }
块更漂亮。该字符串将被评估然后被丢弃,因此只要它不是函数中的最后一行,就不会发生任何事情。主要的限制是,当你注释掉一些内容时,你必须注意你的引号:如果你里面有一种引号,你就必须使用另一种引号;如果您在该块内有类似“带'撇号的字符串”之类的内容,那么这种方法就不是一个好主意。但仍然存在
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.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.
Eclipse + StatET 和 Rstudio 中的 CTRL+SHIFT+C。
CTRL+SHIFT+C in Eclipse + StatET and Rstudio.
阻止执行多行。然而,这些行在语法上仍然必须是正确的,即不能是正确意义上的注释。但对某些情况仍然有帮助。
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.
从 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 ;'.
将以下内容放入您的
~/.Rprofile
文件中:现在,您可以排除块,如下所示:
Put the following into your
~/.Rprofile
file:Now, you can exclude blocks like follows:
不幸的是,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.