How can I replace a block of code that appears several times in a Script?

Viewed 439

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?

1 Answers

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!

Related