Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a renv environment from a crated function #554

Closed
lorenzwalthert opened this issue Oct 10, 2020 · 2 comments
Closed

Create a renv environment from a crated function #554

lorenzwalthert opened this issue Oct 10, 2020 · 2 comments

Comments

@lorenzwalthert
Copy link

The R package https://github.com/r-lib/carrier can be used to create standalone functions. However, it does not manage package dependencies. For this reason, deployment of R functions in arbitrary environments (e.g. deployment of crate functions in mlflow) is not that straight-forward because the user has to manage dependencies manually. I think it would be a great addition to connect renv and carrier, creating a renv.lockfile from a crated function:

fn <- carrier::crate(function(x) styler::style_text(x))
renv::init(bare = TRUE)
renv::snapshot(fn) # make snapshot an S3 generic to handle project (directories) / single files and crated functions

(The API change is just a suggestion, maybe there are better ways to implement this)

Then, to execute the function anywhere, one could just

renv::restore()
renv::activate()
fn <- readRDS('path/to/crated/function.Rds')
fn(...)

I assume most of the functionality to do this already exists in renv, but I could not find them in the exposed API.

This is similar to two existing renv issues that aim at using renv for low(er) level dependency management than an average user:

I also filed an issue in r-lib/carrier at some point: r-lib/carrier#5

@kevinushey
Copy link
Collaborator

kevinushey commented Oct 14, 2020

I've made some changes on master that should make this kind of workflow possible -- let me know if this captures what you need.

> deps <- renv::dependencies(function() { styler::style_text() })
> pkgs <- unique(deps$Package)
> renv::snapshot(packages = pkgs)
The following package(s) will be updated in the lockfile:

# CRAN ===============================
- R.cache       [* -> 0.14.0]
- R.methodsS3   [* -> 1.8.1]
- R.oo          [* -> 1.24.0]
- R.utils       [* -> 2.10.1]
- assertthat    [* -> 0.2.1]
- backports     [* -> 1.1.10]
- cli           [* -> 2.1.0]
- crayon        [* -> 1.3.4]
- digest        [* -> 0.6.25]
- ellipsis      [* -> 0.3.1]
- fansi         [* -> 0.4.1]
- glue          [* -> 1.4.2]
- lifecycle     [* -> 0.2.0]
- magrittr      [* -> 1.5]
- pillar        [* -> 1.4.6]
- pkgconfig     [* -> 2.0.3]
- purrr         [* -> 0.3.4]
- rematch2      [* -> 2.1.2]
- rprojroot     [* -> 1.3-2]
- styler        [* -> 1.3.2]
- tibble        [* -> 3.0.4]
- utf8          [* -> 1.1.4]
- vctrs         [* -> 0.3.4]
- withr         [* -> 2.3.0]
- xfun          [* -> 0.18]

Do you want to proceed? [y/N]: y
* Lockfile written to '~/scratch/test/renv.lock'.

@lorenzwalthert
Copy link
Author

lorenzwalthert commented Nov 2, 2020

Thanks @kevinushey. It looks like now, with any object that inherits from function (so crate), we can do the following:

fun <- carrier::crate(~styler::style_text(.x))
deps <- renv::dependencies(fun)
renv::snapshot(packages = unique(deps$Package))

Hence, I can achieve what I was after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants