5

I have several R scripts and my client just wants to change the colours of the graphs.

Is there any way to find and replace all at once instead of find&replace opening the scripts one by one?

I have tried a little tool called fnr with .txt files and it works, but it doesn't with .R files.

Bijan
  • 7,737
  • 18
  • 89
  • 149
user3507584
  • 3,246
  • 5
  • 42
  • 66
  • If the tool works why not change the .R extension to .txt? – user20650 Aug 28 '14 at 12:06
  • Is it possible? can you use txt straight to R as scripts? Just changing .R for .txt? – user3507584 Aug 28 '14 at 12:09
  • Should be ok - they are just text files. But you can change them to .txt run your tool then change them back to .R. – user20650 Aug 28 '14 at 12:12
  • On linux you could put all the .R files in a folder then `cd "path/to/folder"` then rename the R files as.txt with `rename 's/.R$/.txt/' *.R` , run your tool then chnage the names back. Although you could replace strings more simply (if you are using linux) - quite a few examples on SO – user20650 Aug 28 '14 at 12:16
  • I'm running Windows and RStudio. Using the trick of just substitute .txt for .R works but when you open the .txt script in RStudio there is no highlighting/formatting of the code functions... – user3507584 Aug 28 '14 at 12:34
  • `Find And Replace` is a nice app that will do the job for you. http://sourceforge.net/projects/findandreplace/ – hplieninger Aug 28 '14 at 12:36
  • Find And Replace didn't work either... I will go manual this time, hopefully someone will find the answer in the future. – user3507584 Aug 28 '14 at 12:45
  • 1
    in "Find And Replace", did you untick the "exclude binary files..." option in the settings menu? – hplieninger Aug 28 '14 at 12:47
  • 2
    Notepad++ offers the ability for find and replace in all open documents and find and replace in files (within a directory, and optionally its sub-directories). – James Aug 28 '14 at 13:15
  • 1
    You can still use `rename` in Windows. ie `rename *.R *.txt` to change all .R files in the current directory from .R to .txt. By changing the extension you do lose some of the functionality of Rstudio but why des this matter - you are just running fnr.exe to find and replace. You can change the files back to .R after. That said, others have offered better methods to do this. – user20650 Aug 28 '14 at 13:52

4 Answers4

9

The xfun R package has a few functions to do exactly this (gsub_file(), gsub_dir(), etc.)

For example, if all your R scripts are in a sub-folder in your working directory, you can simply write:

library(xfun)

gsub_dir(dir = "Scripts", pattern = "color = 'green'", replacement = "color = 'blue'")
bschneidr
  • 6,014
  • 1
  • 37
  • 52
2

Here's an approach using R, where you create a function that finds-and-replaces text in a file and you apply this function to all of the R scripts in your directory.

In the example below, this changes the code color = 'green' in any R script to color = 'blue'.

# Define function to find-and-replace text in a single file
  file_find_replace <- function(filepath, pattern, replacement) {
    file_contents <- readLines(filepath)
    updated_contents <- gsub(x = file_contents, pattern = pattern, replacement = replacement)
    cat(updated_contents, file = filepath, sep = "\n")
  }

# Apply the function to each of the R scripts in the directory
  my_r_scripts <- list.files(path = my_dir, pattern = "(r|R)$")

  for (r_script in my_r_scripts ) {
    file_find_replace(r_script,
                      "color = 'green'",
                      "color = 'blue'")
  }
bschneidr
  • 6,014
  • 1
  • 37
  • 52
1

There's also regexxer: https://github.com/GNOME/regexxer for Linux. RStudio developers seem to be working on a solution also, as documented here (which contains information on a few other approaches): https://github.com/rstudio/rstudio/issues/2066

RobinLovelace
  • 4,799
  • 6
  • 29
  • 40
0

I found a very useful tool called grepWin. It has plenty of options for search and replace. It search subfolders, different types of matches, date filters, etc...

You can download it at https://tools.stefankueng.com/grepWin.html

enter image description here

user3507584
  • 3,246
  • 5
  • 42
  • 66
  • Notepad++ is the easiest method. You can drag the entire folder or files into notepad and go to `Search > Replace` or `Ctrl+H` and be sure to select `Replace All in All Opened Documents` – Bijan Nov 04 '14 at 22:42