lbartnik / defer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

defer

CRAN version Travis build status AppVeyor Coverage
CRAN version Build Status AppVeyor Build Status codecov

Overview

defer wraps a function together with its dependencies. The result is a self-contained function that can be:

  • stored for documentation purposes
  • sent over to a different (possibly remote) R session and run there without elaborate dependency restoring

The wrapper itself is a function with the same signature (that is, formals()).

  • defer() wraps a function and discovers its dependencies
  • augment() defines default values for arguments
  • run_deferred() is an optional and explicit way to run the wrapper

Installation

Currently defer is available only on GitHub.

if(!require("devtools")) install.packages("devtools")
devtools::install_github("lbartnik/defer")

Usage

library(defer)

wrapper <- defer(function(x) x*x)
wrapper
#> Deferred-execution package
#> 
#> Entry function:
#>   function (x) 
#>   x * x

wrapper(10)
#> [1] 100

When wrapping a more elaborate pipeline:

C <- 10

f <- function(x) x*x + C
g <- function(y) (f(y) + 10) * f(y+3)
h <- function(z) mean(c(g(z), g(z+1), g(z+2)))

wrapper <- defer(h)
#> Found functions:
#>   g, f
#> variables:
#>   C
#> library calls:
#>   base::mean

rm(C, f, g, h)

wrapper(10)
#> [1] 29688.67

How about executing this last wrapper() via opencpu? It's enough to:

  • serialize the wrapper and turn the byte array into Base64-encoded ASCII string
  • put the string in a R snippet that restores the original R function object
  • run the function

(See the Examples vignette for more details.)

The best thing is that opencpu does not have to provide the defer package because the wrapper is self-contained!

library(httr)

public_opencpu_url <- "https://cloud.opencpu.org/ocpu/library/base/R/source/print"

# we're still using the same wrapper object as above
serialized_wrapper <- jsonlite::base64_enc(serialize(wrapper, NULL))

local_script_path <- tempfile(fileext = ".R")
cat(paste0("wrapper <- unserialize(jsonlite::base64_dec('", serialized_wrapper, "'))\n",
           "wrapper(10)\n"),
    file = local_script_path)

http_result <- httr::POST(public_opencpu_url,
                          body = list(file = upload_file(local_script_path)))
content(http_result, 'text')
#> [1] "$value\n[1] 29688.67\n\n$visible\n[1] TRUE\n\n"

About

License:Other


Languages

Language:R 100.0%