IOT>> rco>> 返回
项目作者: jcrodriguez1989

项目描述 :
Package: The R Code Optimizer
高级语言: R
项目地址: git://github.com/jcrodriguez1989/rco.git
创建时间: 2019-03-28T17:47:31Z
项目社区:https://github.com/jcrodriguez1989/rco

开源协议:

下载


rco - The R Code Optimizer

CRAN
status
Lifecycle:
stable
Travis build
status
AppVeyor build
status
Coverage
status

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:

  1. install.packages("rco")

Or install the development version from GitHub:

  1. if (!require("remotes")) {
  2. install.packages("remotes")
  3. }
  4. 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 highlighted 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
    1. optimize_files(c("file_to_optimize_1.R", "file_to_optimize_2.R"))
    1. Optimize some code in a character vector
    1. code <- paste(
    2. "code_to_optimize <- 8 ^ 8 * 1918",
    3. "cto <- code_to_optimize * 2",
    4. sep = "\n"
    5. )
    6. optimize_text(code)
    1. Optimize all .R code files into a folder
    1. optimize_folder("~/myfolder_to_optimize", recursive = FALSE)

Example

Suppose we have the following code:

  1. code <- paste(
  2. "# I want to know my age in seconds!",
  3. "years_old <- 29",
  4. "days_old <- 365 * years_old # leap years don't exist",
  5. "hours_old <- 24 * days_old",
  6. "seconds_old <- 60 * 60 * hours_old",
  7. "",
  8. "if (seconds_old > 10e6) {",
  9. ' print("Whoa! More than a million seconds old, what a wise man!")',
  10. "} else {",
  11. ' print("Meh!")',
  12. "}",
  13. sep = "\n"
  14. )

We can automatically optimize it by doing:

  1. opt_code <- optimize_text(code, iterations = 1)
  1. ## Optimization number 1
  2. ## # I want to know my age in seconds!
  3. ## years_old <- 29
  4. ## days_old <- 365 * 29 # leap years don't exist
  5. ## hours_old <- 24 * days_old
  6. ## seconds_old <- 3600 * hours_old
  7. ##
  8. ## if (seconds_old > 10e6) {
  9. ## print("Whoa! More than a million seconds old, what a wise man!")
  10. ## } else {
  11. ## print("Meh!")
  12. ## }

After one optimization pass we can see that it has only propagated the
years_old variable. Another pass:

  1. opt_code <- optimize_text(opt_code, iterations = 1)
  1. ## Optimization number 1
  2. ## # I want to know my age in seconds!
  3. ## years_old <- 29
  4. ## days_old <- 10585 # leap years don't exist
  5. ## hours_old <- 24 * 10585
  6. ## seconds_old <- 3600 * hours_old
  7. ##
  8. ## if (seconds_old > 10e6) {
  9. ## print("Whoa! More than a million seconds old, what a wise man!")
  10. ## } else {
  11. ## print("Meh!")
  12. ## }

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

  1. opt_code <- optimize_text(opt_code, iterations = 1)
  1. ## Optimization number 1
  2. ## # I want to know my age in seconds!
  3. ## years_old <- 29
  4. ## days_old <- 10585 # leap years don't exist
  5. ## hours_old <- 254040
  6. ## seconds_old <- 3600 * 254040
  7. ##
  8. ## if (seconds_old > 10e6) {
  9. ## print("Whoa! More than a million seconds old, what a wise man!")
  10. ## } else {
  11. ## print("Meh!")
  12. ## }

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

  1. opt_code <- optimize_text(opt_code, iterations = 1)
  1. ## Optimization number 1
  2. ## # I want to know my age in seconds!
  3. ## years_old <- 29
  4. ## days_old <- 10585 # leap years don't exist
  5. ## hours_old <- 254040
  6. ## seconds_old <- 914544000
  7. ##
  8. ## if (914544000 > 10e6) {
  9. ## print("Whoa! More than a million seconds old, what a wise man!")
  10. ## } else {
  11. ## print("Meh!")
  12. ## }

It has folded the seconds_old variable, and then propagated it into
the if condition. Another pass:

  1. opt_code <- optimize_text(opt_code, iterations = 1)
  1. ## Optimization number 1
  2. ## # I want to know my age in seconds!
  3. ## years_old <- 29
  4. ## days_old <- 10585 # leap years don't exist
  5. ## hours_old <- 254040
  6. ## seconds_old <- 914544000
  7. ##
  8. ## 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.