Skip to content

Commit dd293d4

Browse files
committed
continue porting
1 parent 7b4b623 commit dd293d4

File tree

159 files changed

+4408
-1746
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+4408
-1746
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ pkgdown/
1414
^\.here$
1515
^LICENSE\.md$
1616
^codecov\.yml$
17+
^codemeta\.json$

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
.RData
66
.DS_Store
77
.httr-oauth
8+
.Rdata
9+
.quarto
10+
inst/doc

DESCRIPTION

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
Package: chronicler
2-
Version: 0.2.1.9999
32
Title: Add Logging To Functions
3+
Version: 0.2.1.9999
4+
Authors@R: c(
5+
person("Bruno", "Rodrigues", , "[email protected]", role = c("aut", "cre", "cph"),
6+
comment = c(ORCID = "0000-0002-3211-3689")),
7+
person("Matouš", "Eibich", , "[email protected]", role = "ctb")
8+
)
49
Description: Decorate functions to make them return enhanced output. The
5-
enhanced output consists in an object of type 'chronicle' containing the
6-
result of the function applied to its arguments, as well as a log detailing
7-
when the function was run, what were its inputs, what were the errors (if the
8-
function failed to run) and other useful information. Tools to handle
9-
decorated functions are included, such as a forward pipe operator that makes
10-
chaining decorated functions possible.
11-
Authors@R: c(person(given = "Bruno",
12-
family = "Rodrigues",
13-
role = c("aut", "cre", "cph"),
14-
email = "[email protected]",
15-
comment = c(ORCID = "0000-0002-3211-3689")),
16-
person(given = "Matouš",
17-
family = "Eibich",
18-
role = "ctb",
19-
email = "[email protected]"))
10+
enhanced output consists in an object of type 'chronicle' containing
11+
the result of the function applied to its arguments, as well as a log
12+
detailing when the function was run, what were its inputs, what were
13+
the errors (if the function failed to run) and other useful
14+
information. Tools to handle decorated functions are included, such as
15+
a forward pipe operator that makes chaining decorated functions
16+
possible.
2017
License: GPL (>= 3)
18+
Depends:
19+
R (>= 2.10)
20+
Imports:
21+
clipr,
22+
diffobj,
23+
dplyr,
24+
maybe,
25+
rlang,
26+
stringr,
27+
tibble,
28+
utils
29+
Suggests:
30+
knitr,
31+
lubridate,
32+
purrr,
33+
readr,
34+
rmarkdown,
35+
testthat (>= 3.0.0),
36+
tidyr
37+
VignetteBuilder:
38+
knitr
39+
Config/fusen/version: 0.5.2
40+
Config/testthat/edition: 3
2141
Encoding: UTF-8
42+
LazyData: true
2243
Roxygen: list(markdown = TRUE)
2344
RoxygenNote: 7.2.3

NAMESPACE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export(pick)
1313
export(purely)
1414
export(read_log)
1515
export(record)
16-
export(record_ggplot)
1716
export(record_many)
1817
importFrom(clipr,write_clip)
1918
importFrom(diffobj,diffObj)
@@ -23,7 +22,6 @@ importFrom(dplyr,lag)
2322
importFrom(dplyr,mutate)
2423
importFrom(dplyr,row_number)
2524
importFrom(dplyr,select)
26-
importFrom(ggplot2,ggplot_build)
2725
importFrom(maybe,fmap)
2826
importFrom(maybe,from_maybe)
2927
importFrom(maybe,is_nothing)
@@ -40,4 +38,5 @@ importFrom(rlang,quo_get_expr)
4038
importFrom(rlang,try_fetch)
4139
importFrom(stringr,str_remove_all)
4240
importFrom(tibble,tibble)
41+
importFrom(utils,data)
4342
importFrom(utils,tail)

R/.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# WARNING - Generated by {fusen} from dev/flat_pipe.Rmd: do not edit by hand
2+
3+
#' Pipe a chronicle object to a decorated function.
4+
#' @param .c A value returned by record.
5+
#' @param .f A chronicle function to apply to the returning value of .c.
6+
#' @return A chronicle object.
7+
#' @importFrom rlang enquo quo_get_expr quo_get_env call_match call2 eval_tidy
8+
#' @importFrom maybe from_maybe nothing
9+
#' @examples
10+
#' r_sqrt <- record(sqrt)
11+
#' r_exp <- record(exp)
12+
#' 3 |> r_sqrt() %>=% r_exp()
13+
#' @export
14+
`%>=%` <- function(.c, .f) {
15+
16+
f_quo <- rlang::enquo(.f)
17+
f_exp <- rlang::quo_get_expr(f_quo)
18+
f_env <- rlang::quo_get_env(f_quo)
19+
f_chr <- deparse(f_exp[[1]])
20+
21+
f <- get(f_chr, envir = f_env)
22+
23+
q_ex_std <- rlang::call_match(call = f_exp, fn = f)
24+
expr_ls <- as.list(q_ex_std)
25+
26+
# need to set .value to empty, if not .value will be matched multiple times in call2
27+
names(expr_ls)[names(expr_ls) == ".value"] <- ""
28+
29+
rlang::eval_tidy(rlang::call2(f,
30+
.value = maybe::from_maybe(.c$value, default = maybe::nothing()),
31+
!!!expr_ls[-1],
32+
.log_df = .c$log_df))
33+
34+
}
35+

R/bind_record.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# WARNING - Generated by {fusen} from dev/flat_bind_fmap.Rmd: do not edit by hand
2+
3+
#' Evaluate a decorated function; used to chain multiple decorated functions.
4+
#' @param .c A chronicle object.
5+
#' @param .f A chronicle function to apply to the returning value of .c.
6+
#' @param ... Further parameters to pass to .f.
7+
#' @return A chronicle object.
8+
#' @importFrom maybe from_maybe nothing
9+
#' @examples
10+
#' r_sqrt <- record(sqrt)
11+
#' r_exp <- record(exp)
12+
#' 3 |> r_sqrt() |> bind_record(r_exp)
13+
#' @export
14+
bind_record <- function(.c, .f, ...){
15+
16+
.f(maybe::from_maybe(.c$value, default = maybe::nothing()), ..., .log_df = .c$log_df)
17+
18+
}
19+
20+
#' Flatten nested chronicle objects
21+
#' @param .c A nested chronicle object, where the $value element is itself a chronicle object
22+
#' @return Returns `.c` where value is the actual value, and logs are concatenated.
23+
#' @export
24+
#' @examples
25+
#' r_sqrt <- record(sqrt)
26+
#' r_log <- record(log)
27+
#' a <- as_chronicle(r_log(10))
28+
#' a
29+
#' flatten_record(a)
30+
flatten_record <- function(.c){
31+
32+
list(value = .c$value$content$value,
33+
log_df = dplyr::bind_rows(.c$value$log_df,
34+
.c$log_df)) |>
35+
structure(class = "chronicle")
36+
37+
}
38+
39+
#' Evaluate a non-chronicle function on a chronicle object.
40+
#' @param .c A chronicle object.
41+
#' @param .f A non-chronicle function.
42+
#' @param ... Further parameters to pass to `.f`.
43+
#' @importFrom maybe fmap
44+
#' @importFrom dplyr bind_rows
45+
#' @return Returns the result of `.f(.c$value)` as a new chronicle object.
46+
#' @examples
47+
#' as_chronicle(3) |> fmap_record(sqrt)
48+
#' @export
49+
fmap_record <- function(.c, .f, ...){
50+
51+
res_pure <- list("log" = NA,
52+
"value" = NA)
53+
54+
log_df <- make_log_df(
55+
success = 1,
56+
fstring = "fmap_chronicle",
57+
args = NA,
58+
res_pure = res_pure,
59+
start = Sys.time(),
60+
end = Sys.time())
61+
62+
list(value = maybe::fmap(.c$value, .f, ...),
63+
log_df = dplyr::bind_rows(.c$log_df,
64+
log_df)) |>
65+
structure(class = "chronicle")
66+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# WARNING - Generated by {fusen} from dev/flat_data_doc.Rmd: do not edit by hand
2+
3+
#' avia
14
#' Air passenger transport between the main airports of Luxembourg and their main partner airports
25
#'
36
#' A non-tidy dataset from EUROSTAT which can be found [here](https://ec.europa.eu/eurostat/databrowser/bookmark/f9e65cb1-39d2-4b80-9a8b-3b3076b95575?lang=en).
47
#'
5-
#' @format A data frame with 510 rows and 238 columns.
8+
#' @format A data frame with 1,434 rows and 332 columns.
69
"avia"

R/pipe.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# WARNING - Generated by {fusen} from dev/flat_pipe.Rmd: do not edit by hand
2+
3+
#' Pipe a chronicle object to a decorated function.
4+
#' @param .c A value returned by record.
5+
#' @param .f A chronicle function to apply to the returning value of .c.
6+
#' @return A chronicle object.
7+
#' @importFrom rlang enquo quo_get_expr quo_get_env call_match call2 eval_tidy
8+
#' @importFrom maybe from_maybe nothing
9+
#' @examples
10+
#' r_sqrt <- record(sqrt)
11+
#' r_exp <- record(exp)
12+
#' 3 |> r_sqrt() %>=% r_exp()
13+
#' @export
14+
`%>=%` <- function(.c, .f) {
15+
16+
f_quo <- rlang::enquo(.f)
17+
f_exp <- rlang::quo_get_expr(f_quo)
18+
f_env <- rlang::quo_get_env(f_quo)
19+
f_chr <- deparse(f_exp[[1]])
20+
21+
f <- get(f_chr, envir = f_env)
22+
23+
q_ex_std <- rlang::call_match(call = f_exp, fn = f)
24+
expr_ls <- as.list(q_ex_std)
25+
26+
# need to set .value to empty, if not .value will be matched multiple times in call2
27+
names(expr_ls)[names(expr_ls) == ".value"] <- ""
28+
29+
rlang::eval_tidy(rlang::call2(f,
30+
.value = maybe::from_maybe(.c$value, default = maybe::nothing()),
31+
!!!expr_ls[-1],
32+
.log_df = .c$log_df))
33+
34+
}
35+

R/purely.R

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# WARNING - Generated by {fusen} from dev/flat_purely.Rmd: do not edit by hand
2+
3+
#' Capture all errors, warnings and messages.
4+
#' @param .f A function to decorate.
5+
#' @param strict Controls if the decorated function should catch only errors (1), errors and
6+
#' warnings (2, the default) or errors, warnings and messages (3).
7+
#' @return A function which returns a list. The first element of the list, `$value`,
8+
#' is the result of the original function `.f` applied to its inputs. The second element, `$log` is
9+
#' `NULL` in case everything goes well. In case of error/warning/message, `$value` is NA and `$log`
10+
#' holds the message. `purely()` is used by `record()` to allow the latter to handle errors.
11+
#' @importFrom rlang try_fetch eval_tidy cnd_message
12+
#' @importFrom maybe just nothing is_nothing
13+
#' @examples
14+
#' purely(log)(10)
15+
#' purely(log)(-10)
16+
#' purely(log, strict = 1)(-10) # This produces a warning, so with strict = 1 nothing gets captured.
17+
#' @export
18+
purely <- function(.f, strict = 2){
19+
20+
function(.value, ..., .log_df = "Log start..."){
21+
22+
if(maybe::is_nothing(.value)){
23+
24+
final_result <- list(
25+
value = maybe::nothing(),
26+
log_df = "A `Nothing` was given as input."
27+
)
28+
29+
} else {
30+
31+
res <- switch(strict,
32+
only_errors(.f, .value, ...),
33+
errors_and_warnings(.f, .value, ...),
34+
errs_warn_mess(.f, .value, ...))
35+
36+
final_result <- list(
37+
value = NULL,
38+
log_df = NULL
39+
)
40+
41+
final_result$value <- if(any(c("error", "warning", "message") %in% class(res))){
42+
maybe::nothing()
43+
} else {
44+
maybe::just(res)
45+
}
46+
47+
final_result$log_df <- if(any(c("error", "warning", "message") %in% class(res))){
48+
rlang::cnd_message(res)
49+
} else {
50+
NA
51+
}
52+
53+
54+
}
55+
56+
final_result
57+
58+
}
59+
}
60+
61+
62+
63+
#' @noRd
64+
only_errors <- function(.f, ...){
65+
66+
rlang::try_fetch(
67+
rlang::eval_tidy(.f(...)),
68+
error = function(err) err,
69+
)
70+
71+
}
72+
73+
#' @noRd
74+
errors_and_warnings <- function(.f, ...){
75+
76+
rlang::try_fetch(
77+
rlang::eval_tidy(.f(...)),
78+
error = function(err) err,
79+
warning = function(warn) warn,
80+
)
81+
}
82+
83+
#' @noRd
84+
errs_warn_mess <- function(.f, ...){
85+
86+
rlang::try_fetch(
87+
rlang::eval_tidy(.f(...)),
88+
error = function(err) err,
89+
warning = function(warn) warn,
90+
message = function(message) message,
91+
)
92+
}

0 commit comments

Comments
 (0)