Skip to content

Commit

Permalink
ability to add edbl_fns together
Browse files Browse the repository at this point in the history
  • Loading branch information
emitanaka committed Nov 25, 2023
1 parent c3f9f6a commit be32fda
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 83 deletions.
13 changes: 10 additions & 3 deletions R/allot.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
#' @return Return an edibble design.
#' @seealso assign_fcts
#' @export
allot_trts <- function(.edibble, ..., .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = "edbl_fn"))
allot_trts <- function(.edibble = NULL, ..., .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = c("edbl_fn", "edbl")))
if(is_formula(.edibble)) {
cl <- match.call()
ncl <- length(cl)
cl[3:(ncl + 1)] <- cl[2:ncl]
cl$.edibble <- NULL
return(structure(cl, class = "edbl_fn"))
}
dots <- list2(...)
not_edibble(.edibble)
des <- edbl_design(.edibble)
Expand Down Expand Up @@ -153,7 +160,7 @@ allot_units <- function(.edibble, ..., .record = TRUE) {
#'
#' @export
allot_table <- function(.edibble, ..., order = "random", seed = NULL, constrain = nesting_structure(.edibble), label_nested = NULL, fail = "error", .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = "edbl_fn"))
if(is.null(.edibble)) return(structure(match.call(), class = c("edbl_fn", "edbl")))
prov <- activate_provenance(.edibble)
if(.record) prov$record_step()
.edibble %>%
Expand Down
7 changes: 4 additions & 3 deletions R/assign.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#' serve_table()
#' @return An edibble design.
#' @export
assign_trts <- function(.edibble, order = "random", seed = NULL, constrain = nesting_structure(.edibble), ..., .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = "edbl_fn"))
assign_trts <- function(.edibble = NULL, order = "random", seed = NULL, constrain = nesting_structure(.edibble), ..., .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = c("edbl_fn", "edbl")))
not_edibble(.edibble)
force(constrain) # evaluate this now rather than later

Expand Down Expand Up @@ -165,7 +165,8 @@ assign_trts <- function(.edibble, order = "random", seed = NULL, constrain = nes

#' @rdname assign_fcts
#' @export
assign_units <- function(.edibble, order = "random", seed = NULL, constrain = nesting_structure(.edibble), ..., .record = TRUE) {
assign_units <- function(.edibble = NULL, order = "random", seed = NULL, constrain = nesting_structure(.edibble), ..., .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = c("edbl_fn", "edbl")))
not_edibble(.edibble)
prov <- activate_provenance(.edibble)
if(.record) prov$record_step()
Expand Down
4 changes: 2 additions & 2 deletions R/serve.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#' serve_table()
#' @import tidyselect
#' @export
serve_table <- function(.edibble, label_nested = NULL, fail = c("error", "warn", "ignore"), .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = "edbl_fn"))
serve_table <- function(.edibble = NULL, label_nested = NULL, fail = c("error", "warn", "ignore"), .record = TRUE) {
if(is.null(.edibble)) return(structure(match.call(), class = c("edbl_fn", "edbl")))
prov <- activate_provenance(.edibble)
fail <- match.arg(fail)
if(.record) prov$record_step()
Expand Down
175 changes: 104 additions & 71 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,82 +359,115 @@ print.edbl_fct <- function(x, ...) {
cli::cli_abort(c("Cannot use {.code +} with a single argument",
i = "Did you accidentally put {.code +} on a new line?"))
}
prov1 <- activate_provenance(e1)
if(is_edibble_design(e2)) {
prov2 <- activate_provenance(e2)
# add factor nodes and edges from e2
fnodes2 <- prov2$fct_nodes
if(nrow(fnodes2)) {
prov1$append_fct_nodes(name = fnodes2$name,
role = fnodes2$role,
attrs = fnodes2$attrs)
}
fedges2 <- prov2$fct_edges
if(nrow(fedges2)) {
from1 <- prov1$fct_id(name = prov2$fct_names(id = fedges2$from))
to1 <- prov1$fct_id(name = prov2$fct_names(id = fedges2$to))
needs_group_id <- !is.na(fedges2$group)
if(sum(needs_group_id)) {
prov1$append_fct_edges(from = from1[needs_group_id],
to = to1[needs_group_id],
type = fedges2$type[needs_group_id],
group = TRUE,
# FIXME: this needs to be modified if attrs is data.frame!
attrs = fedges2$attrs[needs_group_id])
} else if(sum(!needs_group_id)) {
prov1$append_fct_edges(from = from1[!needs_group_id],
to = to1[!needs_group_id],
type = fedges2$type[!needs_group_id],
group = FALSE,
# FIXME: this needs to be modified if attrs is data.frame!
attrs = fedges2$attrs[!needs_group_id])
if(inherits(e1, "edbl_design")) {
prov1 <- activate_provenance(e1)
if(is_edibble_design(e2)) {
prov2 <- activate_provenance(e2)
# add factor nodes and edges from e2
fnodes2 <- prov2$fct_nodes
if(nrow(fnodes2)) {
prov1$append_fct_nodes(name = fnodes2$name,
role = fnodes2$role,
attrs = fnodes2$attrs)
}
}
# add level nodes and edges from e2
missing_cols <- function(col, df) {
if(col %in% colnames(df)) {
ret <- lnode[[col]]
} else {
ret <- NULL
fedges2 <- prov2$fct_edges
if(nrow(fedges2)) {
from1 <- prov1$fct_id(name = prov2$fct_names(id = fedges2$from))
to1 <- prov1$fct_id(name = prov2$fct_names(id = fedges2$to))
needs_group_id <- !is.na(fedges2$group)
if(sum(needs_group_id)) {
prov1$append_fct_edges(from = from1[needs_group_id],
to = to1[needs_group_id],
type = fedges2$type[needs_group_id],
group = TRUE,
# FIXME: this needs to be modified if attrs is data.frame!
attrs = fedges2$attrs[needs_group_id])
} else if(sum(!needs_group_id)) {
prov1$append_fct_edges(from = from1[!needs_group_id],
to = to1[!needs_group_id],
type = fedges2$type[!needs_group_id],
group = FALSE,
# FIXME: this needs to be modified if attrs is data.frame!
attrs = fedges2$attrs[!needs_group_id])
}
}
# add level nodes and edges from e2
missing_cols <- function(col, df) {
if(col %in% colnames(df)) {
ret <- lnode[[col]]
} else {
ret <- NULL
}
ret
}
ret
}

lnodes2 <- prov2$lvl_nodes
if(length(lnodes2)) {
for(cfid in names(lnodes2)) {
fname <- prov2$fct_names(id = as.numeric(cfid))
fid <- prov1$fct_id(name = fname)
lnode <- lnodes2[[cfid]]
prov1$append_lvl_nodes(value = lnode$value,
n = missing_cols("n", lnode),
attrs = missing_cols("attrs", lnode),
label = missing_cols("label", lnode),
fid = fid)
lnodes2 <- prov2$lvl_nodes
if(length(lnodes2)) {
for(cfid in names(lnodes2)) {
fname <- prov2$fct_names(id = as.numeric(cfid))
fid <- prov1$fct_id(name = fname)
lnode <- lnodes2[[cfid]]
prov1$append_lvl_nodes(value = lnode$value,
n = missing_cols("n", lnode),
attrs = missing_cols("attrs", lnode),
label = missing_cols("label", lnode),
fid = fid)
}
}
}
ledges2 <- lvl_edges(e2)
if(!is.null(ledges2)) {
for(lnode in ledges2) {
from <- prov1$lvl_id(value = lnode$val_from, fid = prov1$fct_id(name = lnode$var_from[1]))
to <- prov1$lvl_id(value = lnode$val_to, fid = prov1$fct_id(name = lnode$var_to[1]))
prov1$append_lvl_edges(from = from,
to = to,
attrs = lnode$attrs)
ledges2 <- lvl_edges(e2)
if(!is.null(ledges2)) {
for(lnode in ledges2) {
from <- prov1$lvl_id(value = lnode$val_from, fid = prov1$fct_id(name = lnode$var_from[1]))
to <- prov1$lvl_id(value = lnode$val_to, fid = prov1$fct_id(name = lnode$var_to[1]))
prov1$append_lvl_edges(from = from,
to = to,
attrs = lnode$attrs)
}
}
}

# TODO: add these components when adding design
# validation,
# anatomy,
# simulate,
# context
e1 <- return_edibble_with_graph(e1, prov1)
des2 <- edbl_design(e2)
for(code in des2$recipe[-1]) e1 <- add_edibble_code(e1, code)
e1
} else if(inherits(e2, "edbl_fn")) {
e2$.edibble <- e1
eval(e2)
# TODO: add these components when adding design
# validation,
# anatomy,
# simulate,
# context
e1 <- return_edibble_with_graph(e1, prov1)
des2 <- edbl_design(e2)
for(code in des2$recipe[-1]) e1 <- add_edibble_code(e1, code)
e1
} else if(inherits(e2, "edbl_fn")) {
e2$.edibble <- e1
eval(e2)
} else if(inherits(e2, "edbl_fns")) {
ret <- e1 + e2[[1]]
if(length(e2) == 1L) return(ret)
e2add <- structure(e2[-1], class = c("edbl_fns", "edbl"))
ret + e2add
} else {
cli::cli_abort(paste("Not sure how to do deal with {.code +} for object of class",
class(e2)[1]))
}
} else if(inherits(e1, "edbl_fn")) {
if(inherits(e2, "edbl_fn")) {
structure(list(e1, e2), class = c("edbl_fns", "edbl"))
} else if(inherits(e2, "edbl_fns")) {
structure(c(list(e1), e2), class = c("edbl_fns", "edbl"))
} else {
cli::cli_abort(paste("Not sure how to do deal with {.code +} for object of class",
class(e2)[1]))
}
} else if(inherits(e1, "edbl_fns")) {
if(inherits(e2, "edbl_fn")) {
structure(c(e1, list(e2)), class = c("edbl_fns", "edbl"))
} else if(inherits(e2, "edbl_fns")) {
structure(c(e1, e2), class = c("edbl_fns", "edbl"))
} else {
cli::cli_abort(paste("Not sure how to do deal with {.code +} for object of class",
class(e2)[1]))
}
} else {
cli::cli_abort(paste("Not sure how to do deal with {.code +} for object of class",
class(e1)[1]))
}
}


2 changes: 1 addition & 1 deletion man/allot_trts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/assign_fcts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/serve_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be32fda

Please sign in to comment.