Skip to content

Commit

Permalink
need to fix pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues committed Feb 14, 2024
1 parent e24690c commit 63bb16a
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ Imports:
dplyr,
ggplot2,
maybe,
purrr,
rlang,
stringr,
tibble,
utils
Suggests:
knitr,
lubridate,
purrr,
rmarkdown,
testthat (>= 3.0.0),
tidyr
Expand Down
4 changes: 2 additions & 2 deletions R/pipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
expr_ls <- as.list(q_ex_std)

# need to set .value to empty, if not .value will be matched multiple times in call2
names(expr_ls)[names(expr_ls) == ".value"] <- ""
names(expr_ls)[names(expr_ls) == "..1"] <- ""

rlang::eval_tidy(rlang::call2(f,
.value = maybe::from_maybe(.c$value, default = maybe::nothing()),
..1 = maybe::from_maybe(.c$value, default = maybe::nothing()),
!!!expr_ls[-1],
.log_df = .c$log_df))

Expand Down
24 changes: 12 additions & 12 deletions R/purely.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
#' @export
purely <- function(.f, strict = 2){

function(.value, ..., .log_df = "Log start..."){

if(maybe::is_nothing(.value)){

final_result <- list(
value = maybe::nothing(),
log_df = "A `Nothing` was given as input."
)

function(..., .log_df = "Log start..."){

if(any(purrr::map_lgl(list(...), maybe::is_nothing))){
final_result <- list(
value = maybe::nothing(),
log_df = "A `Nothing` was given as input."
)
} else {

res <- switch(strict,
only_errors(.f, .value, ...),
errors_and_warnings(.f, .value, ...),
errs_warn_mess(.f, .value, ...))
only_errors(.f, ...),
errors_and_warnings(.f, ...),
errs_warn_mess(.f, ...))

final_result <- list(
value = NULL,
Expand Down
7 changes: 4 additions & 3 deletions R/record.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ record <- function(.f, .g = (\(x) NA), strict = 2, diff = "none"){

fstring <- deparse1(substitute(.f))

function(.value, ..., .log_df = data.frame()){
function(..., .log_df = data.frame()){

args <- paste0(rlang::enexprs(...), collapse = ",")

start <- Sys.time()
pure_f <- purely(.f, strict = strict)
res_pure <- (pure_f(.value, ...))
res_pure <- (pure_f(...))
end <- Sys.time()

input <- .value
input <- "hu"
#input <- .value
output <- maybe::from_maybe(res_pure$value, default = maybe::nothing())
diff_obj <- switch(diff,
"none" = NULL,
Expand Down
9 changes: 9 additions & 0 deletions dev/0-dev_history.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ fusen::inflate(
)
```

```{r}
fusen::inflate(
flat_file = "dev/flat_named_args.Rmd",
vignette_name = NA,
overwrite = TRUE,
check = FALSE
)
```

```{r}
fusen::inflate(
flat_file = "dev/flat_ggplot.Rmd",
Expand Down
13 changes: 13 additions & 0 deletions dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ flat_ggplot.Rmd:
check: false
document: true
overwrite: 'yes'
flat_named_args.Rmd:
path: dev/flat_named_args.Rmd
state: active
R: []
tests: tests/testthat/test-named_args.R
vignettes: []
inflate:
flat_file: dev/flat_named_args.Rmd
vignette_name: .na
open_vignette: true
check: false
document: true
overwrite: 'yes'
flat_pipe.Rmd:
path: dev/flat_pipe.Rmd
state: active
Expand Down
7 changes: 4 additions & 3 deletions dev/flat_chronicle.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ record <- function(.f, .g = (\(x) NA), strict = 2, diff = "none"){
fstring <- deparse1(substitute(.f))
function(.value, ..., .log_df = data.frame()){
function(..., .log_df = data.frame()){
args <- paste0(rlang::enexprs(...), collapse = ",")
start <- Sys.time()
pure_f <- purely(.f, strict = strict)
res_pure <- (pure_f(.value, ...))
res_pure <- (pure_f(...))
end <- Sys.time()
input <- .value
input <- "hu"
#input <- .value
output <- maybe::from_maybe(res_pure$value, default = maybe::nothing())
diff_obj <- switch(diff,
"none" = NULL,
Expand Down
75 changes: 75 additions & 0 deletions dev/flat_named_args.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: "Named args tests"
output: html_document
editor_options:
chunk_output_type: console
---

```{r development, include=FALSE}
library(testthat)
```

This vignette is only for defining some tests.


```{r tests-purely_decorated_args, filename = "named_args.R"}
test_that("purely decorated function with named args works", {
pmean <- purely(mean)
arg <- c(1, 2, 3)
expect_equal(pmean(arg), pmean(x = arg))
})
test_that("purely decorated function, we can name many arguments", {
soma <- function(x, y){
res <- x+y
return(res)
}
psoma <- purely(soma)
expect_equal(psoma(2, 6), psoma(2, y = 6))
expect_equal(psoma(2, 6), psoma(x = 2, y = 6))
})
```

```{r tests-record_decorated_args, filename = "named_args.R"}
test_that("record decorated function with named args works", {
rmean <- record(mean)
arg <- c(1, 2, 3)
expect_equal(rmean(arg)$value, rmean(x = arg)$value)
})
test_that("record decorated function, we can name many arguments", {
soma <- function(x, y){
res <- x+y
return(res)
}
rsoma <- record(soma)
expect_equal(rsoma(2, 6)$value, rsoma(2, y = 6)$value)
expect_equal(rsoma(2, 6)$value, rsoma(x = 2, y = 6)$value)
})
```
5 changes: 3 additions & 2 deletions dev/flat_pipe.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ editor_options:
q_ex_std <- rlang::call_match(call = f_exp, fn = f)
expr_ls <- as.list(q_ex_std)
browser()
# need to set .value to empty, if not .value will be matched multiple times in call2
names(expr_ls)[names(expr_ls) == ".value"] <- ""
#names(expr_ls)[names(expr_ls) == "..1"] <- ""
rlang::eval_tidy(rlang::call2(f,
.value = maybe::from_maybe(.c$value, default = maybe::nothing()),
..1 = maybe::from_maybe(.c$value, default = maybe::nothing()),
!!!expr_ls[-1],
.log_df = .c$log_df))
Expand Down
59 changes: 59 additions & 0 deletions tests/testthat/test-named_args.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# WARNING - Generated by {fusen} from dev/flat_named_args.Rmd: do not edit by hand

test_that("purely decorated function with named args works", {

pmean <- purely(mean)

arg <- c(1, 2, 3)

expect_equal(pmean(arg), pmean(x = arg))

})


test_that("purely decorated function, we can name many arguments", {

soma <- function(x, y){

res <- x+y

return(res)
}

psoma <- purely(soma)

expect_equal(psoma(2, 6), psoma(2, y = 6))

expect_equal(psoma(2, 6), psoma(x = 2, y = 6))

})


test_that("record decorated function with named args works", {

rmean <- record(mean)

arg <- c(1, 2, 3)

expect_equal(rmean(arg)$value, rmean(x = arg)$value)

})


test_that("record decorated function, we can name many arguments", {

soma <- function(x, y){

res <- x+y

return(res)
}

rsoma <- record(soma)

expect_equal(rsoma(2, 6)$value, rsoma(2, y = 6)$value)

expect_equal(rsoma(2, 6)$value, rsoma(x = 2, y = 6)$value)

})

0 comments on commit 63bb16a

Please sign in to comment.