如何替换脚本中多次出现的代码块?

发布于 2025-01-15 15:30:34 字数 192 浏览 1 评论 0原文

我正在使用 RStudio,我需要替换 R 脚本中的一段代码。该代码块在该脚本中出现了大约 30 次。

执行 CTRL+FReplace 似乎不是正确的方法。我猜这是因为输入的文本框有字数限制。

有没有一种有效的方法来解决这个问题?

I am using RStudio and I need to replace a block of code in my R script. This block of code appears around 30 times in that script.

Doing a CTRL+F and Replace does not appear to be the correct way of doing it. I guess that is because the text box for the input has a limit on the number of words.

Is there an efficient way of getting around this problem?

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

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

发布评论

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

评论(1

梓梦 2025-01-22 15:30:34

我可以在这里想到两个相当简单的解决方案(希望这两个之一有帮助)。

1.)查找/替换工具栏(与您尝试过的方式不同)。不要尝试让“查找”工具栏进行整个选择,而只需让查找工具栏在所有出现的地方创建多个光标,然后自己从那里操作文本(在“查找/替换”工具栏之外)。因此,首先,请确保您复制了想要复制到脚本中的代码块。然后,转到“查找/替换”工具栏,并在块中键入一个简短的代码片段,该代码片段仅对您想要的代码是唯一的(适合工具栏的简短代码片段,但也可以是在任何其他代码中都找不到的代码片段) 30 次出现之外的代码片段)。执行此操作并点击“全部”后,您会发现多个光标出现在您想要更改的所有位置。它被恰当地称为多光标选择(想想看,哈?!)。从那里,您几乎可以选择并突出显示周围的文本,就像任何其他单个光标/光标选择一样。无论您在一个光标组中选择什么,都将在所有其他光标组中选择完全相同的内容。因此,只需专注于让其中一个光标选择要替换的大文本块(我在想要选择的任何方向上使用命令+Shift+箭头键)。一旦选择了整个代码块,它也将被创建的所有其他多个光标选择。然后,您所要做的就是粘贴(command+v)您想要的代码,您应该在第一步中复制它。要直观地了解其工作原理(因为这是一个冗长的解释),您可以检查 this(滚动到“如何在 RStudio 中使用多个光标进行编辑”部分)。有一个更好的解释和一个 gif 示例,显示了我在此处概述的所有步骤!

请注意,在您的情况下,根据我对您问题的理解,由于替换的长度,您无法使用查找/替换工具栏,而在我的示例中,我们仅使用查找/替换工具栏在出现的地方获取多光标,然后执行我们可以通过单个光标选择执行的操作(选择一段文本,然后粘贴我们想要替换的内容)。

2.) 在计算机上打开 R 脚本文档,将其复制并粘贴到新的文本文档中,将其另存为 .txt 文件。

在 R Studio 中,导入 .txt 文件。执行简单的字符串替换(使用replace()、正则表达式或您喜欢的任何方式)。完成后,只需打印该字符串,从控制台复制它,然后将其粘贴到原始 R 脚本中(或新脚本,无论您喜欢哪种)。瞧!完毕!

第一个选项有更长的解释,因为实际操作需要更多解释,但在我看来,它要快得多,而且涉及的内容更少。 R Studio 中的多光标选择也是您工具箱中一项非常好的小技能!

我希望我能够帮助您,而不是浪费您的时间!祝你好运!

I can think of 2 fairly simple solutions here (hopefully one of the two is helpful).

1.) Find/Replace Toolbar (in a different way than it sounds like you tried). Instead of trying to get the Find toolbar to make the WHOLE selection, instead just get the find toolbar to create multiple cursors at all the occurences and then manipulate the text yourself from there (outside of the Find/Replace toolbar). So FIRST, make sure that you have the block of code that you WANT to be in your script copied. Then, go to the "Find/Replace" toolbar and type a short code snippet within the block that is unique to ONLY the code that you want (something short that will fit in the toolbar but also something that isn't found in any other piece of your code outside of the 30 occurrences). Once you do this, and hit "All", you'll notice that multiple cursors have appeared at all of the occurrences you will want to change. It's appropriately called multicursor selection (go figure, huh?!). From there, you can pretty much select and highlight surrounding text just like you would any other single cursor/cursor selection. Whatever you select in one cursor group will select the same exact thing in all the other ones. So just focus on getting one of the cursors to select the huge block of text you want to replace (I use command+shift+arrow keys in whatever direction I want to select). Once the entire code block is selected, it will also be selected by all the other multiple cursors created. Then, all you have to do is paste (command+v) the code that you want to be there which you should have copied in the very first step. For a visual of how this would work (since this was sort of a lengthy explanation), you can check this out (scroll to the "How to Edit With Multiple Cursors in RStudio" section). There's a better explanation and a gif example showing all of the steps I outlined here!

Note that while in your case, based on what I understood from your question, you weren't able to use the find/replace toolbar because of your replacements' length, while in my example, we're just using the find/replace toolbar to get multicursors at the occurrences, and then performing operations that we could do with a single cursor selection (selecting a piece of text and then pasting what we want to replace instead).

2.) Open your R script document on your computer, copy and paste it into a new text document, save it as a .txt file.

In R Studio, import the .txt file. Perform a simple string replacement (using replace(), a regex expression, or however you prefer to do it). Once done, just print the string, copy it from the console, and paste it into your original R script (or a new one, whichever you prefer). Voila! Done!

The first option had a longer explanation because the actual actions needed a little more explanation, but it's a lot faster and less involved in my opinion. Multicursor selection in R Studio is also just a really good little skill to have in your toolbox!

I hope that I was able to help and didn't just waste your time! Best of luck to you!

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