HenrikBengtsson / Wishlist-for-R

Features and tweaks to R that I and others would love to see - feel free to add yours!
https://github.com/HenrikBengtsson/Wishlist-for-R/issues
GNU Lesser General Public License v3.0
133 stars 4 forks source link

WISH: Customizable print method for character vector with long text #163

Open chainsawriot opened 4 months ago

chainsawriot commented 4 months ago

The print method of character vector has been a major issue when text analysts deal with large text datasets.

To illustrate the issue:

gibberish <- paste0(sample(c(LETTERS, letters, " "), 100000, replace = TRUE), collapse = "")
rep(gibberish, 10000)

The printing of the vector with very long texts blocks the R interpreter [^1]. As far as I know, there is no way to override the default printing method of character vector.

Of course, the community comes up with different solutions, e.g.

gibberish <- paste0(sample(c(LETTERS, letters, " "), 100000, replace = TRUE), collapse = "")
tibble::tibble(rep(gibberish, 10000))
gibberish <- paste0(sample(c(LETTERS, letters, " "), 100000, replace = TRUE), collapse = "")
quanteda::corpus(rep(gibberish, 10000))

However, it would be good if there is a way to customize the print method of character vector, so that it can truncate the printing of very long texts.

[^1]: In this case, it will print ultimately because the length of the vector is not that long. But for real life datasets, it blocks the interpreter so that one has to kill it. print(max = 10) works to cut the number of items to be printed, but not the length of the text itself. object.size(gibberish) in this case is just 98KB, real life text data can easily be a few MB per item.