同时编辑多行代码的最快方法
在 RStudio 源编辑器中跨多行代码执行相同操作的最佳方法是什么?
示例 1
假设我从文本文件复制一个列表并将其粘贴到 R 中(如下列表所示)。然后,我想在每个单词周围添加引号,并在每行添加逗号,这样我就可以创建一个向量。
Krista Hicks
Miriam Cummings
Ralph Lamb
Jaylene Gilbert
Jordon Sparks
Kenna Melton
预期输出
"Krista Hicks",
"Miriam Cummings",
"Ralph Lamb",
"Jaylene Gilbert",
"Jordon Sparks",
"Kenna Melton"
示例 2
如何在多行上添加缺少的括号。例如,如果我有一个 if
语句,那么如何在第 1 行和第 4 行上为 names
添加缺少的左括号。
if (!is.null(names pattern))) {
vec <- FALSE
replacement <- unname(pattern)
pattern[] <- names pattern)
}
预期输出
if (!is.null(names(pattern))) {
vec <- FALSE
replacement <- unname(pattern)
pattern[] <- names(pattern)
}
*注意:这些名称仅来自随机名称生成器。
What is the best way to do the same action across multiple lines of code in the RStudio source editor?
Example 1
Let's say that I copy a list from a text file and paste it into R (like the list below). Then, I want to add quotation marks around each word and add a comma to each line, so that I can make a vector.
Krista Hicks
Miriam Cummings
Ralph Lamb
Jaylene Gilbert
Jordon Sparks
Kenna Melton
Expected Output
"Krista Hicks",
"Miriam Cummings",
"Ralph Lamb",
"Jaylene Gilbert",
"Jordon Sparks",
"Kenna Melton"
Example 2
How can I add missing parentheses on multiple lines. For example, if I have an if
statement, then how can I add the missing opening parentheses for names
on line 1 and line 4.
if (!is.null(names pattern))) {
vec <- FALSE
replacement <- unname(pattern)
pattern[] <- names pattern)
}
Expected Output
if (!is.null(names(pattern))) {
vec <- FALSE
replacement <- unname(pattern)
pattern[] <- names(pattern)
}
*Note: These names are just from a random name generator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RStudio 支持多个光标,允许您同时写入和编辑多行。
示例 1
您只需在 Windows/Linux 上单击 Alt(或在 Mac 上单击 选项)并拖动鼠标即可进行选择,或者您也可以使用Alt+Shift创建从光标当前位置到单击位置的矩形选区。
示例 2
另一个多光标选项用于选择术语的所有匹配实例。因此,您可以选择
names
并按 Ctrl+Alt+Shift+M 。然后,您可以使用方向键移动光标删除空格并添加括号。RStudio has support for multiple cursors, which allows you to write and edit multiple lines at the same time.
Example 1
You can simply click Alt on Windows/Linux (or option on Mac) and drag your mouse to make your selection, or you can use Alt+Shift to create a rectangular selection from the current location of the cursor to a clicked position.
Example 2
Another multiple cursor option is for selecting all matching instances of a term. So, you can select
names
and press Ctrl+Alt+Shift+M. Then, you can use the arrow keys to move the cursors to delete the space and add in the parentheses.