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 files including: R data sets (.rds), SPSS (.sav), Excel (.xlsx), and .csv files using a series of R functions from the flipAPI package.
- Method - Load the required flipAPI R package - This is required to work with all the Methods below.
- Method - Check if file exists in the Displayr Cloud Drive
- Method - Load file from the Displayr Cloud Drive
- Method - Save file to Displayr Cloud Drive
- Method - Delete files from the Cloud Drive
- Method - Set repeated workflow
- Technical Details
Requirements
- Familiarity with R How to Learn R and How to Work with Data in R.
Method - Load the required flipAPI R package
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. Note that most times you will want to develop your custom R code in a Calculation before using it in a Data Source so you can more easily troubleshoot errors that come up.
2. Add a line to reference the flipAPI library:
library(flipAPI)
Method - Check if file exists in the Displayr Cloud Drive
If you'd like to check whether a file exists within the Displayr 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 logical TRUE or FALSE answer. So you can use this within an if-else structure to load the file if it exists or otherwise print an error message, see How to Work with Conditional R Formulas for an example. 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.
Method - Load file from the Displayr 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)
Note that the data set loaded from an .rds automatically already has a name in R you can reference (it may be different than the filename). However, if you are reading in a sav or csv file, you will need to assign the data a name within R to use it in code further down the line:
data = QLoadData('test.sav')
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)
If the file is in a folder in the Cloud Drive, use two backslashes (\\) to access the file inside the folder:
con = QFileOpen('testfolder\\Book1.csv', open='r')
read.csv(con)
Method - Save file to Displayr Cloud Drive
To save or update a file to the Displayr Cloud Drive, you should use QSaveData by passing the name of the item in your document (or created in your R code) 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)
If you want to save a file into a folder in the Cloud Drive, use double backslashes (\\) to save inside the desired folder.
#open a connection to the csv file to write to on the cloudcon = QFileOpen('testfolder\\test.csv', open='w')
#write the data data frame to the filewrite.csv(data,con)
#close the connection to the file to saveclose(con)
Note that you cannot reference an entire Displayr data source in R, so if you'd like to save any data sets from the Data Sources pane to the Displayr Cloud Drive, you can export the data via the menu (which can be setup to auto-export on publishing). Otherwise, you need to create a table of the raw data: either a raw data table output or by using the data.frame() function within R (you can select all the variables and drag them in bulk the raw data table output or R code to automatically insert their references). Then you can reference that raw data table in the QSaveData function.
Method - 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.
Like reading and saving, access files inside folders in the Cloud Drive with double backslashes.
QDeleteFiles("testfolder\\test.csv")
Method - Set repeated workflow
In order to create a workflow that automatically imports and exports updated files, you can additionally add a schedule to your calculation and optionally set it to republish each time.
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 Set Up Schedules in Displayr