replace

Choose a target, a replacement, files or folders, and whether you want to replace standard variables, function calls, argument names, formal names, or package names.

Installation

You can install the development version of replace like so:

remotes::install_github("moodymudskipper/replace")

Example

library(replace)
tmp <- tempfile(fileext = ".R")
code <- "
# foo
foo <- function(foo = foo) {
   foo(this)
   foo('foo')
   foo::foo(foo = foo$foo)
   lapply(foo, foo)
}
"
writeLines(code, tmp)

# by default we look at everything udner the R folder and replace variables and function calls
# below we do it every token type in succession

#tmp
replace_in_files("foo", "VAR", tmp, "var") # variable names and `$` elements
replace_in_files("foo", "FUN", tmp, "fun")
replace_in_files("foo", "ARG", tmp, "arg")
replace_in_files("foo", "FORMAL", tmp, "formal")
replace_in_files("foo", "PACKAGE", tmp, "package")
replace_in_files("# foo", "# COMMENT", tmp, "comment")
replace_in_files("'foo'", "'STRING'", tmp, "string")
replace_in_files("FUN(this)", "EXPRESSION", tmp, "expression")
cat(readLines(tmp), sep = "\n")
#> 
#> # COMMENT
#> VAR <- function(FORMAL = VAR) {
#>    EXPRESSION
#>    FUN('STRING')
#>    PACKAGE::FUN(ARG = VAR$VAR)
#>    lapply(VAR, VAR)
#> }