Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Last active August 22, 2024 20:48

Revisions

  1. tjmahr revised this gist Aug 22, 2024. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions logger.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,12 @@ as_file_check_log.default <- function(x) {
    apply_to_file_check_log <- function(x, fn, ...) {
    results <- fn(x$x, ...)
    l <- list(results)
    names(l) <- deparse1(rlang::call2(substitute(fn), quote(x), ...))
    call <- rlang::call2(substitute(fn), quote(x), ...)
    if (!rlang::is_call_simple(call)) {
    f <- paste0("<unnamed-function-", length(x$notes) + 1, ">")
    call <- rlang::call2(rlang::sym(f), quote(x), ...)
    }
    names(l) <- deparse1(call)
    x$notes <- c(x$notes, l)
    x
    }
    @@ -18,14 +23,16 @@ as_file_check_log(files) |>
    apply_to_file_check_log(head) |>
    apply_to_file_check_log(tail, 3) |>
    apply_to_file_check_log(toupper) |>
    apply_to_file_check_log(stringr::str_subset, "[aeiou]")
    apply_to_file_check_log(stringr::str_subset, "[aeiou]") |>
    apply_to_file_check_log(function(x) toupper(x))
    #> List of 2
    #> $ x : chr [1:26] "a" "b" "c" "d" ...
    #> $ notes:List of 4
    #> $ notes:List of 5
    #> ..$ head(x) : chr [1:6] "a" "b" "c" "d" ...
    #> ..$ tail(x, 3) : chr [1:3] "x" "y" "z"
    #> ..$ toupper(x) : chr [1:26] "A" "B" "C" "D" ...
    #> ..$ stringr::str_subset(x, "[aeiou]"): chr [1:5] "a" "e" "i" "o" ...
    #> ..$ `<unnamed-function-5>`(x) : chr [1:26] "A" "B" "C" "D" ...
    #> - attr(*, "class")= chr "file_check_log"
    ```

  2. tjmahr created this gist Aug 22, 2024.
    32 changes: 32 additions & 0 deletions logger.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    ``` r
    print.file_check_log <- function(x, ...) str(x, ...)
    as_file_check_log <- function(x) UseMethod("as_file_check_log")
    as_file_check_log.default <- function(x) {
    structure(list(x = x, notes = list()), class = "file_check_log")
    }

    apply_to_file_check_log <- function(x, fn, ...) {
    results <- fn(x$x, ...)
    l <- list(results)
    names(l) <- deparse1(rlang::call2(substitute(fn), quote(x), ...))
    x$notes <- c(x$notes, l)
    x
    }

    files <- letters
    as_file_check_log(files) |>
    apply_to_file_check_log(head) |>
    apply_to_file_check_log(tail, 3) |>
    apply_to_file_check_log(toupper) |>
    apply_to_file_check_log(stringr::str_subset, "[aeiou]")
    #> List of 2
    #> $ x : chr [1:26] "a" "b" "c" "d" ...
    #> $ notes:List of 4
    #> ..$ head(x) : chr [1:6] "a" "b" "c" "d" ...
    #> ..$ tail(x, 3) : chr [1:3] "x" "y" "z"
    #> ..$ toupper(x) : chr [1:26] "A" "B" "C" "D" ...
    #> ..$ stringr::str_subset(x, "[aeiou]"): chr [1:5] "a" "e" "i" "o" ...
    #> - attr(*, "class")= chr "file_check_log"
    ```

    <sup>Created on 2024-08-22 with [reprex v2.1.1](https://reprex.tidyverse.org)</sup>