Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does RStudio determine the console width, and why does it seem to be getting it consistently wrong?

Tags:

r

rstudio

I just discovered wid <- options()$width in RStudio, and it seems to be the source (or rather, much closer to the source) of much irritation in my everyday console usage. I should say up front that I'm currently on R 3.2.2, RStudio 0.99.491, on Linux Mint 17.3 (built over Ubuntu 14.04.3 LTS)

As I understand it, wid should be measured in characters -- if wid is equal to 52, say, then one should be able to fit the alphabet on the screen twice (given the fixed-width default font), but this doesn't appear to be the case:

A screenshot showing the observed-vs-actual width discrepancy. The first line shows a call to options()$width and its output (52); the second line types out the alphabet twice, but the second time only makes it to 't' (if the true width was 52, the second alphabet should complete)

As you can see, despite having wid equal to 52, I am unable to fit the alphabet twice -- I come up 6 characters short. I also note that this means it is not simply due to the presence of the command prompt arrow and space (> ).

The problem seems somewhat proportional -- if I have wid up to 78, I can only fit 70 characters; up to 104, 93, so wid is about 88% off pretty consistently (side note: this also suggests my assumption wid is measured in characters is probably right).

The problem that this engenders is that oftentimes console output overflows beyond its intended line, making the output ugly and hard to digest; take, for example, the simple snipped setDT(lapply(1:30, function(x) 1:3))[] which produces for me:

A screenshot of garbled output. A 3-row, 27-colum data.table is printed, but columns 26 and 27 interweave with the other columns, instead of being put alongside the next register of output where columns 28-30 ended up.

It seems clear to me that the output was attempted on a screen width which was not available in practice -- that internally, a larger screen width than actually exists was used for printing.

This leaves me with three questions:

  1. How is options()$width determined?
  2. Why is it so consistently wrong?
  3. What can we do to override this error?
like image 359
MichaelChirico Avatar asked Nov 08 '22 20:11

MichaelChirico


1 Answers

Found a post about this on Rstudio support and it seems the issue has to do with high DPI Displays; there is a claimed bug fix in RStudio version 0.99.878 (released just today! as luck would have it), according to the release notes:

Bug Fixes

...

  • Correct computation of getOption(“width”) on high DPI displays

Hope this helps anyone else experiencing this! I'm tempted to post about this on /r/oddlysatisfying B-)

Would love to see the relevant commit on the RStudio GitHub page if anyone can track it down (I had no luck).

like image 102
MichaelChirico Avatar answered Nov 15 '22 06:11

MichaelChirico