This post explains how to use Displayr to automatically append new data to an existing dataset stored in Dropbox. Specifically, it will show you how to set up a system that automatically imports a file from Dropbox to Displayr, adds data to it using Displayr and then exports the updated file back to Dropbox. An alternative approach is to use the API.


Adding a file to Dropbox

For Displayr to access Dropbox you need an account and an authorization token. If you do not already have one, the process of creating a token is described here.

First, I am going to add a file to Dropbox where I will store my data. If you have an existing file that you want to build upon, you just need to transfer that file to your Dropbox account and you can skip the rest of this step. I don't have an existing file, so I create one in Displayr by navigating to Insert >  R Output then entering the R code below. In place of the token argument, you need to put your Dropbox token string. For instance, if your token was ABCDefgh1234 you would need to put "ABCDefgh1234" in place of token (including the speech marks). Tokens are normally quite long.

library(flipAPI)
empty.data = data.frame(time = as.POSIXct(character()),
                        memory = integer())
ExportToDropbox(empty.data, token, file = "memorydata.rda")

I am going to use this file to store the memory usage of the R session, so my empty file has 2 columns: time and memory. That's why I have two arguments in the data.frame() function above. You can amend the names, number and data type of the columns to fit the format of your data. Perform this step once only. Repeating it will overwrite any existing file.


Updating the file with new data

Next, I am going to create a process that automatically downloads the existing data file from Dropbox, adds a new line then re-exports the updated file. The first two lines of the code below set the update frequency to every 15 minutes. The wakeup option means this happens even if the document is closed.

The following two lines import the existing data. Again token should be replaced by your token string. For this example, the amount of memory used by R (in megabytes) is imported and a new row with the time and the memory used is added. Finally, the new data is appended to the existing data and sent back to Dropbox.

library(flipTime)
UpdateEvery(15, units = "minutes", options = "wakeup")

library(flipAPI)
imported = ImportFromDropbox("memorydata.rda", token)

library(pryr)
memory = as.integer(mem_used() / 1000000)
new.data = data.frame(time = Sys.time(), memory = memory)

exported = rbind(imported, new.data)
ExportToDropbox(exported, token, "memorydata.rda")

After leaving this running for a short time the imported data.frame is as shown below. This data can then be plotted or used in other analyses. If you like, you can make a new R object and use lines 4 to 5 above as the code to recall imported (which is what I do in my example on the third page of this Displayr document).

time memory table example


Try it yourself

You can see and edit and append new data to the document I created by clicking here. Use it as a template. For the R code on each page, you need to paste in your own Dropbox token. In my example I am storing the token in a variable called token on the first line, so you can just use token later in the code (remember to include the speech marks around your token).