[R] Automatically updating a plot from a regularly updated data file

MacQueen, Don macqueen1 at llnl.gov
Fri May 29 23:16:34 CEST 2015

A lot will depend on how frequently data is added to the file, how big the
file gets, and how important it is to see updated plots quickly.

I have R doing exactly what you describe, and have found logic like this
(which might be described as crude) to be sufficient

while( {some condition} ) {
  {read the data file}
  {make the plot}
  Sys.sleep( {some number of seconds} )

Of course this is not actually noticing that the file has changed and
responding, it is just updating at regular intervals. But that might be
good enough.

A slightly more sophisticated approach would be to set up a loop like the
above, and have the sleep time short, but within the loop use

  file.info({the csv file})

and when the modification time is later than the previous modification
time, read the data and update the plot.

If the file gets really big, you might not want to reload the entire file
each time. That might lead you into things like keeping track of how many
lines the file has, and only reading the new lines -- if you need your
plots to be cumulative. In that situation you might end up using the
pipe() function to create your connection to the file, and pass the OS's
'tail' command (Linux or Mac, not sure about Win) to pipe.

If you only need to plot the last, say, X hours of data, then you may not
need to keep track of the number of lines, just read the last N lines
(hopefully not too hard to figure out what N should be).

If you don't want an R process running indefinitely, as is the case for
the above, you can, on Linux and Mac, set up a cron job to run an R script
as often as once per minute. I have at least one such task where it
happens every 2 minutes, and makes plots of the current data. In this
case, we have 16 measurement devices each sending data to a MySQL database
once per minute; the R script pulls the data from the database every 2
minutes and plots, and the system works well for our needs. Windows will
have some equivalent to cron, I just don't know what it is.

FWIW, all of the above write png files which are viewed via a webserver.


Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

On 5/29/15, 12:51 PM, "Sam Albers" <tonightsthenight at gmail.com> wrote:

>Hi all,
>I have a question about using R in a way that may not be correct but I
>thought I would ask anyway.
>I have an instrument that outputs a text file with comma separated data. A
>new line is added to the file each time the instrument takes a new
>Is there any way to configure R such that a script to generate a plot from
>said text file is re-run each time the file is modified (i.e. a new line
>added). So basically update an exported plot each time a new line of data
>is collected.
>Is this type of thing possible in R? If not can anyone recommend some
>Windows (or Linux if need be) tools that could help me accomplish this
>preferably still utilizing R's plotting capabilites? I know that there are
>other tools that can do this all but nothing makes figures as nicely as R.
>I suppose more generally this is a question about way to automate
>with R to take advantage of R's functionality.
>Thanks in advance.
>	[[alternative HTML version deleted]]
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list