Skip to content

Commit

Permalink
band aid for characters with unknown encodings
Browse files Browse the repository at this point in the history
closes #12
  • Loading branch information
MilesMcBain committed Nov 13, 2022
1 parent 7c81835 commit a7e97dd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion 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.6
Version: 0.1.7
Authors@R:
person(given = "Miles",
family = "McBain",
Expand All @@ -24,6 +24,7 @@ Imports:
pryr,
purrr,
RColorBrewer,
stringi,
utils,
vctrs,
viridisLite
Expand Down
5 changes: 2 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# 0.3.2
# 0.1.7

* Minor usability fix for RStudio
* Make compatible with more strict type checking in dev `{dplyr}`.
* Handle characters with invalid encoding by attempting conversion with `stringi::stri_trans_general`. This will result invalid characters being replaces by a missing char question mark. Previously these crashed out `{paint}`, failing in `{crayon}`.

# 0.1.6

Expand Down
7 changes: 4 additions & 3 deletions R/paint.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ paint.data.frame <- function(
utils::head(
offset(df, start_row),
getOption("paint_n_rows", length(palette))
)
)
cols <- mapply(
paint_col,
rows_to_paint,
Expand All @@ -61,10 +61,11 @@ paint.data.frame <- function(
col_names <- align_str(col_names)
col_types <- align_str(col_types)
}
col_lines <- paste0(col_names, " ", col_types, " ", cols)
sanitised_cols <- vapply(cols, sanitise_col, character(1))
col_lines <- paste0(col_names, " ", col_types, " ", sanitised_cols)
cropped_lines <- crop_lines(col_lines, getOption("paint_max_width", 60))
col_block <- paste0(
sanitise_text(cropped_lines),
cropped_lines,
collapse = "\n"
)
name <- paint_name(name)
Expand Down
7 changes: 7 additions & 0 deletions R/sanitise_col.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' Attempt to transform chars in encodings we don't understand, rather than crash crayon.
#' @author Miles McBain
sanitise_col <- function(col) {
sanitise_text(
ifelse(!validEnc(col), stringi::stri_trans_general(col, "latin-ascii"), col)
)
}
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/invalid-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# invalid encodings are handled

Code
paint(df)
Output
data.frame [1, 3]
bad chr 23�C
good chr dummy
the_worst chr 2015 President and Vice-Chancellor�s Alumni S~

9 changes: 9 additions & 0 deletions tests/testthat/test-invalid-encoding.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("invalid encodings are handled", {
df <- data.frame(
bad = "23\xbfC",
good = "dummy",
the_worst = "2015 President and Vice-Chancellor\x92s Alumni Scholarship Appeal"
)

expect_snapshot(paint(df))
})

0 comments on commit a7e97dd

Please sign in to comment.