R Client Library
The R client library is a Storage API client which you can use in your R code. The current implementation supports all basic data manipulations:
- Importing data
- Exporting data
- Creating and deleting buckets and tables
The client source code is available in our Github repository.
Installation
Section titled “Installation”This library is available on Github, so we
recommend that you use the devtools package to install it.
# first install the devtools package if it isn't already installedinstall.packages("devtools")
# install dependencies (another github package for aws requests)devtools::install_github("cloudyr/aws.s3")
# install the SAPI R client packagedevtools::install_github("keboola/sapi-r-client")
# load the library (dependencies will be loaded automatically)library(keboola.sapi.r.client)To list available commands, run
?keboola.sapi.r.client::SapiClientThe client is implemented as an RC class. To work with it, create an instance of the client. The only required argument to create it is a valid Storage API token.
client <- SapiClient$new( token = 'your-token')Example --- Create a Table and Import Data
Section titled “Example --- Create a Table and Import Data”To create a new table in Storage, use the saveTable function. Provide the name of an existing bucket,
the name of the new table and a CSV file with the table’s contents.
To create the new-table table in the in.c-main bucket, use
myDataFrame <- data.frame(id = c(1,2,3,4), secondCol = c('a', 'b', 'c', 'd'))client <- SapiClient$new( token = 'your-token')
table <- client$saveTable( df = myDataFrame, bucket = "in.c-main", tableName = "new-table", options = list(primaryKey = 'id'))The above command will import the contents of the myDataFrame variable into the newly created table. It will
also mark the id column as the primary key.
Example --- Export Data
Section titled “Example --- Export Data”If you want to export a table from Storage and import it into R, use the importTable function. Provide
the ID (bucketName.tableName) of an existing table.
To export data from the old-table table in the in.c-main bucket, use
client <- SapiClient$new( token = 'your-token')
data <- client$importTable('in.c-main.old-table')The above command will export the table from Storage and save it in the data variable. The output is
a data.table object compatible with a data.frame.
Other Examples
Section titled “Other Examples”# create a clientclient <- SapiClient$new( token = 'your-token')
# verify the tokentokenDetails <- client$verifyToken()
# create a bucketbucket <- client$createBucket("new_bucket", "in", "A brand new Bucket!")
# list bucketsbuckets <- client$listBuckets()
# list all tablestables <- client$listTables()
# list all tables in a buckettables <- client$listTables(bucket = bucket$id)
# delete a tableclient$deleteTable(table$id)
# delete a bucketclient$deleteBucket(bucket$id)