Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Last active August 22, 2024 20:48
Show Gist options
  • Save tjmahr/b12e8df3c84d1e613ee97c67722a85ac to your computer and use it in GitHub Desktop.
Save tjmahr/b12e8df3c84d1e613ee97c67722a85ac to your computer and use it in GitHub Desktop.
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)
  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
}

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]") |> 
  apply_to_file_check_log(function(x) toupper(x))
#> List of 2
#>  $ x    : chr [1:26] "a" "b" "c" "d" ...
#>  $ 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"

Created on 2024-08-22 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment