Skip to content

Instantly share code, notes, and snippets.

@thebioengineer
Last active April 14, 2020 18:53
Show Gist options
  • Save thebioengineer/203d3b63829441c2cdaf6e58cbd05b72 to your computer and use it in GitHub Desktop.
Save thebioengineer/203d3b63829441c2cdaf6e58cbd05b72 to your computer and use it in GitHub Desktop.
simple overriding print.default method to prevent accidentally printing large objects
# what object size should be prevented from being printed. Defaults to ~ 30mb, which is not very large
options("print_protect.print_size_bytes" = "30000")
# This method ONLY applies to functions sent to print.default (ie. matrix & vectors)
print.default <- function(x, ...) {
x_bytes <- as.numeric(utils::object.size(x))
answer <- "NA"
if (interactive() &
x_bytes >
as.numeric(getOption("print_protect.print_size_bytes", "300"))) {
answer <-
readline(paste(
"Object to print is",
x_bytes,
"bytes. Are you sure you want to print? [Y/n]"
))
while (!(answer %in% c("Y", "n"))) {
if (answer %in% c("Y", "n")) {
break
}
answer <-
readline(paste(
"Object to print is",
x_bytes,
"bytes. Are you sure you want to print? [Y/n]"
))
}
} else{
answer <- "Y"
}
if (answer == "Y") {
base::print.default(x, ...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment