Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic column resizing in .Rprofile

Tags:

r

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.

like image 933
Chris Neff Avatar asked Jan 19 '23 13:01

Chris Neff


2 Answers

Maybe wrapping the option in a try function helps:

try( options(width=Sys.getenv("COLUMNS")), silent = TRUE)
like image 163
ROLO Avatar answered Jan 21 '23 18:01

ROLO


For me COLUMNS doesn't get updated when my X terminal window (vte based, on linux) gets resized while R is running, since it gets updated by bash after each command. (according to the accepted answer for this question)

I found a hint towards a better solution on this page. It talks about a resize command for solaris, but also mentions stty, which linux does have.

So after reading the man-page (and some basic R questions), I came up with this:

# auto width adjustment
if(interactive()) {
    .adjustWidth <- function(...){
        options('width' = sapply(strsplit(system("stty size", intern = T), " "), "[[", 2))
        TRUE
    }
    .adjustWidthCallBack <- addTaskCallback(.adjustWidth)
}
like image 28
martijn Avatar answered Jan 21 '23 18:01

martijn