MyNixOS website logo
Description

The R Code Optimizer.

Automatically apply different strategies to optimize R code. 'rco' functions take R code as input, and returns R code as output.

rco - The R Code Optimizer

CRANstatus Lifecycle:stable Travis buildstatus AppVeyor buildstatus Coveragestatus

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.

Installation

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)

Usage

rco can be used in three ways:

  • Using the RStudio Addins

    1. Optimize active file: Optimizes the file currently open in RStudio. It will apply the optimizers present in all_optimizers.

    2. Optimize selection: Optimizes the code currently highlited in the RStudio Source Pane. It will apply the optimizers present in all_optimizers.

  • Using the shiny GUIs

    1. rco_gui("code_optimizer") opens a shiny interface in a browser. This GUI allows to easily optimize chunks of code.

    2. 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

    1. Optimize some .R code files
    optimize_files(c("file_to_optimize_1.R", "file_to_optimize_2.R"))
    
    1. Optimize some code in a character vector
    code <- paste(
      "code_to_optimize <- 8 ^ 8 * 1918",
      "cto <- code_to_optimize * 2",
      sep = "\n"
    )
    optimize_text(code)
    
    1. Optimize all .R code files into a folder
    optimize_folder("~/myfolder_to_optimize", recursive = FALSE)
    

Example

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)
## Optimization number 1

## # I want to know my age in seconds!
## years_old <- 29
## days_old <- 365 * 29 # leap years don't exist
## hours_old <- 24 * days_old
## seconds_old <- 3600 * hours_old
## 
## if (seconds_old > 10e6) {
##   print("Whoa! More than a million seconds old, what a wise man!")
## } else {
##   print("Meh!")
## }

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)
## Optimization number 1

## # I want to know my age in seconds!
## years_old <- 29
## days_old <- 10585 # leap years don't exist
## hours_old <- 24 * 10585
## seconds_old <- 3600 * hours_old
## 
## if (seconds_old > 10e6) {
##   print("Whoa! More than a million seconds old, what a wise man!")
## } else {
##   print("Meh!")
## }

Now, it has folded the days_old variable, and then propagated it. Another pass:

opt_code <- optimize_text(opt_code, iterations = 1)
## Optimization number 1

## # I want to know my age in seconds!
## years_old <- 29
## days_old <- 10585 # leap years don't exist
## hours_old <- 254040
## seconds_old <- 3600 * 254040
## 
## if (seconds_old > 10e6) {
##   print("Whoa! More than a million seconds old, what a wise man!")
## } else {
##   print("Meh!")
## }

It has folded the hours_old variable, and then propagated it. Another pass:

opt_code <- optimize_text(opt_code, iterations = 1)
## Optimization number 1

## # I want to know my age in seconds!
## years_old <- 29
## days_old <- 10585 # leap years don't exist
## hours_old <- 254040
## seconds_old <- 914544000
## 
## if (914544000 > 10e6) {
##   print("Whoa! More than a million seconds old, what a wise man!")
## } else {
##   print("Meh!")
## }

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)
## Optimization number 1

## # I want to know my age in seconds!
## years_old <- 29
## days_old <- 10585 # leap years don't exist
## hours_old <- 254040
## seconds_old <- 914544000
## 
## print("Whoa! More than a million seconds old, what a wise man!")

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.

Guidelines for contributing

rco is an open source package, and the contributions to the development of the library are more than welcome. Please see our CONTRIBUTING.md file and “Contributing an Optimizer” article for detailed guidelines of how to contribute.

Code of Conduct

Please note that the ‘rco’ project is released with a Contributor Code of Conduct.

By contributing to this project, you agree to abide by its terms.

Metadata

Version

1.0.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows