@@ -83,7 +83,7 @@ new_cal <- function(x = data.frame(year = integer(0), p = numeric(0)), ...) {
83
83
# }
84
84
85
85
86
- # S3 Methods ---- --------------------------------------------------------------
86
+ # Print methods --------------------------------------------------------------
87
87
88
88
# ' @rdname cal
89
89
# ' @export
@@ -101,7 +101,7 @@ print.cal <- function(x, ...) {
101
101
102
102
cli :: cli_text(" # Calibrated probability distribution from {start} to {end} {era}" )
103
103
cli :: cat_line()
104
- txtplot :: txtplot( x $ year , x $ p , height = 10 )
104
+ cal_txtplot( x )
105
105
cli :: cat_line()
106
106
# TODO: Messy – should probably refactor into its own function
107
107
if (! is.null(metadata $ lab_id )) {
@@ -122,6 +122,68 @@ print.cal <- function(x, ...) {
122
122
invisible (x )
123
123
}
124
124
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
+
125
187
# ' @export
126
188
min.cal <- function (... ) {
127
189
cals <- rlang :: list2(... )
0 commit comments