Chris Neff Chris Neff - 1 month ago 6
R Question

Dynamic column resizing in .Rprofile

I'd like to put the following in my .Rprofile:

# auto width adjustment
.adjustWidth <- function(...){
options(width=Sys.getenv("COLUMNS"))
TRUE
}
.adjustWidthCallBack <- addTaskCallback(.adjustWidth)


This will dynamically resize the max columns in my R session to be the width of the window. This works in an interactive session, but when doing something like R CMD INSTALL or a batch session I always get:

Error in options(width = Sys.getenv("COLUMNS")) :
invalid 'width' parameter, allowed 10...10000
Execution halted


How can I fix this? I assume the issue is that Sys.getenv("COLUMNS") is failing somehow? Is there some if() statement I could do that lets me detect if I run in batch or not? The original auto width adjustment code isn't mine, I found it somewhere else online.

Answer

Maybe wrapping the option in a try function helps:

try( options(width=Sys.getenv("COLUMNS")), silent = TRUE)