| Title: | Tools for Refactoring Code |
|---|---|
| Description: | Tools for refactoring code. |
| Authors: | Antoine Fabri [aut, cre] |
| Maintainer: | Antoine Fabri <[email protected]> |
| License: | GPL-3 |
| Version: | 0.0.0.9000 |
| Built: | 2026-03-05 09:23:42 UTC |
| Source: | https://github.com/moodymudskipper/refactor |
This identifies files that contain non syntactic code, including files that have an R extension despite not being an R script.
check_files_parse(path = ".", recursive = TRUE)check_files_parse(path = ".", recursive = TRUE)
path |
A string. The path to a file or the folder to explore By default explores the working directory. |
recursive |
A boolean. Passed to |
Returns the path invisibly, called for side effects.
This is a wrapper around dupree::dupree(). It analyses provided files looking for
similar code. It is a bit slow so it can be impractical on big projects to run
it with default (all files contained in working directory recursively), in
this case it is wiser to run it on one or more specific paths or/and
or to filter the files using the pattern argument.
detect_similar_code(paths = ".", recursive = TRUE, pattern = NULL)detect_similar_code(paths = ".", recursive = TRUE, pattern = NULL)
paths |
Paths to scripts or folders containing scripts |
recursive |
Whether folders should be explorer recursively |
pattern |
A regular expression used to filter files |
The paths argument invisibly. Called for side effects
Scans the code and finds all namespaced call of the form pkg::fun and returns
a vector of unique package names.
fetch_namespace_names(path = ".", recursive = TRUE)fetch_namespace_names(path = ".", recursive = TRUE)
path |
A string. The path to a file or the folder to explore By default explores the working directory. |
recursive |
A boolean. Passed to |
A character vector of package names
This will show false positives, but guarantees that we don't miss any instance.
find_pkg_funs(pkg, path = ".", recursive = TRUE, exclude = NULL)find_pkg_funs(pkg, path = ".", recursive = TRUE, exclude = NULL)
pkg |
A string. The name of the target package |
path |
A string. The path to a file or the folder to explore By default explores the working directory. |
recursive |
A boolean. Passed to |
exclude |
A character vector of function names to dismiss. |
Returns its input invisibly, called for side effects
Identify scripts who contain both function definitions and other object definitions.
identify_hybrid_scripts(path = ".", recursive = TRUE)identify_hybrid_scripts(path = ".", recursive = TRUE)
path |
A string. The path to a file or the folder to explore By default explores the working directory. |
recursive |
A boolean. Passed to |
Returns the path invisibly, called for side effects.
These operators are used to refactor code and differ in the difference of behavior they allow between refactored and original code.
original %refactor% refactored original %refactor_chunk% refactored original %refactor_value% refactored original %refactor_chunk_and_value% refactored original %refactor_chunk_efficiently% refactored original %refactor_value_efficiently% refactored original %refactor_chunk_and_value_efficiently% refactored original %ignore_original% refactored original %ignore_refactored% refactoredoriginal %refactor% refactored original %refactor_chunk% refactored original %refactor_value% refactored original %refactor_chunk_and_value% refactored original %refactor_chunk_efficiently% refactored original %refactor_value_efficiently% refactored original %refactor_chunk_and_value_efficiently% refactored original %ignore_original% refactored original %ignore_refactored% refactored
original |
original expression |
refactored |
refactored expression |
Both original and refactored expressions are run. By default the function will fail if
the outputs are different. %ignore_original% and %ignore_refactored% do as
heir names suggest.
Options can be set to alter the behavior of %refactor%:
if refactor.value is TRUE (the default), the sameness of the outputs of
original and refactored is tested
if refactor.env is TRUE (default is FALSE), the sameness of the modifications
to the local environment made by original and refactored is tested
if refactor.time is TRUE (default is FALSE), the improved execution speed of
the refactored solution is tested
if refactor.waldo is TRUE (the default), the waldo::compare will be used
to compare objects or environments in case of failure. 'waldo' is sometimes
slow and if we set this option to FALSE, dplyr::all_equal() would be used instead.
%refactor_*% functions are variants that are not affected by options other than
refactor.waldo:
%refactor_chunk% behaves like %refactor% with options(refactor.value = FALSE, refactor.env = TRUE, refactor.time = FALSE),
it's convenient to refactor chunks of code that modify the local environment.
%refactor_value% behaves like %refactor% with options(refactor.value = TRUE, refactor.env = FALSE, refactor.time = FALSE),
it's convenient to refactor the body of a function that returns a useful value.
%refactor_chunk_and_value% behaves like %refactor% with options(refactor.value = TRUE, refactor.env = TRUE, refactor.time = FALSE),
it's convenient to refactor the body of a function that returns a closure.
%refactor_chunk_efficiently%, %refactor_value_efficiently% and %refactor_chunk_and_value_efficiently% are variants of the above
which also check the improved execution speed of the refactored solution
2 additional functions are used to avoid akward commenting of code, when the original and refactored code have different behaviors.
%ignore_original% and %ignore_refactored% are useful when original and
refactored code give different results (possibly because one of them is wrong)
and we want to keep both codes around without commenting.
This opens up an untitled script in RStudio containing calls to lintr::lint()
or lintr::lint_dir() with various linters, sorted by category and rough
order of importance.
use_lintr_template_on_file(path = NULL) use_lintr_template_on_dir(path = NULL)use_lintr_template_on_file(path = NULL) use_lintr_template_on_dir(path = NULL)
path |
Path to a R script or a directory. By default |
Returns NULL invisibly. Called for side effects.
Wrapper around fetch_namespace_names() that opens a new RStudio source editot
tab with code to be pasted at the top of the main script to enumerate required
packages and test if they are installed.
use_namespace_check()use_namespace_check()
Returns NULL invisibly, called for side effects.