https://github.com/lbartnik/defer
https://github.com/lbartnik/defer
r
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lbartnik/defer
- Owner: lbartnik
- License: other
- Created: 2015-12-22T07:02:45.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-11-21T05:46:07.000Z (almost 7 years ago)
- Last Synced: 2024-12-20T13:22:31.502Z (11 months ago)
- Topics: r
- Language: R
- Size: 125 KB
- Stars: 23
- Watchers: 5
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - lbartnik/defer - (R)
README
defer
==========================
| CRAN version | Travis build status | AppVeyor | Coverage |
| :-------------: |:---------------------:|:--------:|:--------:|
| [](https://cran.r-project.org/package=defer) | [](https://travis-ci.org/lbartnik/defer) | [](https://ci.appveyor.com/project/lbartnik/defer) | [](https://codecov.io/gh/lbartnik/defer)|
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.
``` r
if(!require("devtools")) install.packages("devtools")
devtools::install_github("lbartnik/defer")
```
Usage
-----
``` r
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:
``` r
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`](http://www.opencpu.org)?
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](inst/doc/examples.html) vignette for more details.)
The best thing is that `opencpu` does not have to provide the `defer` package
because the wrapper is self-contained!
``` r
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"
```