README.md

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:

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

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:

(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"


lbartnik/defer documentation built on May 20, 2019, 8:27 p.m.