By default, R wraps or truncates its output at 80 columns, making it difficult to read data frames with many columns or with long text values. With a modern computer it’s quite likely your terminal emulator or console is significantly wider than 80 columns.
One apparent solution is:
options(setWidthOnResize = TRUE)
Unfortunately, this only takes effect after resizing the terminal, and not at startup. The R documentation suggests setting the initial width from the COLUMNS
environment variable:
cols <- Sys.getenv("COLUMNS") if(nzchar(cols)) options(width = as.integer(cols))
However, at the time ~/.Rprofile
is evaluated, COLUMNS
is surprisingly empty for me! My guess is that GNU readline initializes it sometime after startup. To work around this, on POSIX platforms we can use the stty
command to query the terminal dimensions:
% /bin/stty size 68 128
So the eventual snippet in my ~/.Rprofile
is:
# Set output width from terminal width. if (interactive() && nzchar(Sys.getenv("TERM"))) local({ # Needs modern readline and R to work. options(setWidthOnResize = TRUE) columns <- Sys.getenv("COLUMNS") if (nzchar(columns)) { options(width = as.integer(columns)) } else { # weirdly, COLUMNS is usually empty at startup, so run a child process. tryCatch({ size <- system2("/bin/stty", "size", stdout = TRUE) width <- as.integer(strsplit(size, " ")[[1]])[2] options(width = width) }, error = function(e) e ) } })