Skip to content

Commit df4398e

Browse files
authored
Merge pull request #27 from b-rodrigues/fix_issue_7
Fix issue 7
2 parents 9b66834 + ec8d038 commit df4398e

16 files changed

Lines changed: 153 additions & 171 deletions

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AIDER_AUTO_COMMITS=false

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ inst/doc
1111
result
1212
.envrc
1313
.direnv
14-
.aider*
14+
.aider*
15+
.env

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Suggests:
3434
tidyr
3535
VignetteBuilder:
3636
knitr
37-
Config/fusen/version: 0.5.2
3837
Encoding: UTF-8
3938
LazyData: TRUE
4039
Roxygen: list(markdown = TRUE)

R/.R

Lines changed: 0 additions & 35 deletions
This file was deleted.

R/record.R

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# WARNING - Generated by {fusen} from dev/flat_chronicle.Rmd: do not edit by hand
2-
31
#' Decorates a function to output objects of type `chronicle`.
42
#' @param .f A function to decorate.
53
#' @param .g Optional. A function to apply to the intermediary results for monitoring purposes. Defaults to returning NA.
@@ -24,19 +22,52 @@
2422
#' @importFrom utils tail
2523
#' @examples
2624
#' record(sqrt)(10)
25+
#' record(sqrt)(x = 10)
2726
#' @export
2827
record <- function(.f, .g = (\(x) NA), strict = 2, diff = "none") {
2928
fstring <- deparse1(substitute(.f))
3029

3130
function(.value, ..., .log_df = data.frame()) {
32-
args <- paste0(rlang::enexprs(...), collapse = ",")
31+
# Capture the call and arguments
32+
other_args_exprs <- rlang::enexprs(...)
33+
34+
# Determine the main data value and other arguments based on call type
35+
if (missing(.value)) {
36+
# This is a direct call, not from a pipe.
37+
# e.g., r_sqrt(10) or r_select(df, col)
38+
if (length(other_args_exprs) == 0) {
39+
stop("At least one argument must be provided.", call. = FALSE)
40+
}
41+
# The first argument in ... is the data
42+
data_val <- eval(other_args_exprs[[1]], envir = parent.frame())
43+
func_args <- other_args_exprs[-1]
44+
log_args_exprs <- other_args_exprs
45+
} else {
46+
# This is a piped call, either from `|>` or `%>=%` (bind_record)
47+
# .value is the data from the pipe.
48+
data_val <- .value
49+
func_args <- other_args_exprs
50+
# For logging, we need to represent the full call.
51+
# deparse(substitute(.value)) will give the name of the variable if it came from a pipe.
52+
# For bind_record, it might be complex, so we just represent it as the data itself.
53+
# A simple deparse is sufficient for most logging cases.
54+
log_args_exprs <- c(list(substitute(.value)), other_args_exprs)
55+
}
56+
57+
# For logging, deparse the expressions to get a string representation
58+
# For direct calls, this is perfect. For piped calls, it's an approximation.
59+
mc <- match.call()
60+
mc$.log_df <- NULL
61+
mc[[1]] <- NULL
62+
args <- paste0(sapply(mc, deparse), collapse = ", ")
3363

3464
start <- Sys.time()
3565
pure_f <- purely(.f, strict = strict)
36-
res_pure <- pure_f(.value, ...)
66+
# We pass the evaluated data_val and the unevaluated other args to pure_f
67+
res_pure <- do.call(pure_f, c(list(data_val), func_args))
3768
end <- Sys.time()
3869

39-
input <- .value
70+
input <- data_val
4071
output <- maybe::from_maybe(res_pure$value, default = maybe::nothing())
4172
diff_obj <- switch(
4273
diff,

README.Rmd

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ knitr::opts_chunk$set(
1717

1818

1919
<!-- badges: start -->
20-
[![R-CMD-check](https://github.com/b-rodrigues/chronicler/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/b-rodrigues/chronicler/actions/workflows/R-CMD-check.yaml)
20+
[![R-hub v2](https://github.com/b-rodrigues/chronicler/actions/workflows/rhub.yaml/badge.svg)](https://github.com/b-rodrigues/chronicler/actions/workflows/rhub.yaml/)
2121
[![Codecov test coverage](https://codecov.io/gh/b-rodrigues/chronicler/branch/master/graph/badge.svg)](https://app.codecov.io/gh/b-rodrigues/chronicler?branch=master)
2222
<!-- badges: end -->
2323

@@ -41,12 +41,13 @@ devtools::install_github("b-rodrigues/chronicler")
4141

4242
## Introduction
4343

44-
`{chronicler}` provides the `record()` function, which allows you to modify functions so that they
45-
provide enhanced output. This enhanced output consists in a detailed log, and by chaining decorated
46-
functions, it becomes possible to have a complete trace of the operations that led to the final
47-
output. These decorated functions work exactly the same as their undecorated counterparts, but some
48-
care is required for correctly handling them. This introduction will give you a quick overview of
49-
this package’s functionality.
44+
`{chronicler}` provides the `record()` function, which allows you to modify
45+
functions so that they provide enhanced output. This enhanced output consists in
46+
a detailed log, and by chaining decorated functions, it becomes possible to have
47+
a complete trace of the operations that led to the final output. These decorated
48+
functions work exactly the same as their undecorated counterparts, but some care
49+
is required for correctly handling them. This introduction will give you a quick
50+
overview of this package’s functionality.
5051

5152
Let's first start with a simple example, by decorating the `sqrt()` function:
5253

@@ -113,8 +114,8 @@ b <- 1:10 |>
113114
114115
```
115116

116-
(`bind_record()` is used to chain multiple decorated functions and will be explained in
117-
detail in the next section.)
117+
(`bind_record()` is used to chain multiple decorated functions and will be
118+
explained in detail in the next section.)
118119

119120
```{r}
120121
read_log(b)
@@ -132,9 +133,10 @@ r_exp <- record(exp)
132133
r_mean <- record(mean)
133134
```
134135

135-
you can use the `record_many()` function. `record_many()` takes a list of functions (as strings)
136-
as an input and puts generated code in your system's clipboard. You can then paste the code
137-
into your text editor. The gif below illustrates how `record_many()` works:
136+
you can use the `record_many()` function. `record_many()` takes a list of
137+
functions (as strings) as an input and puts generated code in your system's
138+
clipboard. You can then paste the code into your text editor. The gif below
139+
illustrates how `record_many()` works:
138140

139141
![`record_many()` in action](https://raw.githubusercontent.com/b-rodrigues/chronicler/master/data-raw/record_many.gif)
140142

README.md

Lines changed: 53 additions & 47 deletions
Large diffs are not rendered by default.
64 Bytes
Loading

man/pick.Rd

Lines changed: 0 additions & 24 deletions
This file was deleted.

man/record.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)