Skip to content

Commit

Permalink
enhanced version of ipaint()
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesMcBain committed Mar 3, 2022
1 parent 8ca7c70 commit 9445b91
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 165 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: paint
Title: paint data.frames summaries in colour
Version: 0.1.2
Version: 0.1.3
Authors@R:
person(given = "Miles",
family = "McBain",
Expand All @@ -13,7 +13,7 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
URL: https://github.com/MilesMcBain/paint-
BugReports: https://github.com/MilesMcBain/paint-/issues
Imports:
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 0.1.3

* Enhanced version of `ipaint()` for interactively scrollable painted output. It no longer fills the terminal with junk. It's a nice terminal-only competitor to `View()`
73 changes: 12 additions & 61 deletions R/ipaint.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#'
#' It will only work in terminals supported by {keypress} - Not many!
#'
#' In order to paint the `df` in the same place each time this function outputs a
#' whole lot of newlines in between paints. This will eat up your terminal scroll buffer,
#' so DO NOT use this if there's information currently in the terminal that is important to you.
#'
#' @param df the dataframe to scroll through, defaults to `.Last.value`
#' @export
ipaint <- function(df = .Last.value) {
Expand All @@ -28,22 +24,21 @@ ipaint <- function(df = .Last.value) {
row <- min(nrow(df), row + 1)
if (row > old_row) palette <- rotate_palette_forward(palette)
}
paint_to_buffer(df, palette = palette, start_row = row)
row_footer_to_buffer(row, df, palette)
paint_to_output_buffer(df, palette = palette, start_row = row)
row_footer_to_output_buffer(row, df, palette)

if (key != "") {
buffer_size <- get_cat_buffer_rows()
cat(sprintf("\033[%dA\r", buffer_size - 1))
buffer_size <- get_output_buffer_rows()
cat(sprintf("\033[%dA\r", buffer_size - 1)) # back to first row
cat(blank_buffer(buffer_size))
cat(sprintf("\033[%dA\r", buffer_size))
cat(sprintf("\033[%dA\r", buffer_size - 1))
}

cat(get_cat_buffer(), sep = "")
reset_cat_buffer()
cat(get_output_buffer(), sep = "")
reset_output_buffer()

key <- keypress::keypress()
}
cat("\n")
}

rotate_palette_forward <- function(palette) {
Expand All @@ -54,7 +49,7 @@ rotate_palette_backward <- function(palette) {
c(palette[[length(palette)]], palette[1:(length(palette) - 1)])
}

row_footer_to_buffer <- function(row, df, palette) {
row_footer_to_output_buffer <- function(row, df, palette) {
min_row <- row
row_width <- getOption("paint_n_rows", length(palette))
max_row <- min(nrow(df), (min_row + row_width) - 1)
Expand All @@ -67,61 +62,17 @@ row_footer_to_buffer <- function(row, df, palette) {
)
instructions <-
crayon::silver("scroll with <-,h,l,-> or Enter to exit")
append_cat_buffer(

append_to_output_buffer(
"\n",
"\n",
row_message,
"\n",
instructions
instructions,
"\n"
)
}

capture_cat_buffer <- function(..., sep = "") {
cat_buffer <<-
c(
cat_buffer,
unlist(list(...))
)
}

reset_cat_buffer <- function() {
cat_buffer <<- NULL
}

get_cat_buffer <- function() {
cat_buffer
}

get_cat_buffer_rows <- function() {
newlines <-
lapply(
gregexpr("(?<!\\\\)\n", get_cat_buffer(), perl = TRUE),
function(match) {
if (length(match) == 1 && match == -1) 0 else length(match)
}
) |>
unlist() |>
sum()

newlines
}

append_cat_buffer <- function(...) {
cat_buffer <<-
c(
cat_buffer,
unlist(list(...))
)
}

cat_buffer <- NULL

paint_to_buffer <- function(...) {
assign("cat", capture_cat_buffer, environment(paint))
paint(...)
remove("cat", envir = environment(paint))
}

blank_buffer <- function(n_rows) {
line <- strrep(" ", getOption("paint_max_width", 60))
paste0(rep(line, n_rows), collapse = "\n")
Expand Down
46 changes: 46 additions & 0 deletions R/output.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
OUTPUT_TO_BUFFER <- FALSE
OUTPUT_BUFFER <- NULL

output <- function(...) {
if (!OUTPUT_TO_BUFFER) {
cat(..., sep = "")
} else {
append_to_output_buffer(...)
}
}

append_to_output_buffer <- function(...) {
OUTPUT_BUFFER <<-
c(
OUTPUT_BUFFER,
unlist(list(...))
)
}

reset_output_buffer <- function() {
OUTPUT_BUFFER <<- NULL
}

get_output_buffer <- function() {
OUTPUT_BUFFER
}

get_output_buffer_rows <- function() {
newlines <-
lapply(
gregexpr("(?<!\\\\)\n", get_output_buffer(), perl = TRUE),
function(match) {
if (length(match) == 1 && match == -1) 0 else length(match)
}
) |>
unlist() |>
sum()

newlines + 1
}

paint_to_output_buffer <- function(...) {
OUTPUT_TO_BUFFER <<- TRUE
on.exit(OUTPUT_TO_BUFFER <<- FALSE)
paint(...)
}
10 changes: 5 additions & 5 deletions R/paint.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ paint.data.frame <- function(
...
) {
if (ncol(df) == 0) {
cat(paint_head(df), "\n", sep = "")
output(paint_head(df), "\n")
return(invisible(df))
}
col_types <- lapply(df, paint_col_type)
Expand Down Expand Up @@ -70,15 +70,15 @@ paint.data.frame <- function(
name <- paint_name(name)
header <- trimws(paste(name, paint_head(df))) # the name is used for nested data.frames
meta <- paint_meta(df)
cat(header, "\n")
if (!is.null(meta)) cat(meta, "\n")
cat(col_block, "\n")
output(header, "\n")
if (!is.null(meta)) output(meta, "\n")
output(col_block, "\n")
# paint nested data frames
nested_data_frame_idxs <- which(trimws(crayon::strip_style(col_types)) == "df")
lapply(
nested_data_frame_idxs,
function(idx) {
cat("\n")
output("\n")
paint(df[[idx]], name = names(df)[idx], palette = palette)
}
)
Expand Down
4 changes: 0 additions & 4 deletions man/ipaint.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/_snaps/dataframe.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Code
paint(data.frame(cool = c("a", NA, "c"), stuff = c(1, -Inf, Inf)))
Output
[90mdata.frame[39m [90m[3, 2][39m
[90mdata.frame[39m [90m[3, 2][39m
cool chr a NA c
stuff [90mdbl[39m [31m1[39m [33m[7m-Inf[27m[39m [36m[7mInf[27m[39m
stuff [90mdbl[39m [31m1[39m [33m[7m-Inf[27m[39m [36m[7mInf[27m[39m

---

Expand All @@ -16,13 +16,13 @@
"2013-01-01 05:00:00 EST", "2013-01-01 05:00:00 EST")), complex_col = c(0 +
0+1i, 1 + 0+2i, 2 + 0+3i)))
Output
[90mdata.frame[39m [90m[3, 6][39m
[90mdata.frame[39m [90m[3, 6][39m
chr_col chr a b c
raw_col raw 61 62 63
int_col int 1 2 3
dbl_col dbl 1 2 3
datetime_col dttm 2013-01-01 05:00:00 2013-01-01 05:00:00 2~
complex_col [90mcplx[39m [31m0+1i[39m [33m1+2i[39m [36m2+3i[39m
complex_col [90mcplx[39m [31m0+1i[39m [33m1+2i[39m [36m2+3i[39m

---

Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/datatable.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
Code
paint(pp_dt)
Output
[90mdata.table[39m [90m[344, 8][39m
[90mdata.table[39m [90m[344, 8][39m
species fct Adelie Adelie Adelie Adelie Adelie Ad~
island fct Torgersen Torgersen Torgersen Torgers~
bill_length_mm dbl 39.1 39.5 40.3 NA 36.7 39.3
bill_depth_mm dbl 18.7 17.4 18 NA 19.3 20.6
flipper_length_mm int 181 186 195 NA 193 190
body_mass_g int 3750 3800 3250 NA 3450 3650
sex fct male female female NA female male
year [90mint[39m [31m2007[39m [33m2007[39m [36m2007[39m [32m2007[39m [35m2007[39m [34m2007[39m
year [90mint[39m [31m2007[39m [33m2007[39m [36m2007[39m [32m2007[39m [35m2007[39m [34m2007[39m

---

Code
paint(pp_dt_keyed)
Output
[90mdata.table[39m [90m[344, 8][39m
[90mkeys: body_mass_g, flipper_length_mm[39m
[90mdata.table[39m [90m[344, 8][39m
[90mkeys: body_mass_g, flipper_length_mm[39m
species fct Adelie Gentoo Chinstrap Adelie Adelie~
island fct Torgersen Biscoe Dream Biscoe Biscoe ~
bill_length_mm dbl NA NA 46.9 36.5 36.4 33.1
bill_depth_mm dbl NA NA 16.6 16.6 17.1 16.1
flipper_length_mm int NA NA 192 181 184 178
body_mass_g int NA NA 2700 2850 2850 2900
sex fct NA NA female female female female
year [90mint[39m [31m2007[39m [33m2009[39m [36m2008[39m [32m2008[39m [35m2008[39m [34m2008[39m
year [90mint[39m [31m2007[39m [33m2009[39m [36m2008[39m [32m2008[39m [35m2008[39m [34m2008[39m

---

Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/_snaps/mask_print.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Code
mtcars
Output
[90mdata.frame[39m [90m[32, 11][39m
[90mdata.frame[39m [90m[32, 11][39m
mpg dbl 21 21 22.8 21.4 18.7 18.1
cyl dbl 6 6 4 6 8 6
disp dbl 160 160 108 258 360 225
Expand All @@ -14,54 +14,54 @@
vs dbl 0 0 1 1 0 1
am dbl 1 1 1 0 0 0
gear dbl 4 4 4 3 3 3
carb [90mdbl[39m [31m4[39m [33m4[39m [36m1[39m [32m1[39m [35m2[39m [34m1[39m
carb [90mdbl[39m [31m4[39m [33m4[39m [36m1[39m [32m1[39m [35m2[39m [34m1[39m

---

Code
palmerpenguins::penguins
Output
[90mtibble[39m [90m[344, 8][39m
[90mtibble[39m [90m[344, 8][39m
species fct Adelie Adelie Adelie Adelie Adelie Ad~
island fct Torgersen Torgersen Torgersen Torgers~
bill_length_mm dbl 39.1 39.5 40.3 NA 36.7 39.3
bill_depth_mm dbl 18.7 17.4 18 NA 19.3 20.6
flipper_length_mm int 181 186 195 NA 193 190
body_mass_g int 3750 3800 3250 NA 3450 3650
sex fct male female female NA female male
year [90mint[39m [31m2007[39m [33m2007[39m [36m2007[39m [32m2007[39m [35m2007[39m [34m2007[39m
year [90mint[39m [31m2007[39m [33m2007[39m [36m2007[39m [32m2007[39m [35m2007[39m [34m2007[39m

---

Code
spData::nz
Output
[90msf[39m [90m[16, 7][39m
[90msf[39m [90m[16, 7][39m
active geometry column: geom (MULTIPOLYGON)
crs: 2193 (NZGD2000 / New Zealand Transverse Mercator 2000)
crs unit: metre[39m
crs unit: metre[39m
Name chr Northland Auckland Waikato Bay of Plenty ~
Island chr North North North North North North
Land_area dbl 12500.561149 4941.572557 23900.036383 120~
Population dbl 175500 1657200 460100 299900 48500 164000
Median_income int 23400 29600 27900 26200 24400 26100
Sex_ratio dbl 0.942453 0.944286 0.95205 0.928039 0.9349~
geom [90msfc[39m [31mMPOLY 1,784B[39m [33mMPOLY 2,288B[39m [36mMPOLY 2,376B[39m [32mMP[39m[35m[39m[34m[39m[1m~[22m
geom [90msfc[39m [31mMPOLY 1,784B[39m [33mMPOLY 2,288B[39m [36mMPOLY 2,376B[39m [32mMP[39m[35m[39m[34m[39m[1m~[22m

---

Code
data.table::as.data.table(palmerpenguins::penguins)
Output
[90mdata.table[39m [90m[344, 8][39m
[90mdata.table[39m [90m[344, 8][39m
species fct Adelie Adelie Adelie Adelie Adelie Ad~
island fct Torgersen Torgersen Torgersen Torgers~
bill_length_mm dbl 39.1 39.5 40.3 NA 36.7 39.3
bill_depth_mm dbl 18.7 17.4 18 NA 19.3 20.6
flipper_length_mm int 181 186 195 NA 193 190
body_mass_g int 3750 3800 3250 NA 3450 3650
sex fct male female female NA female male
year [90mint[39m [31m2007[39m [33m2007[39m [36m2007[39m [32m2007[39m [35m2007[39m [34m2007[39m
year [90mint[39m [31m2007[39m [33m2007[39m [36m2007[39m [32m2007[39m [35m2007[39m [34m2007[39m

---

Expand Down
Loading

0 comments on commit 9445b91

Please sign in to comment.