[R] RGnumeric real time refresh?

Duncan Temple Lang
Tue, 10 Dec 2002 12:37:08 -0000

That's a very nice example of a general concept that I have hoped we
might get to. There are a variety of ways to approach this issue.
While slightly abstract/apparently indirect, using the Gnumeric/Gtk
event loop is probably the easiest way to think about things and you
can interact with it using the RGtk package.

At its simplest, if you really knew that there was more data arriving
each and every 7 seconds, you could register a timer action with the
Gnumeric event loop using the gtkAddTimeout() function in theRGtk
package.  Here's a function a that one can call from Gnumeric which
arranges for the function f (defined inside this function) to be
called every interval milli-seconds.  That function f generates a
single normal observation and puts it in a cell ( A1 by default)
and forces the entire workbook to be updated.

setTimer <-
function(interval, r = 1, c = 1, .sheet) {
 library(RGtk)

 f <-
   function() {
     .sheet[r, c] <- rnorm(1, mean = 10)
     recalcBook(getGnumericWorkbook(.sheet))
     return(FALSE)
   }

 gtkAddTimeout(interval, f)
}

gnumeric.registerFunction(
      setTimer, "setTimer", "fff", "garbage",
"Set a timer to fire each interval")

So you can then call this from within a Gnumeric cell as

  =setTimer(7000, 1, 1)

and it will just continue to update.

(The return(FALSE) at the end of f() is slightly unintuitive. It says
don't renew the timer action. Why? Because recalculating the sheet
will reset the timer when the =setTimer(..) cell is re-evaluated).


That's how you can do things with timers.  If the data is coming from
one of a collection of connection types (e.g. FIFOs, sockets, etc.) we
might register an S function that is invoked each time more data
becomes available.  In other words, we would put an event handler on
the connection. At present, this is not possible to do in regular R.
I have written code that implements it but never committed it for a
variety of reasons. Perhaps I will do so soon.

 Thanks for the application. This is one of the more interesting cases
which motivated the RGtk package.

 D.


Richard Spady wrote:
> I have data that comes in every 7 seconds or so and I'd like to display
it in a spreadsheet, and
> possibly take user input from the spreadsheet.
>
> I have installed RGnumeric and written an appropriate R function that
reads, manipulates and
> displays the data by writing via RGnumeric to a spreadsheet. However, the
results of this are
> not displayed until the R function returns, so refreshing by just putting
the body of this function
> in a loop will not work (apparently.) Nor can I see how to get Gnumeric to
call the function
> repeatedly, i.e. without retyping 'runR()' or similar; even the
refresh button F9 only works once
> (therafter a message to the effect that "R Code is not found"
appears.)
>
> Any pointers?
>
> Thnaks,
> Richard Spady
> P.S. This ua Gnumeric 1.0.9 on RedHat 8.0, R 1.6.1.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help

--
_______________________________________________________________

Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070