From maechler at stat.math.ethz.ch  Fri Jul 15 15:21:56 2016
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Fri, 15 Jul 2016 15:21:56 +0200
Subject: [Rd] cat() in system.time() ?
In-Reply-To: <577A8594.9070408@gmail.com>
References: <577A8594.9070408@gmail.com>
Message-ID: <22408.58228.665116.384597@stat.math.ethz.ch>

Hi Ben (and everyone else),

as this did not attract attention yet, let me start

>>>>> Ben Bolker <bbolker at gmail.com>
>>>>>     on Mon, 4 Jul 2016 11:49:40 -0400 writes:

    > Does anyone know if there's a reason that proc.time() uses cat()
    > rather than message() to print the output when there has been an error
    > in the process of timing?

This is really not about proc.time(), but  about  system.time()
[ and I have corrected the  'Subject' accordingly ] ..

    > line 31 of time.R,

    > https://github.com/wch/r-source/blob/e5b21d0397c607883ff25cca379687b86933d730/src/library/base/R/time.R#L31

    > on.exit(cat("Timing stopped at:", ppt(proc.time() - time), "\n"))

    > This means that as far as I can tell the general way to make sure
    > there is no output from a timed statement is ...

    > tt1 <- capture.output(tt0 <- suppressMessages(suppressWarnings(
    >					try(<stuff to try>, silent=TRUE))))

    > (I know I could/should be using tryCatch() instead of try(), but I don't
    > think it really matters here ... ?)

    > What would people think of a request to change this to message()
    > rather than cat() in the future ... ?  (This would mess up code that is
    > already using capture.output() to store this information ...)

[I think that (last issue) would be acceptable.]

One reason of the current cat() may just be historical:
message() did not exist yet when  system.time()  was created.
However, I agree that that is not good enough a reason to keep
it. Much more important is the fact that it is *nice* that the
message

   Timing stopped at: ...

is printed in many cases when a system.time()d call is stopped
early.  Quite often for me this is *not* when an error happens
as your suppress*() contortions (;-) suggest, but rather when I
interrupt the long lasting call.
And the current setup nicely gives 

   > i <- 0; system.time( while(TRUE) i <- i+1 )
     C-c C-c
   Timing stopped at: 1.001 0.084 1.086 
   > 


However, at least this simple case, also works fine with
message() instead of cat() ... as I just tried now.

A harder case are the "bad errors", e.g.,  memory overflow and
similar bad things...
cat() seems to be pretty robust, where as message() does invoke
a handler (exactly *why* you want it, right?) and that may be
harder to keep working correctly after certain errors than a
simple cat().

I hope that some real experts (on "context switching", "long
jumps", etc) would chime in now.

Martin



[Rd] cat() in system.time() ?
Martin Maechler