The Displayr Cloud Drive offers a way for users to store and share files across your company documents. This article will show you how to use R code to load and save R data sets (.rds), Excel (.xlsx), and .csv files using a series of R functions from the flipAPI package.
- A professional user license.
1. Create either a Calculation via Calculation > Custom Code or an R Data set via Data Sets > Plus (+) > R.
2. Add a line to reference the flipAPI library:
A. Check file exists in Cloud Drive
To check whether a file exists within the Cloud Drive, you can use the QFileExists function by simply specifying the name and extension of the file:
This will return a TRUE or FALSE. Note that this should be used in a Calculation rather than when adding a new data set, as the latter still needs to return a data set if it is FALSE.
B. Load file from Cloud Drive
To load an existing file stored in the Cloud Drive, you should use QLoadData with the exact name and extension of the file:
If you have multiple companies under the same account, you can share files among your companies using the company secret key where the file is located (this can be provided on request):
Alternatively, when working with .xlsx and .csv files, QFileOpen is a further function for reading data. The below code opens the specified .csv file as read-only and loads it into your Calculation or data set:
con = QFileOpen('test.csv', open='r')
C. Save file to Cloud Drive
To save or update a file to the Cloud Drive, you should use QSaveData by passing the name of the output and then specifying the name and extension of the file:
Here, 'test.csv' is created, the contents of 'data' are written to it and it is then saved to the Cloud Drive. This function can create CSV, SAV, RDS, and RDA files.
Alternatively, when working with .csv files, QFileOpen can also be used for writing data to a .csv file:
#open a connection to the csv file to write to on the cloud
con = QFileOpen('test.csv', open='w')
#write the data data frame to the file
#close the connection to the file to save
D. Set repeated workflow
In order to create a workflow that automatically imports and exports updated files, you can additionally add a flipTime function to the first line such as UpdateEvery or UpdateAt to set a timer.
Below we have set it to run every 3 hours:
Here, we have it running every 3 hours from a specific time, time zone, and date:
flipTime::UpdateAt("01-01-2021 18:00:00", time.zone = "America/New_York", 3,"hours",options="wakeup")
This feature uses the flipAPI package. More information about the package can be found here.