Skip to content

Commit 3ba882b

Browse files
committed
Pretty printing
1 parent 2253d9a commit 3ba882b

3 files changed

Lines changed: 73 additions & 15 deletions

File tree

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ S3method(check_constraint_strict,cstr_primary_key)
1414
S3method(check_constraint_strict,cstr_unique_key)
1515
S3method(check_constraints,qf_dataset)
1616
S3method(check_constraints,qf_table)
17+
S3method(print,qf_dataset)
18+
S3method(print,qf_table)
1719
S3method(utils::str,qf_dataset)
1820
S3method(utils::str,qf_table)
1921
S3method(validate_qf_constraint,cstr_foreign_key)

R/qf_dataset.R

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,26 @@ check_constraints.qf_dataset <- function(x) {
6464

6565
#' @noRd
6666
#' @exportS3Method utils::str
67-
str.qf_dataset <- function(object, ...) {
68-
cat("<qf_dataset>\n")
69-
cat("qualifyr data set with ", length(object), " tables: \n", sep = "")
70-
purrr::iwalk(unclass(object), \(table, name) {
67+
str.qf_dataset <- function(object,
68+
nest.lev = 0,
69+
indent.str = paste(rep.int(" ", max(0, nest.lev + 1)), collapse = ".."),
70+
...
71+
) {
72+
cat("<qf_dataset> with ", length(object), " tables: \n", sep = "")
73+
74+
table_names <- format(names(object))
75+
purrr::walk2(object, table_names, \(table, name) {
7176
cat(" $ ", name, ": ", sep = "")
72-
utils::str(table)
77+
utils::str(table, nest.lev = nest.lev + 1, show.context = FALSE, ...)
7378
})
74-
object
79+
80+
invisible(object)
81+
}
82+
83+
#' @noRd
84+
#' @export
85+
print.qf_dataset <- function(x, ...) {
86+
str(x)
7587
}
7688

7789
#' Subsetting qualifyr data sets

R/qf_table.R

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,60 @@ check_constraints.qf_table <- function(x) {
6666

6767
#' @noRd
6868
#' @exportS3Method utils::str
69-
str.qf_table <- function(object, ...) {
70-
cat("<qf_table> [", nrow(object), " x ", ncol(object), "]\n", sep = "")
71-
cat(" Columns: ", paste(colnames(object), collapse = ", "), "\n", sep = "")
72-
cat(
73-
" Constraints: ",
74-
paste(purrr::map(constraints(object),
75-
\(cstr) paste("<", class(cstr)[[1]], ">", sep = "")
76-
), collapse = ", "),
77-
"\n", sep = ""
69+
str.qf_table <- function(object,
70+
show.context = TRUE,
71+
nest.lev = 0,
72+
indent.str = paste(rep.int(" ", max(0, nest.lev + 1)), collapse = ".."),
73+
...
74+
) {
75+
dots <- list(...)
76+
context <- attr(object, "context")
77+
attr(object, "context") <- NULL
78+
79+
# Header
80+
cat("<qf_table>\n")
81+
82+
# Columns / data
83+
cat(indent.str, "- Data:", sep = "")
84+
NextMethod(
85+
nest.lev = nest.lev + 1,
86+
indent.str = paste(rep.int(" ", max(0, nest.lev + 2)),
87+
collapse = ".."),
88+
give.attr = FALSE
7889
)
90+
91+
# Constraints
92+
cat(indent.str, "- Constraints:", "\n", sep = "")
93+
constraint_types <- constraints(object) |>
94+
vapply(pretty_class, character(1)) |>
95+
format()
96+
constraint_cols <- constraints(object) |>
97+
vapply(\(cstr) {
98+
col_names <- paste0("[", paste(cstr$cols, collapse = ", "), "]")
99+
if (inherits(cstr, "cstr_foreign_key")) {
100+
ref_col_names <- paste(cstr$ref_cols, collapse = ", ")
101+
paste0(col_names, " => ", cstr$ref_table, "[", ref_col_names, "]")
102+
} else {
103+
col_names
104+
}
105+
}, character(1))
106+
paste0(indent.str, "..", " - ", constraint_types, " ", constraint_cols) |>
107+
cat(sep = "\n")
108+
109+
# Context
110+
if (show.context && !is.null(context)) {
111+
cat(indent.str, "- Context: ", pretty_class(context), ": ", sep = "")
112+
cat(names(context), sep = ", ")
113+
cat("\n")
114+
}
115+
116+
invisible(object)
117+
}
118+
119+
#' @noRd
120+
#' @export
121+
print.qf_table <- function(x, ...) {
122+
str(x)
79123
}
80124

81125

0 commit comments

Comments
 (0)