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
- Host: GitHub
- URL: https://github.com/moodymudskipper/replace
- Owner: moodymudskipper
- License: other
- Created: 2023-05-11T13:54:55.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-30T09:44:50.000Z (9 months ago)
- Last Synced: 2024-08-13T07:11:16.469Z (6 months ago)
- Language: R
- Size: 7.81 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - moodymudskipper/replace - Replace Variable Names in R Scripts (R)
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")
```