Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the print for modules to contain more information #1401

Merged
merged 20 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Depends:
teal.slice (>= 0.5.1.9009)
Imports:
checkmate (>= 2.1.0),
crayon,
m7pr marked this conversation as resolved.
Show resolved Hide resolved
vedhav marked this conversation as resolved.
Show resolved Hide resolved
jsonlite,
lifecycle (>= 0.2.0),
logger (>= 0.2.0),
Expand Down
134 changes: 118 additions & 16 deletions R/modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,124 @@ modules <- function(..., label = "root") {
# printing methods ----

#' @rdname teal_modules
#' @param what character vector specifying which metadata to display.
#' Possible values: "data_sources", "properties", "ui_args", "server_args", "transformers"
#' @export
format.teal_module <- function(x, indent = 0, ...) {
paste0(paste(rep(" ", indent), collapse = ""), "+ ", x$label, "\n", collapse = "")
format.teal_module <- function(x, indent = 0, is_last = FALSE, parent_prefix = "", what = c("data_sources", "properties", "ui_args", "server_args", "transformers"), ...) {
vedhav marked this conversation as resolved.
Show resolved Hide resolved
vedhav marked this conversation as resolved.
Show resolved Hide resolved
empty_text <- "─ none"
vedhav marked this conversation as resolved.
Show resolved Hide resolved
branch <- if (is_last) "└─" else "├─"
current_prefix <- paste0(parent_prefix, branch, " ")
content_prefix <- paste0(parent_prefix, if (is_last) " " else "│ ")
m7pr marked this conversation as resolved.
Show resolved Hide resolved

format_list <- function(lst, empty = empty_text) {
if (is.null(lst) || length(lst) == 0) {
empty
} else {
paste(names(lst), collapse = ", ")
}
}

vedhav marked this conversation as resolved.
Show resolved Hide resolved
bookmarkable <- isTRUE(attr(x, "teal_bookmarkable"))
reportable <- "reporter" %in% names(formals(x$server))

transformers <- if (length(x$transformers) > 0) {
paste(sapply(x$transformers, function(t) attr(t, "label")), collapse = ", ")
} else {
empty_text
}
vedhav marked this conversation as resolved.
Show resolved Hide resolved

output <- paste0(current_prefix, crayon::bold(x$label), "\n")

if ("data_sources" %in% what) {
output <- paste0(
output,
content_prefix, "├─ ", crayon::blue("Data Sources:"), " ", paste(x$datanames, collapse = ", "), "\n"
vedhav marked this conversation as resolved.
Show resolved Hide resolved
)
}
if ("properties" %in% what) {
output <- paste0(
vedhav marked this conversation as resolved.
Show resolved Hide resolved
output,
content_prefix, "├─ ", crayon::yellow("Properties:"), "\n",
content_prefix, "│ ├─ Bookmarkable: ", bookmarkable, "\n",
content_prefix, "│ └─ Reportable: ", reportable, "\n"
)
}
if ("ui_args" %in% what) {
output <- paste0(
output,
content_prefix, "├─ ", crayon::green("UI Arguments:"), " ", format_list(x$ui_args), "\n"
vedhav marked this conversation as resolved.
Show resolved Hide resolved
)
}
if ("server_args" %in% what) {
output <- paste0(
output,
content_prefix, "├─ ", crayon::green("Server Arguments:"), " ", format_list(x$server_args), "\n"
vedhav marked this conversation as resolved.
Show resolved Hide resolved
)
}
if ("transformers" %in% what) {
output <- paste0(
output,
content_prefix, "└─ ", crayon::magenta("Transformers:"), " ", transformers, "\n"
)
}
vedhav marked this conversation as resolved.
Show resolved Hide resolved

output
}

#' @rdname teal_modules
#' @export
format.teal_modules <- function(x, indent = 0, is_root = TRUE, is_last = FALSE, parent_prefix = "", ...) {
if (is_root) {
header <- paste0(crayon::bold("TEAL ROOT"), "\n")
new_parent_prefix <- " " # Initial indent for root level
} else {
if (!is.null(x$label)) {
branch <- if (is_last) "└─" else "├─"
header <- paste0(parent_prefix, branch, " ", crayon::bold(x$label), "\n")
vedhav marked this conversation as resolved.
Show resolved Hide resolved
new_parent_prefix <- paste0(parent_prefix, if (is_last) " " else "│ ")
} else {
header <- ""
new_parent_prefix <- parent_prefix
}
}

if (length(x$children) > 0) {
children_output <- character(0)
n_children <- length(x$children)

for (i in seq_along(x$children)) {
child <- x$children[[i]]
is_last_child <- (i == n_children)

if (inherits(child, "teal_modules")) {
children_output <- c(
children_output,
format(child,
indent = indent,
is_root = FALSE,
is_last = is_last_child,
parent_prefix = new_parent_prefix,
...
)
)
} else {
children_output <- c(
children_output,
format(child,
indent = indent,
is_last = is_last_child,
parent_prefix = new_parent_prefix,
...
)
)
}
}

paste0(header, paste(children_output, collapse = ""))
} else {
header
}
}

#' @rdname teal_modules
#' @export
Expand All @@ -334,17 +447,11 @@ print.teal_module <- function(x, ...) {
invisible(x)
}


#' @rdname teal_modules
#' @export
vedhav marked this conversation as resolved.
Show resolved Hide resolved
format.teal_modules <- function(x, indent = 0, ...) {
paste(
c(
paste0(rep(" ", indent), "+ ", x$label, "\n"),
unlist(lapply(x$children, format, indent = indent + 1, ...))
),
collapse = ""
)
print.teal_modules <- function(x, ...) {
cat(format(x, ...))
invisible(x)
}

#' @param modules (`teal_module` or `teal_modules`)
Expand Down Expand Up @@ -380,11 +487,6 @@ set_datanames <- function(modules, datanames) {
modules
}

#' @rdname teal_modules
#' @export
print.teal_modules <- print.teal_module


# utilities ----
## subset or modify modules ----

Expand Down
22 changes: 16 additions & 6 deletions man/teal_modules.Rd

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

Loading