Skip to content

Commit 8569415

Browse files
committed
Replace txtplot with an internal text plotting function for cal objects
And drop it as a dependency.
1 parent 0f81a04 commit 8569415

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Imports:
3232
fs,
3333
glue,
3434
cli,
35-
txtplot,
3635
rlang,
3736
stringr,
3837
tidyr,

R/cal.R

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ new_cal <- function(x = data.frame(year = integer(0), p = numeric(0)), ...) {
8383
# }
8484

8585

86-
# S3 Methods ------------------------------------------------------------------
86+
# Print methods --------------------------------------------------------------
8787

8888
#' @rdname cal
8989
#' @export
@@ -101,7 +101,7 @@ print.cal <- function(x, ...) {
101101

102102
cli::cli_text("# Calibrated probability distribution from {start} to {end} {era}")
103103
cli::cat_line()
104-
txtplot::txtplot(x$year, x$p, height = 10)
104+
cal_txtplot(x)
105105
cli::cat_line()
106106
# TODO: Messy – should probably refactor into its own function
107107
if(!is.null(metadata$lab_id)) {
@@ -122,6 +122,68 @@ print.cal <- function(x, ...) {
122122
invisible(x)
123123
}
124124

125+
cal_txtplot <- function(x, height = 8, margin = 2) {
126+
width <- cli::console_width()
127+
if (width > 80) width <- 80
128+
129+
# Plot geometries
130+
geom_area <- cal_txtplot_geom_area(x, width - margin, height - 2)
131+
132+
# Axis & labels
133+
# TODO: Detect direction of year
134+
nbreaks <- floor((width - margin) / (max(nchar(round(x$year))) * 3))
135+
breaks <- pretty(x$year, nbreaks - 1)
136+
while (sum(nchar(breaks)) >= (width - margin)) {
137+
nbreaks <- nbreaks - 1
138+
breaks <- pretty(x$year, nbreaks - 1)
139+
}
140+
xaxis <- cal_txtplot_scale(x$year, breaks, width - margin)
141+
labels <- cal_txtplot_labels(x$year, breaks, width - margin)
142+
143+
# Print
144+
cli::cat_line(stringr::str_pad(geom_area, width, side = "left"))
145+
cli::cat_line(stringr::str_pad(xaxis, width, side = "left"))
146+
cli::cat_line(stringr::str_pad(labels, width, side = "left"))
147+
}
148+
149+
cal_txtplot_geom_area <- function(x, width, height) {
150+
k <- stats::ksmooth(x$year, x$p,
151+
bandwidth = abs(max(x$year) - min(x$year)) / width,
152+
n.points = width)
153+
k$y[is.na(k$y)] <- 0
154+
k$y <- round((k$y / max(k$y)) * height)
155+
156+
stringr::str_dup("#", k$y) %>%
157+
stringr::str_pad(height, side = "left") %>%
158+
stringr::str_split(pattern = "", simplify = TRUE) %>%
159+
apply(2, paste0, collapse = "")
160+
}
161+
162+
cal_txtplot_scale <- function(x, breaks, width) {
163+
breakpoints <- cal_txtplot_breakpoints(x, breaks, width)
164+
165+
axis <- rep("-", width)
166+
axis[breakpoints] <- "|"
167+
paste(axis, collapse = "")
168+
}
169+
170+
cal_txtplot_labels <- function(x, breaks, width) {
171+
breakpoints <- cal_txtplot_breakpoints(x, breaks, width)
172+
173+
labels <- stringr::str_pad(breaks[-1], c(diff(breakpoints)), side = "left")
174+
paste(labels, collapse = "")
175+
}
176+
177+
cal_txtplot_breakpoints <- function(x, breaks, width) {
178+
if (x[1] > x[length(x)]) {
179+
x <- -x
180+
breaks <- -breaks
181+
}
182+
sort(round(findInterval(breaks, x) / length(x) * width))
183+
}
184+
185+
# S3 Methods ------------------------------------------------------------------
186+
125187
#' @export
126188
min.cal <- function(...) {
127189
cals <- rlang::list2(...)

0 commit comments

Comments
 (0)