Each workspace already is an API
QBit Workspace is a new service to immediately deploy data science results at scale. You can think of it as an online data science editor (like RStudio) which can also be controlled and automated from any programming language through a REST API. Once a workspace has been created—including code, environment objects and files—there is no need for a separate (API) deployment step any more. Each workspace already is an API. With its powerful REST API interface it can be easily embedded into any application, app or programming language without running and managing your own R- or Python server.
We’re now happy to announce the launch of our API service in public beta, which allows to control every aspect of the workspace programmatically including actions like:
- Workspace creation
- Workspace deployment
- Code execution
- Rendering of RMarkdown documents
- File up- and downloads
- Package install/remove
Get Started with the QBit Workspaces API
To use the API from R first install the qbit package from the Quantargo Github repository:
Next, you need to retrieve your free API key from the Quantargo page settings section:
For more information about API key creation and usage also see our detailed step-by-step guide. Ideally, set your API key
QKEY through the
options() settings as
options(QKEY = "<ENTER-API-KEY-HERE>")
so that all further API calls use the key accordingly. Now you are ready to interact with QBit workspace! As a first example, we’ll show how to create an API-ready RMarkdown report within R.
Creating RMarkdown Documents through the qbit R-API
RMarkdown combines markdown text with R outputs (e.g. plots, tables) to create reproducible documents in multiple output formats (e.g. HTML, PDF, Word, Powerpoint, see also here). Most R-data scientists use their local (RStudio) environment to produce these reports. But what if we want to render these reports through a web application on the fly, maybe even parametrized or with updated input data sets? In the following section we’ll create a QBit workspace for RMarkdown to quickly render an HTML document through the API.
Let’s start by creating a new workspace based on the RMarkdown template:
qbit_id <- qbit::create(qbit_name = "RMarkdown Example HTML document")
The created workspace received a new and unique
qbit_id based on its
qbit_name title. You can also visit the new workspace online and even share its link with your friends/co-workers. Further changes to your workspace can now be done through the API using the
qbit::deploy() function or directly within the online editor.
Once you are satisfied with your workspace you can run specific R commands, retrieve their respective outputs and integrate them into your application. Most typically, you might want to execute specific commands like
predict() (for model predictions) or any kinds of user–defined functions through
qbit::run interface, which allows to execute any arbitrary R code, is therefore very general and can support any complex API use cases. For our RMarkdown use case we would like to render the
main.Rmd file as an HTML document with
render_out <- qbit::render(qbit_id)
$console_output element contains a data frame (tibble) of all created contents through the call:
# A tibble: 8 x 3 type content name <chr> <chr> <chr> 1 code-input "rmarkdown::render(\"main.Rmd\")" <NA> 2 code-mess… "Warning message: \n\nprocessing file: main.Rmd… <NA> 3 code-outp… "\r | … <NA> 4 code-outp… "\r | … <NA> 5 code-mess… "Warning message: output file: main.knit.md\n\n" <NA> 6 code-outp… "/usr/bin/pandoc +RTS -K512m -RTS main.utf8.md … <NA> 7 code-mess… "Warning message: \nOutput created: main.html\n" <NA> 8 file "https://cdn.quantargo.com/assets/user/courses/… main.h…
The link of the created Rmarkdown document is located in the row where content
library(dplyr) render_out$console_output %>% filter(type == "file")
# A tibble: 1 x 3 type content name <chr> <chr> <chr> 1 file https://cdn.quantargo.com/assets/user/courses/b8451… main.ht…
The included link in the
content column can be easily integrated into your own web application (via an
<iframe> tag) or just downloaded locally (e.g. via
download.file() in R).