├── .Rbuildignore ├── .github ├── .gitignore ├── CODE_OF_CONDUCT.md └── workflows │ ├── R-CMD-check.yaml │ ├── pkgdown.yaml │ ├── pr-commands.yaml │ └── test-coverage.yaml ├── .gitignore ├── CRAN-SUBMISSION ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── NEWS.md ├── R ├── carrier-package.R ├── compat-rlang.R └── crate.R ├── README.Rmd ├── README.md ├── codecov.yml ├── external.Rproj ├── man ├── carrier-package.Rd ├── crate.Rd └── is_crate.Rd ├── revdep ├── .gitignore ├── README.md ├── cran.md ├── failures.md └── problems.md └── tests ├── testthat.R └── testthat ├── helper-crate.R └── test-crate.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^CRAN-RELEASE$ 2 | ^cran-comments\.md$ 3 | ^codecov\.yml$ 4 | ^README\.Rmd$ 5 | ^external\.Rproj$ 6 | ^\.Rproj\.user$ 7 | ^\.travis\.yml$ 8 | ^\.github$ 9 | ^LICENSE\.md$ 10 | ^revdep$ 11 | ^CRAN-SUBMISSION$ 12 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, caste, color, religion, or sexual 10 | identity and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the overall 26 | community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or advances of 31 | any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email address, 35 | without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at codeofconduct@posit.co. 63 | All complaints will be reviewed and investigated promptly and fairly. 64 | 65 | All community leaders are obligated to respect the privacy and security of the 66 | reporter of any incident. 67 | 68 | ## Enforcement Guidelines 69 | 70 | Community leaders will follow these Community Impact Guidelines in determining 71 | the consequences for any action they deem in violation of this Code of Conduct: 72 | 73 | ### 1. Correction 74 | 75 | **Community Impact**: Use of inappropriate language or other behavior deemed 76 | unprofessional or unwelcome in the community. 77 | 78 | **Consequence**: A private, written warning from community leaders, providing 79 | clarity around the nature of the violation and an explanation of why the 80 | behavior was inappropriate. A public apology may be requested. 81 | 82 | ### 2. Warning 83 | 84 | **Community Impact**: A violation through a single incident or series of 85 | actions. 86 | 87 | **Consequence**: A warning with consequences for continued behavior. No 88 | interaction with the people involved, including unsolicited interaction with 89 | those enforcing the Code of Conduct, for a specified period of time. This 90 | includes avoiding interactions in community spaces as well as external channels 91 | like social media. Violating these terms may lead to a temporary or permanent 92 | ban. 93 | 94 | ### 3. Temporary Ban 95 | 96 | **Community Impact**: A serious violation of community standards, including 97 | sustained inappropriate behavior. 98 | 99 | **Consequence**: A temporary ban from any sort of interaction or public 100 | communication with the community for a specified period of time. No public or 101 | private interaction with the people involved, including unsolicited interaction 102 | with those enforcing the Code of Conduct, is allowed during this period. 103 | Violating these terms may lead to a permanent ban. 104 | 105 | ### 4. Permanent Ban 106 | 107 | **Community Impact**: Demonstrating a pattern of violation of community 108 | standards, including sustained inappropriate behavior, harassment of an 109 | individual, or aggression toward or disparagement of classes of individuals. 110 | 111 | **Consequence**: A permanent ban from any sort of public interaction within the 112 | community. 113 | 114 | ## Attribution 115 | 116 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 117 | version 2.1, available at 118 | . 119 | 120 | Community Impact Guidelines were inspired by 121 | [Mozilla's code of conduct enforcement ladder][https://github.com/mozilla/inclusion]. 122 | 123 | For answers to common questions about this code of conduct, see the FAQ at 124 | . Translations are available at . 125 | 126 | [homepage]: https://www.contributor-covenant.org 127 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | # 4 | # NOTE: This workflow is overkill for most R packages and 5 | # check-standard.yaml is likely a better choice. 6 | # usethis::use_github_action("check-standard") will install it. 7 | on: 8 | push: 9 | branches: [main, master] 10 | pull_request: 11 | branches: [main, master] 12 | 13 | name: R-CMD-check 14 | 15 | jobs: 16 | R-CMD-check: 17 | runs-on: ${{ matrix.config.os }} 18 | 19 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 20 | 21 | strategy: 22 | fail-fast: false 23 | matrix: 24 | config: 25 | - {os: macos-latest, r: 'release'} 26 | 27 | - {os: windows-latest, r: 'release'} 28 | # Use 3.6 to trigger usage of RTools35 29 | - {os: windows-latest, r: '3.6'} 30 | # use 4.1 to check with rtools40's older compiler 31 | - {os: windows-latest, r: '4.1'} 32 | 33 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 34 | - {os: ubuntu-latest, r: 'release'} 35 | - {os: ubuntu-latest, r: 'oldrel-1'} 36 | - {os: ubuntu-latest, r: 'oldrel-2'} 37 | - {os: ubuntu-latest, r: 'oldrel-3'} 38 | - {os: ubuntu-latest, r: 'oldrel-4'} 39 | 40 | env: 41 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 42 | R_KEEP_PKG_SOURCE: yes 43 | 44 | steps: 45 | - uses: actions/checkout@v3 46 | 47 | - uses: r-lib/actions/setup-pandoc@v2 48 | 49 | - uses: r-lib/actions/setup-r@v2 50 | with: 51 | r-version: ${{ matrix.config.r }} 52 | http-user-agent: ${{ matrix.config.http-user-agent }} 53 | use-public-rspm: true 54 | 55 | - uses: r-lib/actions/setup-r-dependencies@v2 56 | with: 57 | extra-packages: any::rcmdcheck 58 | needs: check 59 | 60 | - uses: r-lib/actions/check-r-package@v2 61 | with: 62 | upload-snapshots: true 63 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | release: 9 | types: [published] 10 | workflow_dispatch: 11 | 12 | name: pkgdown 13 | 14 | jobs: 15 | pkgdown: 16 | runs-on: ubuntu-latest 17 | # Only restrict concurrency for non-PR jobs 18 | concurrency: 19 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} 20 | env: 21 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 22 | steps: 23 | - uses: actions/checkout@v3 24 | 25 | - uses: r-lib/actions/setup-pandoc@v2 26 | 27 | - uses: r-lib/actions/setup-r@v2 28 | with: 29 | use-public-rspm: true 30 | 31 | - uses: r-lib/actions/setup-r-dependencies@v2 32 | with: 33 | extra-packages: any::pkgdown, local::. 34 | needs: website 35 | 36 | - name: Build site 37 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) 38 | shell: Rscript {0} 39 | 40 | - name: Deploy to GitHub pages 🚀 41 | if: github.event_name != 'pull_request' 42 | uses: JamesIves/github-pages-deploy-action@v4.4.1 43 | with: 44 | clean: false 45 | branch: gh-pages 46 | folder: docs 47 | -------------------------------------------------------------------------------- /.github/workflows/pr-commands.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | issue_comment: 5 | types: [created] 6 | 7 | name: Commands 8 | 9 | jobs: 10 | document: 11 | if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }} 12 | name: document 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | steps: 17 | - uses: actions/checkout@v3 18 | 19 | - uses: r-lib/actions/pr-fetch@v2 20 | with: 21 | repo-token: ${{ secrets.GITHUB_TOKEN }} 22 | 23 | - uses: r-lib/actions/setup-r@v2 24 | with: 25 | use-public-rspm: true 26 | 27 | - uses: r-lib/actions/setup-r-dependencies@v2 28 | with: 29 | extra-packages: any::roxygen2 30 | needs: pr-document 31 | 32 | - name: Document 33 | run: roxygen2::roxygenise() 34 | shell: Rscript {0} 35 | 36 | - name: commit 37 | run: | 38 | git config --local user.name "$GITHUB_ACTOR" 39 | git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" 40 | git add man/\* NAMESPACE 41 | git commit -m 'Document' 42 | 43 | - uses: r-lib/actions/pr-push@v2 44 | with: 45 | repo-token: ${{ secrets.GITHUB_TOKEN }} 46 | 47 | style: 48 | if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }} 49 | name: style 50 | runs-on: ubuntu-latest 51 | env: 52 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 53 | steps: 54 | - uses: actions/checkout@v3 55 | 56 | - uses: r-lib/actions/pr-fetch@v2 57 | with: 58 | repo-token: ${{ secrets.GITHUB_TOKEN }} 59 | 60 | - uses: r-lib/actions/setup-r@v2 61 | 62 | - name: Install dependencies 63 | run: install.packages("styler") 64 | shell: Rscript {0} 65 | 66 | - name: Style 67 | run: styler::style_pkg() 68 | shell: Rscript {0} 69 | 70 | - name: commit 71 | run: | 72 | git config --local user.name "$GITHUB_ACTOR" 73 | git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" 74 | git add \*.R 75 | git commit -m 'Style' 76 | 77 | - uses: r-lib/actions/pr-push@v2 78 | with: 79 | repo-token: ${{ secrets.GITHUB_TOKEN }} 80 | -------------------------------------------------------------------------------- /.github/workflows/test-coverage.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: test-coverage 10 | 11 | jobs: 12 | test-coverage: 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | 20 | - uses: r-lib/actions/setup-r@v2 21 | with: 22 | use-public-rspm: true 23 | 24 | - uses: r-lib/actions/setup-r-dependencies@v2 25 | with: 26 | extra-packages: any::covr 27 | needs: coverage 28 | 29 | - name: Test coverage 30 | run: | 31 | covr::codecov( 32 | quiet = FALSE, 33 | clean = FALSE, 34 | install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package") 35 | ) 36 | shell: Rscript {0} 37 | 38 | - name: Show testthat output 39 | if: always() 40 | run: | 41 | ## -------------------------------------------------------------------- 42 | find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true 43 | shell: bash 44 | 45 | - name: Upload test results 46 | if: failure() 47 | uses: actions/upload-artifact@v3 48 | with: 49 | name: coverage-test-failures 50 | path: ${{ runner.temp }}/package 51 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user -------------------------------------------------------------------------------- /CRAN-SUBMISSION: -------------------------------------------------------------------------------- 1 | Version: 0.1.1 2 | Date: 2023-04-28 11:30:02 UTC 3 | SHA: 78b7e2f95bf8aee1917d98a8db406d9d70952c3e 4 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: carrier 2 | Title: Isolate Functions for Remote Execution 3 | Version: 0.1.1.9000 4 | Authors@R: c( 5 | person("Lionel", "Henry", , "lionel@posit.co", role = c("aut", "cre")), 6 | person("Posit Software, PBC", role = c("cph", "fnd")) 7 | ) 8 | Description: Sending functions to remote processes can be wasteful of 9 | resources because they carry their environments with them. With the 10 | carrier package, it is easy to create functions that are isolated from 11 | their environment. These isolated functions, also called crates, print 12 | at the console with their total size and can be easily tested locally 13 | before being sent to a remote. 14 | License: MIT + file LICENSE 15 | URL: https://github.com/r-lib/carrier 16 | BugReports: https://github.com/r-lib/carrier/issues 17 | Depends: 18 | R (>= 3.4.0) 19 | Imports: 20 | lobstr, 21 | rlang (>= 1.0.1) 22 | Suggests: 23 | covr, 24 | testthat (>= 3.0.0) 25 | ByteCompile: true 26 | Encoding: UTF-8 27 | Roxygen: list(markdown = TRUE) 28 | RoxygenNote: 7.2.3 29 | Config/testthat/edition: 3 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2023 2 | COPYRIGHT HOLDER: carrier authors 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2023 carrier authors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(print,crate) 4 | export(crate) 5 | export(is_crate) 6 | import(rlang) 7 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # carrier (development version) 2 | 3 | # carrier 0.1.1 4 | 5 | * Crated functions no longer carry source references (#6). 6 | 7 | * Fixed issue that prevented crate sizes to be printed in 8 | human-readable format (r-lib/lobstr#60). 9 | 10 | 11 | # carrier 0.1.0 12 | 13 | Initial release. The package currently only contains a single method 14 | for creating crates that requires users to be explicit about what data 15 | to pack in the crate. A future release will provide a method to figure 16 | out automatically what objects the crate depends on (with inevitable 17 | false positives and negatives). 18 | -------------------------------------------------------------------------------- /R/carrier-package.R: -------------------------------------------------------------------------------- 1 | #' @keywords internal 2 | "_PACKAGE" 3 | 4 | ## usethis namespace: start 5 | ## usethis namespace: end 6 | NULL 7 | -------------------------------------------------------------------------------- /R/compat-rlang.R: -------------------------------------------------------------------------------- 1 | 2 | # Remove after rlang 0.3.0 is released 3 | locally <- function(..., .env = env(caller_env())) { 4 | dots <- exprs(...) 5 | nms <- names(dots) 6 | out <- NULL 7 | 8 | for (i in seq_along(dots)) { 9 | out <- eval_bare(dots[[i]], .env) 10 | 11 | nm <- nms[[i]] 12 | if (nm != "") { 13 | .env[[nm]] <- out 14 | } 15 | } 16 | 17 | out 18 | } 19 | -------------------------------------------------------------------------------- /R/crate.R: -------------------------------------------------------------------------------- 1 | #' @import rlang 2 | NULL 3 | 4 | #' Crate a function to share with another process 5 | #' 6 | #' @description 7 | #' 8 | #' `crate()` creates functions in a self-contained environment 9 | #' (technically, a child of the base environment). This has two 10 | #' advantages: 11 | #' 12 | #' * They can easily be executed in another process. 13 | #' 14 | #' * Their effects are reproducible. You can run them locally with the 15 | #' same results as on a different process. 16 | #' 17 | #' Creating self-contained functions requires some care, see section 18 | #' below. 19 | #' 20 | #' 21 | #' @section Creating self-contained functions: 22 | #' 23 | #' * They should call package functions with an explicit `::` 24 | #' namespace. This includes packages in the default search path with 25 | #' the exception of the base package. For instance `var()` from the 26 | #' stats package must be called with its namespace prefix: 27 | #' `stats::var(x)`. 28 | #' 29 | #' * They should declare any data they depend on. You can declare data 30 | #' by supplying additional arguments or by unquoting objects with `!!`. 31 | #' 32 | #' @param .fn A fresh formula or function. "Fresh" here means that 33 | #' they should be declared in the call to `crate()`. See examples if 34 | #' you need to crate a function that is already defined. Formulas 35 | #' are converted to purrr-like lambda functions using 36 | #' [rlang::as_function()]. 37 | #' @param ... Arguments to declare in the environment of `.fn`. If a 38 | #' name is supplied, the object is assigned to that name. Otherwise 39 | #' the argument is automatically named after itself. 40 | #' 41 | #' @export 42 | #' @examples 43 | #' # You can create functions using the ordinary notation: 44 | #' crate(function(x) stats::var(x)) 45 | #' 46 | #' # Or the formula notation: 47 | #' crate(~ stats::var(.x)) 48 | #' 49 | #' # Declare data by supplying named arguments. You can test you have 50 | #' # declared all necessary data by calling your crated function: 51 | #' na_rm <- TRUE 52 | #' fn <- crate(~ stats::var(.x, na.rm = na_rm)) 53 | #' try(fn(1:10)) 54 | #' 55 | #' # For small data it is handy to unquote instead. Unquoting inlines 56 | #' # objects inside the function. This is less verbose if your 57 | #' # function depends on many small objects: 58 | #' fn <- crate(~ stats::var(.x, na.rm = !!na_rm)) 59 | #' fn(1:10) 60 | #' 61 | #' # One downside is that the individual sizes of unquoted objects 62 | #' # won't be shown in the crate printout: 63 | #' fn 64 | #' 65 | #' 66 | #' # The function or formula you pass to crate() should defined inside 67 | #' # the crate() call, i.e. you can't pass an already defined 68 | #' # function: 69 | #' fn <- function(x) toupper(x) 70 | #' try(crate(fn)) 71 | #' 72 | #' # If you really need to crate an existing function, you can 73 | #' # explicitly set its environment to the crate environment with the 74 | #' # set_env() function from rlang: 75 | #' crate(rlang::set_env(fn)) 76 | crate <- function(.fn, ...) { 77 | # Evaluate arguments in a child of the caller so the caller context 78 | # is in scope and new data is created in a separate child 79 | env <- child_env(caller_env()) 80 | dots <- exprs(...) 81 | locally(!!!dots, .env = env) 82 | 83 | # Quote and evaluate in the local env to avoid capturing execution 84 | # envs when user passed an unevaluated function or formula 85 | fn <- eval_bare(enexpr(.fn), env) 86 | 87 | # Isolate the evaluation environment from the search path 88 | env_poke_parent(env, base_env()) 89 | 90 | if (is_formula(fn)) { 91 | fn <- as_function(fn) 92 | } else if (!is_function(fn)) { 93 | abort("`.fn` must evaluate to a function") 94 | } 95 | 96 | if (!is_reference(get_env(fn), env)) { 97 | abort("The function must be defined inside the `crate()` call") 98 | } 99 | 100 | # Remove potentially heavy srcrefs (#6) 101 | fn <- zap_srcref(fn) 102 | 103 | new_crate(fn) 104 | } 105 | 106 | 107 | new_crate <- function(crate) { 108 | if (!is_function(crate)) { 109 | abort("`crate` must be a function") 110 | } 111 | 112 | structure(crate, class = "crate") 113 | } 114 | 115 | #' Is an object a crate? 116 | #' 117 | #' @param x An object to test. 118 | #' @export 119 | is_crate <- function(x) { 120 | inherits(x, "crate") 121 | } 122 | 123 | # Unexported until the `bytes` class is moved to lobstr (and probably 124 | # becomes `lobstr_bytes`) 125 | crate_sizes <- function(crate) { 126 | bare_fn <- unclass(crate) 127 | environment(bare_fn) <- global_env() 128 | 129 | bare_size <- lobstr::obj_size(bare_fn) 130 | 131 | env <- fn_env(crate) 132 | nms <- ls(env) 133 | 134 | n <- length(nms) + 1 135 | out <- new_list(n, c("function", nms)) 136 | out[[1]] <- bare_size 137 | 138 | index <- seq2(2, n) 139 | get_size <- function(nm) lobstr::obj_size(env[[nm]]) 140 | out[index] <- lapply(nms, get_size) 141 | 142 | # Sort data by decreasing size but keep function first 143 | order <- order(as.numeric(out[-1]), decreasing = TRUE) 144 | out <- out[c(1, order + 1)] 145 | 146 | out 147 | } 148 | 149 | 150 | #' @export 151 | print.crate <- function(x, ...) { 152 | sizes <- crate_sizes(x) 153 | 154 | total_size <- format_bytes(lobstr::obj_size(x), ...) 155 | cat(sprintf(" %s\n", total_size)) 156 | 157 | fn_size <- format_bytes(sizes[[1]], ...) 158 | cat(sprintf("* function: %s\n", fn_size)) 159 | 160 | nms <- names(sizes) 161 | for (i in seq2_along(2, sizes)) { 162 | nm <- nms[[i]] 163 | size <- format_bytes(sizes[[i]], ...) 164 | cat(sprintf("* `%s`: %s\n", nm, size)) 165 | } 166 | 167 | # Print function without the environment tag 168 | bare_fn <- unclass(x) 169 | environment(bare_fn) <- global_env() 170 | print(bare_fn, ...) 171 | 172 | invisible(x) 173 | } 174 | 175 | format_bytes <- function(x) { 176 | format(as_bytes(unclass(x))) 177 | } 178 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | [![R-CMD-check](https://github.com/r-lib/carrier/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/carrier/actions/workflows/R-CMD-check.yaml) 7 | 8 | 9 | ```{r setup, include = FALSE} 10 | knitr::opts_chunk$set( 11 | collapse = TRUE, 12 | comment = "#>", 13 | fig.path = "man/figures/README-", 14 | out.width = "100%" 15 | ) 16 | 17 | library("carrier") 18 | ``` 19 | 20 | # carrier 21 | 22 | The carrier package provides tools to package up functions so they can be sent to remote R sessions or to different processes, and tools to test your crates locally. They make it easy to control what data should be packaged with the function and what size your crated function is. 23 | 24 | Currently, carrier only provides a strict function constructor that forces you to be explicit about the functions and the data your function depends on. In the future it will also provide tools to figure it out automatically. 25 | 26 | See also the [bundle](https://rstudio.github.io/bundle/) package for the tidymodels ecosystem. 27 | 28 | 29 | ## Creating explicit crated functions 30 | 31 | `crate()` is a function constructor that forces you to be explicit about which data should be packaged with the function. You can create functions using the standard R syntax: 32 | 33 | ```{r} 34 | crate(function(x) mean(x, na.rm = TRUE)) 35 | ``` 36 | 37 | Or with a purrr-like lambda syntax: 38 | 39 | ```{r} 40 | crate(~mean(.x, na.rm = TRUE)) 41 | ``` 42 | 43 | The crated function prints with its total size in the header, so you know how much data you will send to remotes. The size of the bare function without any data is also printed in the first bullet, and if you add objects to the crate their size is printed in decreasing order. 44 | 45 | 46 | ### Explicit namespaces 47 | 48 | `crate()` requires you to be explicit about all dependencies of your function. Except for base functions, you have to call functions with their namespace prefix. You can test your function locally to make sure you've been explicit enough. In the following example we forgot to specify that `var()` comes from the stats namespace: 49 | 50 | ```{r, error = TRUE} 51 | fn <- crate(~var(.x)) 52 | fn(1:10) 53 | ``` 54 | 55 | So let's add the namespace prefix: 56 | 57 | ```{r} 58 | fn <- crate(~stats::var(.x)) 59 | fn(1:10) 60 | ``` 61 | 62 | 63 | ### Explicit data 64 | 65 | If your function depends on global data, you need to declare it to make it available to your crated function. Here we forgot to declare `na_rm`: 66 | 67 | ```{r, error = TRUE} 68 | na_rm <- TRUE 69 | 70 | fn <- crate(function(x) stats::var(x, na.rm = na_rm)) 71 | fn(1:10) 72 | ``` 73 | 74 | There are two techniques for packaging data into your crate: passing data as arguments, and unquoting data in the function. 75 | 76 | #### Passing data as arguments 77 | 78 | You can declare objects by passing them as named arguments to `crate()`: 79 | 80 | ```{r} 81 | fn <- crate( 82 | function(x) stats::var(x, na.rm = na_rm), 83 | na_rm = na_rm 84 | ) 85 | fn(1:10) 86 | ``` 87 | 88 | Note how the size of each imported object is displayed when you print the crated function: 89 | 90 | ```{r} 91 | fn 92 | ``` 93 | 94 | 95 | #### Unquoting data in the function 96 | 97 | Another way of packaging data is to unquote objects with `!!`. This works because unquoting inlines objects in function calls. Unquoting can be less verbose if you have many small objects to import inside the function. 98 | 99 | ```{r} 100 | crate(function(x) stats::var(x, na.rm = !!na_rm)) 101 | ``` 102 | 103 | However, be careful not to unquote large objects because: 104 | 105 | * The sizes of unquoted objects are not detailed when you print the crate. 106 | * Inlined data can cause noisy output. 107 | 108 | Let's unquote a data frame to see the noise caused by inlining: 109 | 110 | ```{r} 111 | # Subset a few rows so the call is not too noisy 112 | data <- mtcars[1:5, ] 113 | 114 | # Inline data in call by unquoting 115 | fn <- crate(~stats::lm(.x, data = !!data)) 116 | ``` 117 | 118 | This crate will print with noisy inlined data: 119 | 120 | ```{r} 121 | fn 122 | ``` 123 | 124 | Same for the function call recorded by `lm()`: 125 | 126 | ```{r} 127 | fn(disp ~ drat) 128 | ``` 129 | 130 | 131 | ## Code of Conduct 132 | 133 | Please note that the carrier project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms. 134 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | [![R-CMD-check](https://github.com/r-lib/carrier/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/carrier/actions/workflows/R-CMD-check.yaml) 5 | 6 | 7 | # carrier 8 | 9 | The carrier package provides tools to package up functions so they can 10 | be sent to remote R sessions or to different processes, and tools to 11 | test your crates locally. They make it easy to control what data should 12 | be packaged with the function and what size your crated function is. 13 | 14 | Currently, carrier only provides a strict function constructor that 15 | forces you to be explicit about the functions and the data your function 16 | depends on. In the future it will also provide tools to figure it out 17 | automatically. 18 | 19 | See also the [bundle](https://rstudio.github.io/bundle/) package for the 20 | tidymodels ecosystem. 21 | 22 | ## Creating explicit crated functions 23 | 24 | `crate()` is a function constructor that forces you to be explicit about 25 | which data should be packaged with the function. You can create 26 | functions using the standard R syntax: 27 | 28 | ``` r 29 | crate(function(x) mean(x, na.rm = TRUE)) 30 | #> 7 kB 31 | #> * function: 6.55 kB 32 | #> function(x) mean(x, na.rm = TRUE) 33 | ``` 34 | 35 | Or with a purrr-like lambda syntax: 36 | 37 | ``` r 38 | crate(~mean(.x, na.rm = TRUE)) 39 | #> 1.57 kB 40 | #> * function: 1.01 kB 41 | #> function (..., .x = ..1, .y = ..2, . = ..1) 42 | #> mean(.x, na.rm = TRUE) 43 | ``` 44 | 45 | The crated function prints with its total size in the header, so you 46 | know how much data you will send to remotes. The size of the bare 47 | function without any data is also printed in the first bullet, and if 48 | you add objects to the crate their size is printed in decreasing order. 49 | 50 | ### Explicit namespaces 51 | 52 | `crate()` requires you to be explicit about all dependencies of your 53 | function. Except for base functions, you have to call functions with 54 | their namespace prefix. You can test your function locally to make sure 55 | you’ve been explicit enough. In the following example we forgot to 56 | specify that `var()` comes from the stats namespace: 57 | 58 | ``` r 59 | fn <- crate(~var(.x)) 60 | fn(1:10) 61 | #> Error in var(.x): could not find function "var" 62 | ``` 63 | 64 | So let’s add the namespace prefix: 65 | 66 | ``` r 67 | fn <- crate(~stats::var(.x)) 68 | fn(1:10) 69 | #> [1] 9.166667 70 | ``` 71 | 72 | ### Explicit data 73 | 74 | If your function depends on global data, you need to declare it to make 75 | it available to your crated function. Here we forgot to declare `na_rm`: 76 | 77 | ``` r 78 | na_rm <- TRUE 79 | 80 | fn <- crate(function(x) stats::var(x, na.rm = na_rm)) 81 | fn(1:10) 82 | #> Error in fn(1:10): object 'na_rm' not found 83 | ``` 84 | 85 | There are two techniques for packaging data into your crate: passing 86 | data as arguments, and unquoting data in the function. 87 | 88 | #### Passing data as arguments 89 | 90 | You can declare objects by passing them as named arguments to `crate()`: 91 | 92 | ``` r 93 | fn <- crate( 94 | function(x) stats::var(x, na.rm = na_rm), 95 | na_rm = na_rm 96 | ) 97 | fn(1:10) 98 | #> [1] 9.166667 99 | ``` 100 | 101 | Note how the size of each imported object is displayed when you print 102 | the crated function: 103 | 104 | ``` r 105 | fn 106 | #> 9.31 kB 107 | #> * function: 8.75 kB 108 | #> * `na_rm`: 56 B 109 | #> function(x) stats::var(x, na.rm = na_rm) 110 | ``` 111 | 112 | #### Unquoting data in the function 113 | 114 | Another way of packaging data is to unquote objects with `!!`. This 115 | works because unquoting inlines objects in function calls. Unquoting can 116 | be less verbose if you have many small objects to import inside the 117 | function. 118 | 119 | ``` r 120 | crate(function(x) stats::var(x, na.rm = !!na_rm)) 121 | #> 7.86 kB 122 | #> * function: 7.42 kB 123 | #> function(x) stats::var(x, na.rm = !!na_rm) 124 | ``` 125 | 126 | However, be careful not to unquote large objects because: 127 | 128 | - The sizes of unquoted objects are not detailed when you print the 129 | crate. 130 | - Inlined data can cause noisy output. 131 | 132 | Let’s unquote a data frame to see the noise caused by inlining: 133 | 134 | ``` r 135 | # Subset a few rows so the call is not too noisy 136 | data <- mtcars[1:5, ] 137 | 138 | # Inline data in call by unquoting 139 | fn <- crate(~stats::lm(.x, data = !!data)) 140 | ``` 141 | 142 | This crate will print with noisy inlined data: 143 | 144 | ``` r 145 | fn 146 | #> 4.65 kB 147 | #> * function: 4.14 kB 148 | #> function (..., .x = ..1, .y = ..2, . = ..1) 149 | #> stats::lm(.x, data = list(mpg = c(21, 21, 22.8, 21.4, 18.7), 150 | #> cyl = c(6, 6, 4, 6, 8), disp = c(160, 160, 108, 258, 360), 151 | #> hp = c(110, 110, 93, 110, 175), drat = c(3.9, 3.9, 3.85, 152 | #> 3.08, 3.15), wt = c(2.62, 2.875, 2.32, 3.215, 3.44), qsec = c(16.46, 153 | #> 17.02, 18.61, 19.44, 17.02), vs = c(0, 0, 1, 1, 0), am = c(1, 154 | #> 1, 1, 0, 0), gear = c(4, 4, 4, 3, 3), carb = c(4, 4, 1, 1, 155 | #> 2))) 156 | ``` 157 | 158 | Same for the function call recorded by `lm()`: 159 | 160 | ``` r 161 | fn(disp ~ drat) 162 | #> 163 | #> Call: 164 | #> stats::lm(formula = .x, data = structure(list(mpg = c(21, 21, 165 | #> 22.8, 21.4, 18.7), cyl = c(6, 6, 4, 6, 8), disp = c(160, 160, 166 | #> 108, 258, 360), hp = c(110, 110, 93, 110, 175), drat = c(3.9, 167 | #> 3.9, 3.85, 3.08, 3.15), wt = c(2.62, 2.875, 2.32, 3.215, 3.44 168 | #> ), qsec = c(16.46, 17.02, 18.61, 19.44, 17.02), vs = c(0, 0, 169 | #> 1, 1, 0), am = c(1, 1, 1, 0, 0), gear = c(4, 4, 4, 3, 3), carb = c(4, 170 | #> 4, 1, 1, 2)), row.names = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", 171 | #> "Hornet 4 Drive", "Hornet Sportabout"), class = "data.frame")) 172 | #> 173 | #> Coefficients: 174 | #> (Intercept) drat 175 | #> 952.3 -207.8 176 | ``` 177 | 178 | ## Code of Conduct 179 | 180 | Please note that the carrier project is released with a [Contributor 181 | Code of 182 | Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). 183 | By contributing to this project, you agree to abide by its terms. 184 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | informational: true 10 | patch: 11 | default: 12 | target: auto 13 | threshold: 1% 14 | informational: true 15 | -------------------------------------------------------------------------------- /external.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | Encoding: UTF-8 9 | 10 | AutoAppendNewline: Yes 11 | StripTrailingWhitespace: Yes 12 | 13 | BuildType: Package 14 | PackageUseDevtools: Yes 15 | PackageInstallArgs: --no-multiarch --with-keep.source 16 | PackageRoxygenize: rd,collate,namespace 17 | -------------------------------------------------------------------------------- /man/carrier-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/carrier-package.R 3 | \docType{package} 4 | \name{carrier-package} 5 | \alias{carrier} 6 | \alias{carrier-package} 7 | \title{carrier: Isolate Functions for Remote Execution} 8 | \description{ 9 | Sending functions to remote processes can be wasteful of resources because they carry their environments with them. With the carrier package, it is easy to create functions that are isolated from their environment. These isolated functions, also called crates, print at the console with their total size and can be easily tested locally before being sent to a remote. 10 | } 11 | \seealso{ 12 | Useful links: 13 | \itemize{ 14 | \item \url{https://github.com/r-lib/carrier} 15 | \item Report bugs at \url{https://github.com/r-lib/carrier/issues} 16 | } 17 | 18 | } 19 | \author{ 20 | \strong{Maintainer}: Lionel Henry \email{lionel@posit.co} 21 | 22 | Other contributors: 23 | \itemize{ 24 | \item Posit Software, PBC [copyright holder, funder] 25 | } 26 | 27 | } 28 | \keyword{internal} 29 | -------------------------------------------------------------------------------- /man/crate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/crate.R 3 | \name{crate} 4 | \alias{crate} 5 | \title{Crate a function to share with another process} 6 | \usage{ 7 | crate(.fn, ...) 8 | } 9 | \arguments{ 10 | \item{.fn}{A fresh formula or function. "Fresh" here means that 11 | they should be declared in the call to \code{crate()}. See examples if 12 | you need to crate a function that is already defined. Formulas 13 | are converted to purrr-like lambda functions using 14 | \code{\link[rlang:as_function]{rlang::as_function()}}.} 15 | 16 | \item{...}{Arguments to declare in the environment of \code{.fn}. If a 17 | name is supplied, the object is assigned to that name. Otherwise 18 | the argument is automatically named after itself.} 19 | } 20 | \description{ 21 | \code{crate()} creates functions in a self-contained environment 22 | (technically, a child of the base environment). This has two 23 | advantages: 24 | \itemize{ 25 | \item They can easily be executed in another process. 26 | \item Their effects are reproducible. You can run them locally with the 27 | same results as on a different process. 28 | } 29 | 30 | Creating self-contained functions requires some care, see section 31 | below. 32 | } 33 | \section{Creating self-contained functions}{ 34 | 35 | \itemize{ 36 | \item They should call package functions with an explicit \code{::} 37 | namespace. This includes packages in the default search path with 38 | the exception of the base package. For instance \code{var()} from the 39 | stats package must be called with its namespace prefix: 40 | \code{stats::var(x)}. 41 | \item They should declare any data they depend on. You can declare data 42 | by supplying additional arguments or by unquoting objects with \verb{!!}. 43 | } 44 | } 45 | 46 | \examples{ 47 | # You can create functions using the ordinary notation: 48 | crate(function(x) stats::var(x)) 49 | 50 | # Or the formula notation: 51 | crate(~ stats::var(.x)) 52 | 53 | # Declare data by supplying named arguments. You can test you have 54 | # declared all necessary data by calling your crated function: 55 | na_rm <- TRUE 56 | fn <- crate(~ stats::var(.x, na.rm = na_rm)) 57 | try(fn(1:10)) 58 | 59 | # For small data it is handy to unquote instead. Unquoting inlines 60 | # objects inside the function. This is less verbose if your 61 | # function depends on many small objects: 62 | fn <- crate(~ stats::var(.x, na.rm = !!na_rm)) 63 | fn(1:10) 64 | 65 | # One downside is that the individual sizes of unquoted objects 66 | # won't be shown in the crate printout: 67 | fn 68 | 69 | 70 | # The function or formula you pass to crate() should defined inside 71 | # the crate() call, i.e. you can't pass an already defined 72 | # function: 73 | fn <- function(x) toupper(x) 74 | try(crate(fn)) 75 | 76 | # If you really need to crate an existing function, you can 77 | # explicitly set its environment to the crate environment with the 78 | # set_env() function from rlang: 79 | crate(rlang::set_env(fn)) 80 | } 81 | -------------------------------------------------------------------------------- /man/is_crate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/crate.R 3 | \name{is_crate} 4 | \alias{is_crate} 5 | \title{Is an object a crate?} 6 | \usage{ 7 | is_crate(x) 8 | } 9 | \arguments{ 10 | \item{x}{An object to test.} 11 | } 12 | \description{ 13 | Is an object a crate? 14 | } 15 | -------------------------------------------------------------------------------- /revdep/.gitignore: -------------------------------------------------------------------------------- 1 | checks 2 | library 3 | checks.noindex 4 | library.noindex 5 | cloud.noindex 6 | data.sqlite 7 | *.html 8 | -------------------------------------------------------------------------------- /revdep/README.md: -------------------------------------------------------------------------------- 1 | # Platform 2 | 3 | |field |value | 4 | |:--------|:------------------------------------------| 5 | |version |R version 4.3.0 (2023-04-21) | 6 | |os |macOS Ventura 13.1 | 7 | |system |aarch64, darwin20 | 8 | |ui |X11 | 9 | |language |(EN) | 10 | |collate |en_US.UTF-8 | 11 | |ctype |en_US.UTF-8 | 12 | |tz |Europe/Brussels | 13 | |date |2023-04-28 | 14 | |pandoc |2.14.0.1 @ /usr/local/bin/ (via rmarkdown) | 15 | 16 | # Dependencies 17 | 18 | |package |old |new |Δ | 19 | |:-----------|:------|:-----|:--| 20 | |carrier |0.1.0 |0.1.1 |* | 21 | |cli |3.6.1 |NA |* | 22 | |cpp11 |0.4.3 |0.4.3 | | 23 | |crayon |1.5.2 |1.5.2 | | 24 | |glue |1.6.2 |NA |* | 25 | |lifecycle |1.0.3 |NA |* | 26 | |lobstr |1.1.2 |1.1.2 | | 27 | |magrittr |2.0.3 |NA |* | 28 | |prettyunits |1.1.1 |1.1.1 | | 29 | |pryr |0.1.6 |NA |* | 30 | |Rcpp |1.0.10 |NA |* | 31 | |rlang |1.1.0 |1.1.0 | | 32 | |stringi |1.7.12 |NA |* | 33 | |stringr |1.5.0 |NA |* | 34 | |vctrs |0.6.2 |NA |* | 35 | 36 | # Revdeps 37 | 38 | -------------------------------------------------------------------------------- /revdep/cran.md: -------------------------------------------------------------------------------- 1 | ## revdepcheck results 2 | 3 | We checked 3 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package. 4 | 5 | * We saw 0 new problems 6 | * We failed to check 0 packages 7 | 8 | -------------------------------------------------------------------------------- /revdep/failures.md: -------------------------------------------------------------------------------- 1 | *Wow, no problems at all. :)* -------------------------------------------------------------------------------- /revdep/problems.md: -------------------------------------------------------------------------------- 1 | *Wow, no problems at all. :)* -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(carrier) 3 | 4 | test_check("carrier") 5 | -------------------------------------------------------------------------------- /tests/testthat/helper-crate.R: -------------------------------------------------------------------------------- 1 | 2 | expect_data <- function(crate, ...) { 3 | nms <- env_names(fn_env(crate)) 4 | expect_true(all(nms %in% c(...))) 5 | } 6 | -------------------------------------------------------------------------------- /tests/testthat/test-crate.R: -------------------------------------------------------------------------------- 1 | test_that("crate() supports lambda syntax", { 2 | expect_equal( 3 | crate(~NULL), 4 | new_crate(as_function(~NULL, env = current_env())), 5 | ignore_function_env = TRUE 6 | ) 7 | }) 8 | 9 | test_that("crate() requires functions", { 10 | expect_error(crate(1), "must evaluate to a function") 11 | }) 12 | 13 | test_that("crate() supports quasiquotation", { 14 | foo <- "foo" 15 | 16 | fn <- crate(function() toupper(!!foo)) 17 | expect_identical(body(fn), quote(toupper("foo"))) 18 | expect_identical(fn(), "FOO") 19 | 20 | fn <- crate(~ toupper(!!foo)) 21 | expect_identical(body(fn), quote(toupper("foo"))) 22 | expect_identical(fn(), "FOO") 23 | }) 24 | 25 | test_that("can supply data", { 26 | fn <- crate(~ toupper(foo), foo = "foo") 27 | expect_identical(fn(), "FOO") 28 | 29 | foo <- "foo" 30 | fn <- crate(~ toupper(foo), foo = foo) 31 | expect_identical(fn(), "FOO") 32 | }) 33 | 34 | test_that("can supply data before or after function", { 35 | foo <- "foo" 36 | fn <- crate(foo = foo, ~ toupper(foo)) 37 | expect_identical(fn(), "FOO") 38 | }) 39 | 40 | test_that("fails if relevant data not supplied", { 41 | foobar <- "foobar" 42 | fn <- crate(foo = "foo", ~ toupper(foobar)) 43 | expect_error(fn(), "not found") 44 | }) 45 | 46 | test_that("can supply data in a block", { 47 | fn <- crate({ 48 | foo <- "foo" 49 | bar <- "bar" 50 | ~ paste(foo, bar) 51 | }) 52 | 53 | expect_data(fn, "foo", "bar") 54 | }) 55 | 56 | test_that("crated function roundtrips under serialisation", { 57 | fn <- crate(~ toupper(foo), foo = "foo") 58 | out <- unserialize(serialize(fn, NULL)) 59 | expect_equal(as.list(fn_env(fn)), as.list(fn_env(out))) 60 | expect_equal(fn(), out()) 61 | }) 62 | 63 | test_that("new_crate() requires functions", { 64 | expect_error(new_crate(1), "must be a function") 65 | expect_error(new_crate(~foo), "must be a function") 66 | }) 67 | 68 | test_that("new_crate() crates", { 69 | expect_s3_class(new_crate(function() NULL), "crate") 70 | }) 71 | 72 | test_that("sizes are printed with the crate", { 73 | foo <- "foo" 74 | bar <- 1:100 75 | fn <- crate(~NULL, foo = foo, bar = bar) 76 | 77 | bare_fn <- fn 78 | attributes(bare_fn) <- NULL 79 | environment(bare_fn) <- global_env() 80 | 81 | bare_size <- format_bytes(lobstr::obj_size(bare_fn)) 82 | bar_size <- format_bytes(lobstr::obj_size(bar)) 83 | foo_size <- format_bytes(lobstr::obj_size(foo)) 84 | 85 | output <- " 86 | * function: %s 87 | * `bar`: %s 88 | * `foo`: %s" 89 | output <- sprintf(output, bare_size, bar_size, foo_size) 90 | 91 | expect_output(print(fn), output, fixed = TRUE) 92 | }) 93 | 94 | test_that("empty crates are printed correctly", { 95 | fn <- crate(~NULL) 96 | 97 | bare_fn <- fn 98 | attributes(bare_fn) <- NULL 99 | environment(bare_fn) <- global_env() 100 | 101 | bare_size <- format_bytes(lobstr::obj_size(bare_fn)) 102 | 103 | output <- " 104 | * function: %s 105 | function" 106 | output <- sprintf(output, bare_size) 107 | 108 | expect_output(print(fn), output, fixed = TRUE) 109 | }) 110 | 111 | test_that("function must be defined in the crate environment", { 112 | fn <- function() NULL 113 | expect_error(crate(fn), "must be defined inside") 114 | 115 | expect_s3_class(crate(set_env(fn)), "crate") 116 | }) 117 | --------------------------------------------------------------------------------