An open API service indexing awesome lists of open source software.

https://github.com/moodymudskipper/replace

Replace Variable Names in R Scripts
https://github.com/moodymudskipper/replace

Last synced: 3 months ago
JSON representation

Replace Variable Names in R Scripts

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# 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:

``` r
remotes::install_github("moodymudskipper/replace")
```

## Example

```{r 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")
```