Skip to content
Prev 351389 / 398502 Next

Automatically updating a plot from a regularly updated data file

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