4

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.

Chris Neff
  • 215
  • 1
  • 2
  • 6
  • There's also `interactive()`, which tests whether R is being used interactively or not; e.g., `if (interactive()) .adjustWidth <- function (...)` etc. – user109114 Oct 27 '14 at 15:56

3 Answers3

7

Maybe wrapping the option in a try function helps:

try( options(width=Sys.getenv("COLUMNS")), silent = TRUE)
ROLO
  • 4,183
  • 25
  • 41
1

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)
}
Community
  • 1
  • 1
martijn
  • 43
  • 7
1

The interactive() check is VERY IMPORTANT: otherwise it doesn't work with update.packages()...

> update.packages(lib.loc="...",ask=FALSE,oldPkgs="httpuv")
trying URL 'http://cran.univ-lyon1.fr/src/contrib/httpuv_1.5.1.tar.gz'
...
stty: 'standard input': Inappropriate ioctl for device
Error in options(width = as.integer(howWide)) : 
  invalid 'width' parameter, allowed 10...10000
Calls: updatePrompt -> options
Execution halted
...
liar
  • 11
  • 1