Skip to content

Smart detection of wrap width?

10 messages · Ista Zahn, Peter Crowther, Paul Bivand +1 more

#
I suspect that the answer is "no", but just in case, is there a way to
have R detect the window width and wrap accordingly?  I don't want to
set an option every time I dock a window or shrink it.

In my ideal paradise, R would not only format output according to
window width, but it would also me to specify a virtual window width.
So if I specify a virtual window width of 200 characters, and the
actual window width is 100, then the output has a carriage return at
the 200 character mark but I only see the 1st 100 characters of each
line of text (the rest being truncated from view).  And of course, the
user would be able to specify when the virtual width mirrors the
actual window width.

I know that this is dreaming in technicolour (which is not that
fantastical these days), but I would be pleasantly surprised if there
was a way to simply have the output wrap according to the actual
window width.
#
A lot of this depends on what context you are running R in, e.g., Windows
console, Mac console, or command line in a unix-alike. Or within ESS in
emacs. Those are different interfaces supported by, to some extent,
different people, and are based on the underlying capabilities provided by
the operating system.

Have you yet encountered
  options()$width
?
For example,
  options(width=100)
will cause wrapping at 100, at least for certain kinds of output.

In an xterm shell running in an X windows context, I frequently use

setwid <- function ()
{
    if (!interactive())
        return(invisible(NULL))
    scon <- pipe("stty -a")
    stty <- scan(scon, what = "", sep = ";", quiet = T)
    close(scon)
    cstr <- stty[grep("columns", stty)]
    options(width = as.numeric(gsub("[^0-9]", "", cstr, ignore.case = T)))
    paste("width =", options()$width, "\n")
}


A function I wrote that resets the width option to match the window
widths, and therefore adjusts the wrapping after I resize a windwo.
#
Yes, I found the width option in the help pages, but I was wondering
if there was automatic setting of the wrapping according to the
current window width.

Your function works exactly as I wished.  I'll probably get smarter
with time (I hope) but would it be reasonably good practice to stick
this into ~/.Rprofile?  I don't suppose there is a way to have it
automatically invoked when the window size/positition changes?  (It's
still priceless even without automatic triggering).

On Fri, Apr 17, 2015 at 7:20 PM, MacQueen, Don <macqueen1 at llnl.gov>
wrote:
#
For ESS see https://github.com/gaborcsardi/dot-emacs/blob/master/.emacs

Best,
Ista
On Apr 17, 2015 7:37 PM, "Paul Domaskis" <paul.domaskis at gmail.com> wrote:
#
On Apr 17, 2015 7:37 PM, "Paul Domaskis" <paul.domaskis at gmail.com> wrote:
Possibly, though it would take a little building.  If you were to
launch R directly when you start the xterm (loosely xterm R rather
than the default) then R would receive a SIGWINCH signal whenever the
xterm window size changes (xterm automatically sends this to its child
process).  R doesn't directly enable handling of the signal, but
there's nothing to stop you loading a dynamic library with a little C
code that set up a handler for SIGWINCH and, when it got one, ran the
equivalent of the stty command to get the new width.  The thing I've
not been able to figure out is how the C code would ever then hand
that to R asynchronously.  Anyone?

Cheers,

- Peter
#
I see now that the link I gave for configuring this is ESS doesn't
give the whole enchilada. Here is what I currently have in my emacs
config:

  (defun my-ess-execute-screen-options (foo)
                (ess-execute-screen-options))
  (add-hook 'inferior-ess-mode-hook
            (lambda()
              (setq-local
               window-size-change-functions
               '(my-ess-execute-screen-options))))

This should give the desired functionality.

Best,
Ista
On Sat, Apr 18, 2015 at 1:26 PM, Ista Zahn <istazahn at gmail.com> wrote:
1 day later
#
At this point, and since we are in an X windows context, I think it might
be easier to use the window manager's features and write a little macro or
something that will send my setwid() command to the active window, then
assign it to a simple keystroke. Then:  resize the window; hit the
keystroke, and you're done. True, it's not fully automatic, but it would
be pretty quick and easy.

Either that or give ESS a try, using the bit that Ista offered. Or maybe
Rstudio?

Peter's got a good start, but I too would be stymied at the last step;
definitely beyond my skill.

-Don
#
Paul Domaskis <paul.domaskis <at> gmail.com> writes:
Ista Zahn <istazahn <at> gmail.com> writes:
Thanks, Ista....I'm...err....I'm a vim user <<cowers>>....

Peter Crowther <peter.crowther <at> melandra.com> writes:
MacQueen, Don <macqueen1 <at> llnl.gov> writes:
Peter, Don,

Considering that I've been using Matlab, VBA, and Access for the last
decade, I think that venturing down this path might take quite some
time.  I appreciate the ideas, and if I'm ever in the zone with
programming under the hood with X-windows (which I use), I'll refer
back.  Thanks.
#
I'm glad it's helpful!

Defining it and then invoking it in ~/.Rprofile would work, but then you
will need to be careful about managing both ./.Rprofile and ~/.Rprofile
files. If you have one of the former, then the latter does not get sourced
at startup (see ?Startup). Of course, you can put source('~/.Rprofile') in
a local ./.Rprofile to take care of that if you want.

But in the long run, it would be a better practice to put personal helper
functions like this in a package and then load it in your .Rprofile
file(s). Most of my ./.Rprofile files have
  require(rmacq)
  setwid()
in them (along with whatever other directory-specific startup actions I
want). The more personal helper functions you have, the more valuable it
will be to put them in a package instead of defining them in ~/.Rprofile.

-Don
#
On Mon, Apr 20, 2015 at 10:59 AM, MacQueen, Don <macqueen1 at llnl.gov>
wrote:
Thanks, I'll keep it in mind, Don.  I'm sort of careening at breakneck
speed into time series and R, so I know I'll be rough around the edges
for a while, with the more refined aspects such as sensible
organization of customizations following in the rear.  Not ideal, I
know, but 'tis what it is...