The R-DataGraph Package

The DataGraph package allows you to directly integrate output from the R language with DataGraph. Using this package, you can set up DataGraph to monitor the output from R in real time. Thus, you can use DataGraph as part of your workflow in R.

You can access the DataGraph Package from the CRAN:
https://CRAN.R-project.org/package=DataGraph

Overview of the DataGraph Package

The R language has several ways of saving data sets including: data frames, vectors, lists, and time-series. These data sets can include numeric data, text data, dates, and data labels. The DataGraph package interprets these R formats and maps the R data into the DataGraph data types (numerical, text, dates).

There are three different uses for the package:

  1. To save a single R data set into a file (.dtable).
  2. To save a sequence of R data sets into a single file (.dtable).
  3. To save a collection of R data sets, either single data sets or sequences, into a file (*.dtbin).

Once you create a *.dtable or *.dtbin file, you can link those files to DataGraph and use the data in graphics or do further data processing. DataGraph monitors the linked dtable and dtbin files. If you update the data in these files, DataGraph will automatically reload the data and redo any graphics or function calculations. Typically the change will be reflected within a second of updating the file so you can effectively use DataGraph as part of your R programing flow.

TIP: To view a list of built-in R data sets type “data()” from the R console. The examples below use the “austres” data set, which contains a Quarterly Time Series of the Number of Australian Residents, from 1971 to 1993. This date set contains 89 entries in a time-series format.

Example 1: Save a single R data set

Save from R

To save a single data frame, use one of the built in datasets.

writeDTable("/tmp/table",austres)

A file named ‘table.dtable’ is the location /tmp/. The data contained in “austres” is a time-series vector. The data gets automatically converted into a table with two columns, date and value. To add ‘table.dtable’ to your DataGraph file, you need to add an extra in your column definition list.

Connect file to DataGraph

Step 1: Select “Data File” from the Other menu in the column definition list.

Step 2: Select the ‘dtable’ file from your file manager (e.g., “table.dtable”). This creates a group called ‘table’ that contains two columns: ‘date’ and ‘value’.

The column named ‘date’ is formatted as a date column, so when you draw the data using a Plot command the axis will be set properly.

The data is not imported into DataGraph in the same way as, for example, a CSV file. Instead the data file is connected by using a link. If you save the DataGraph file and open it up again, the *.dtable file will be read again, using whatever values are there at that time. If the file no longer exists, the columns will be empty.

Update R Output

If you overwrite a *.dtable file with new data, that new data will be drawn instead. You can try this out by pasting the followings command in R:

austres[4] <- 20000
writeDTable("/tmp/table",austres)

The first command changes the 4th entry in the austres data set to the value 20,000. The second command re-writes the data to the ‘table.dtable’ file. DataGraph will automatically notice the change and reload the table.

Any graphs using the data will be automatically updated as well.

Example 2: Save a sequence of data frames

The first example only saved a single table, and overwrote the previous content with the new content. You can however save a sequence of tables into a single table file. Using the austres data frame as an example, create a silly example that saves 6,000 tables into a single file. This is done by first opening the table with the openDTable function, adding a data frame by using the addDTable command and then finally close the table file with the closeDTable function.

 mydata <- austres
openDTable("/tmp/table")
addDTable("/tmp/table",mydata)
for (incr in 1:6000) {
     mydata <- mydata+1
     addDTable("/tmp/table",mydata)
}
closeDTable("/tmp/table")

This saves the 6001 tables into the file and DataGraph doesn’t load the content until the table file has been closed. Then you can use the small time slider to scrub through the sequence of tables. Note that here time is really the sequence number 0,1,2,…,6000

It is possible to have DataGraph load the sequence in while R is still writing the file. In order to do that however you need to make it explicit with a call to syncDTable

mydata <- austres
openDTable("/tmp/mytable")
addDTable("/tmp/mytable",mydata)
for (incr in 1:6000) {
     mydata <- mydata+1
     addDTable("/tmp/mytable",mydata)
     syncDTable("/tmp/mytable")
}
closeDTable("/tmp/mytable")

DataGraph will read this in in chunks, since the file system will cache the file and write it in blocks. But typically your execution will take a longer time and the effect will be more continuous. The file refresh is around one per second.

Example 3: Save a collection of data frames

The previous two examples saved a single frame to a file. You can however same multiple data frames to the same disk, and each one can be a sequence. The main requirement is that the names have to be unique and each frame with the same name has to have the same structure, e.g. number and name of columns. As an example, let’s save a single frame of the women sample data file and the same sequence as before of austres

openDTBin("/tmp/combined")
addDTBin("/tmp/combined","women",women)
mydata <- austres
addDTBin("/tmp/combined","austres data",mydata)
for (incr in 1:6000) {
     mydata <- mydata+1
     addDTBin("/tmp/combined","austres data",mydata)
     syncDTBin("/tmp/combined")
}
closeDTBin("/tmp/combined")

Was this article helpful?

Related Articles