2

Relevant: Dynamic column resizing in .Rprofile

If I attempt to pass Sys.getenv("COLUMNS") as an integer (or as numeric or as is, i.e., as a string) to the parameter width of options() in my user .Rprofile, I get an error upon startup:

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

However, if, once logged into an interactive session, I try the same manually, no error is thrown and getOption("width") returns a value identical to as.integer(Sys.getenv("COLUMNS")).

I'd like to know why it doesn't work from my .Rprofile, especially since something similar appears in the examples given on the help page for Startup. I am using R in xterm and am on Linux (Arch).

user109114
  • 403
  • 4
  • 10

1 Answers1

3

Maybe the setwidth package can help you: http://cran.r-project.org/web/packages/setwidth/index.html

It works for R running on interactive terminals on Linux (sounds like your case exactly).

You will just have to add library(setwidth) to your .Rprofile and it will change the width every time you resize the terminal.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • This is great! Thanks very much. (I also use and enjoy the **colorout** package cited in the manual.) I'd still like to know why I can't seem to set `options(width = as.integer(Sys.getenv("COLUMNS")))` from my .Rprofile, as in the examples on `?Startup`. – user109114 Oct 27 '14 at 17:52
  • colorout is great too and I am using it. I suspect that upon startup the COLUMNS is not yet set. Maybe you can try to write something like `currentValue <- Sys.getenv("COLUMNS")` in your .Rprofile and see what you get? – Karolis Koncevičius Oct 27 '14 at 18:08
  • You're right: during start-up `Sys.getenv("COLUMNS")` is `""`, which I don't understand, since the shell variable `COLUMNS` is set, i.e., `printf '%s\n' $COLUMNS` returns a value prior to initiating R in the same shell session. – user109114 Oct 27 '14 at 18:58
  • I am not really sure what might be going on with R startup. But maybe it's possible that R is spawning it's own terminal before starting. You can also try running that `printf` from within .Rprofile maybe and see if it is set there. If not - you will probably have to seek another way that does not rely on $COLUMNS initially. – Karolis Koncevičius Oct 27 '14 at 19:08
  • 1
    This solution is superced by base R. You can find a nice solution by Dean Scarff [here](https://scarff.id.au/blog/2021/updating-r-output-width-after-resizing-the-terminal-startup/). – Alan Oct 27 '22 at 11:36