Package: The R Code Optimizer
Make your R code run faster!
rco
analyzes your code and applies different optimization strategies that return an R code that runs faster.
The rco
project, from its start to version 1.0.0, was made possible by a Google Summer of Code 2019 project.
Thanks to the kind mentorship of Dr. Yihui Xie and Dr. Nicolás Wolovick.
library("rco")
site_url <- "https://github.com/jcrodriguez1989/rco/blob/master/"
Install the current released version of rco
from CRAN:
install.packages("rco")
Or install the development version from GitHub:
if (!require("remotes")) {
install.packages("remotes")
}
remotes::install_github("jcrodriguez1989/rco", dependencies = TRUE)
rco
can be used in three ways:
Using the RStudio Addins
Optimize active file
: Optimizes the file currently open in RStudio. It will apply the optimizers present in all_optimizers
.
Optimize selection
: Optimizes the code currently highlighted in the RStudio Source Pane. It will apply the optimizers present in all_optimizers
.
Using the shiny
GUIs
rco_gui("code_optimizer")
opens a shiny
interface in a browser. This GUI allows to easily optimize chunks of code.
rco_gui("pkg_optimizer")
opens a shiny
interface in a browser. This GUI allows to easily optimize R packages that are hosted at CRAN or GitHub.
Using the R functions
.R
code filesoptimize_files(c("file_to_optimize_1.R", "file_to_optimize_2.R"))
code <- paste(
"code_to_optimize <- 8 ^ 8 * 1918",
"cto <- code_to_optimize * 2",
sep = "\n"
)
optimize_text(code)
.R
code files into a folderoptimize_folder("~/myfolder_to_optimize", recursive = FALSE)
Suppose we have the following code:
code <- paste(
"# I want to know my age in seconds!",
"years_old <- 29",
"days_old <- 365 * years_old # leap years don't exist",
"hours_old <- 24 * days_old",
"seconds_old <- 60 * 60 * hours_old",
"",
"if (seconds_old > 10e6) {",
' print("Whoa! More than a million seconds old, what a wise man!")',
"} else {",
' print("Meh!")',
"}",
sep = "\n"
)
We can automatically optimize it by doing:
opt_code <- optimize_text(code, iterations = 1)
cat(opt_code)
After one optimization pass we can see that it has only propagated the years_old
variable. Another pass:
opt_code <- optimize_text(opt_code, iterations = 1)
cat(opt_code)
Now, it has folded the days_old
variable, and then propagated it. Another pass:
opt_code <- optimize_text(opt_code, iterations = 1)
cat(opt_code)
It has folded the hours_old
variable, and then propagated it. Another pass:
opt_code <- optimize_text(opt_code, iterations = 1)
cat(opt_code)
It has folded the seconds_old
variable, and then propagated it into the if
condition. Another pass:
opt_code <- optimize_text(opt_code, iterations = 1)
cat(opt_code)
Now, it has folded the if
condition, and as it was TRUE
it just kept its body, as testing the condition or the else
clause were dead code.
So, optimize_text
function has automatically detected constant variables, constant foldable operations, and dead code. And returned an optimized R code.
rco
is an open source package, and the contributions to the development of the library are more than welcome. Please see our [CONTRIBUTING.md](r paste0(site_url, ".github/CONTRIBUTING.md")
) file and “Contributing an Optimizer” article for detailed guidelines of how to contribute.
Please note that the ‘rco’ project is released with a [Contributor Code of Conduct](r paste0(site_url, "CODE_OF_CONDUCT.md")
).
By contributing to this project, you agree to abide by its terms.