Package 'replace'

Title: Replace Variable Names in R Scripts
Description: 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.
Authors: Antoine Fabri [aut, cre]
Maintainer: Antoine Fabri <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9000
Built: 2026-05-28 09:05:13 UTC
Source: https://github.com/moodymudskipper/replace

Help Index


Replace occurence of variable in files

Description

Use to replace code in a collection of files, keeping control on the type of replacement.

Usage

replace_in_files(
  target,
  replacement,
  paths = "R",
  what = c("var", "fun"),
  regex = FALSE,
  ...
)

Arguments

target

a string

replacement

a string

paths

a vector of paths or folders, if the latter all R scripts (".R", ".r") are considered

what

one or several of "var", "fun", "arg", "formal", "package", "comment", "expression". They are mapped to token types found in the output of getParseData(). See details.

regex

a boolean. Whether to use regular expressions for detection and replacement.

  • "var" : standard variable names target, includes element names foo$target

  • "fun" : function calls target() (if called through lapply etc a function will be matched with "var", not "fun")

  • "arg" : Function argument name foo(target = bar)

  • "formal" : a function's formal definition function(target) {} or ⁠function(target = ) {}⁠

  • "package" : target::foo or target:::foo

  • "comment" : comments, usually more useful with regex = TRUE

  • "string" : strings like "target"

  • "expression" : complex calls

For comments the "#" should be included in target and in the replacement and for strings, the quotes should be included in target (unless we use regex = TRUE).