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.
Requirements
Method
1. Create either a Calculation on the page of the data via Calculation > Custom code, or additionally for Displayr licenses, an R data source via Data Sources > Plus (+) > R.
2. Add a line to reference the flipAPI library:
library(flipAPI)
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:QFileExists('test.rds')
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:QLoadData('test.rds')
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):QLoadData('test.rds',company_secret)
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')
read.csv(con)
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:QSaveData(data,'test.csv')
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 cloudcon = QFileOpen('test.csv', open='w')
#write the data data frame to the filewrite.csv(data,con)
#close the connection to the file to saveclose(con)
D. Delete files from the Cloud Drive
To delete one or more files from the Cloud Drive, use QDeleteFiles by passing a list of file names to the function. These should be the full file names as they appear in the Cloud Drive.
For single files:QDeleteFiles("test.csv")
For multiple files:
QDeleteFiles(c("test.csv", "test2.txt"))
This will return a message notifying you whether or not the file(s) has been successfully deleted. Note that if the file doesn't exist in the Cloud Drive, the message will still say the deletion was successful.
E. 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:
flipTime::UpdateEvery(3,"hours",options="wakeup")
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")
Technical Details
This feature uses the flipAPI package. More information about the package can be found here.
Next
How to Use the Displayr Cloud Drive
How to Automatically Update Calculations, Variables and Data Sets Using R