Working with Datasets using R
Overview
R is a powerful programming language for statistical computing, and is commonly used among scientists for anything from simple plot generation to interactive applications. Benchling Analyses allow users to generate datasets from the scientific data in Benchling, as well as importing / exporting these datasets to external tools for processing. Through the API, each dataset is read and written as a data frame: a tabular file with a manifest of download or upload URLs. Using the analyses and data frames endpoints, it’s possible to create R applications that can export datasets, perform complex analysis on scientific data, and seamlessly upload the results back to Benchling.
This guide walks through a basic example of working with Benchling datasets using scripts written in R. The example is an IC50 analysis on cell mortality data; the resulting data and plots are then imported to Benchling and visible as analysis outputs. The code used in this guide can be found in this public GitHub repository.
Which API version this uses
The sample scripts call the V2-beta endpoints /api/v2-beta/analyses, /api/v2-beta/data-frames, and /api/v2-beta/files. The import half has to stay there: the V3 API can read analyses (GET /api/v3/analysis/{analysis_id}), data frames (GET /api/v3/data-frame/{data_frame_id}), and datasets (GET /api/v3/dataset/{dataset_id}), but it has no endpoint to create a data frame or to add outputs to an analysis.
The download half can move to V3. GET /api/v3/data-frame/{data_frame_id} returns the same manifest array, each item carrying a fileName and a url, so the CSV can be read from manifest[[1]]$url as before. These V3 endpoints are beta; send the EARLY-ACCESS: true header. See the V3 Stability Guidelines.
What you’ll need
- Familiarity and an environment to work with R (the repository targets R 4.5.3 or later, with dependencies managed by
renv) - Access to Benchling Analyses
- ~20 minutes
Setup & Configuration
It’s important to note that this sample code is provided for educational purposes and is not for production use. There are a few required configuration steps before you can use it with your tenant:
- Dependencies - Run
Rscript -e "renv::restore()"in the repository to install the packages the scripts use, includinghttr,jsonlite,drc,plotly, andhtmlwidgets. - Analysis with an input dataset - In order to pull/push data to an analysis in Benchling, an analysis must be created with an input dataset attached. To try the scripts without your own data, attach
examples/sample_ic50_data.csvfrom the repository; in production this would be an existing dataset or results table. The scripts expect the columnsCell Name,Concentration,Mortality 24h, andMortality 48h, which R reads asCell.Name,Concentration,Mortality.24h, andMortality.48h. - Authentication - By default the scripts authenticate with an analysis key, so no Benchling app is needed. To use a Benchling app instead, set
USE_OAUTH2 <- TRUEat the top ofget_dataframe.R(andapp.R), and putBENCHLING_CLIENT_ID,BENCHLING_CLIENT_SECRET, andBENCHLING_DOMAINin.Renviron(see.Renviron.example). The app needs read and write access to analyses, data frames, and files.
A visual of a dataset being created from a registration table is included below. The Benchling Help Center and Analyses API Developer Guide are great resources for working with analyses in Benchling.

Getting Started
The sample code provided includes a number of different files:
get_dataframe.R- A script that downloads the analysis’s input dataset from Benchling and converts it to an R dataframeresults_analysis.R- A script that performs an IC50 calculation, then uploads the results and plots as outputs of the Benchling analysisapp.R- A Shiny web app that runs the same workflow from a browser interfaceutils.R- Shared functions for the IC50 calculation and plottingexamples/sample_ic50_data.csv- Sample dose-response data: 21 observations, 7 concentrations with 3 replicates eachexamples/local_test.R- Runs the IC50 calculation on the sample data offline, without connecting to Benchling
Working with the sample code requires some configuration in Benchling (see Setup & Configuration). Once that configuration is complete, you should have an analysis that looks something like this:

Next, copy the analysis key, which is what the scripts use to find the analysis and authenticate. In your analysis, click Connect with external tool, then Copy Analysis Key. The key looks like ana_XXXXX:eyJhbGc...: the analysis ID and a JWT, separated by a colon. It is tied to the user who generated it and expires after 10 minutes, so generate a fresh one if a run fails with a 401.

With an analysis created and analysis key in hand, you’re ready to run the scripts. They must run one after the other in the same R session, because results_analysis.R reads variables that get_dataframe.R leaves behind:
To skip the prompt, set the BENCHLING_ANALYSIS_KEY environment variable. Alternatively, shiny::runApp("app.R") runs the whole workflow from a web interface.
Below is an overview of how each script operates:
get_dataframe.R
This script pulls the input dataset from your analysis in Benchling and converts it to an R dataframe. We can consider three major steps:
- Authentication - The script splits the analysis key into the analysis ID and the JWT. By default it uses the JWT directly as the bearer token and reads your tenant’s domain from it. With
USE_OAUTH2set, it instead exchanges the app’s client ID and secret for an access token atPOST /api/v2/token, and usesBENCHLING_DOMAINas the tenant. - Querying the analysis - The script calls
GET /api/v2-beta/analyses/{analysis_id}and reads the input data frame’s ID fromdataFrameIds, along with the analysis’sfolderIdfor the uploads later. Importantly, this script assumes there is only a single input dataset. - Exporting the data frame - The script calls
GET /api/v2-beta/data-frames/{data_frame_id}and reads the CSV straight from the URL inmanifest, saving it as the R dataframedf.
When it finishes, df, base_domain, analysis_id, access_token, and folder_id are set in the session.
results_analysis.R
results_analysis.R needs those variables, so run get_dataframe.R first, then run results_analysis.R directly after.
There’s a bit more going on in this script; it involves processing the dataframe, generating the relevant outputs (a CSV and two HTML plots), and uploading the outputs to your analysis:
-
Calculating IC50 - The first portion of the script is where the “magic” happens. Using a 4 parameter log-logistic model (
LL.4from thedrcpackage), the script calculates the IC50, slope, standard error, t-value, and p-value for both the 24 hour and 48 hour mortality columns. The two rows are saved locally in a CSV file in preparation for subsequent steps.While most of the sample code is dedicated to interacting with the Benchling API, here is where actual data processing occurs. While you’ll be writing very similar code to interact with Benchling when building your own integrations, this is where you’ll likely be doing very different things. Consider how you can build on what you learn here to create robust, reusable integrations in the future!
-
Uploading the output data frame - The IC50 data must be uploaded to Benchling in order to become an output dataset on your analysis. The script calls
POST /api/v2-beta/data-frameswith the CSV’s file name inmanifestand the nameMortality_IC50, and the response’smanifestholds an S3 upload URL. After uploading the CSV to that URL, the script sets the data frame’suploadStatustoIN_PROGRESSwithPATCH /api/v2-beta/data-frames/{data_frame_id}, which tells Benchling the upload is done and the file can be processed. -
Generating and uploading the plots - Using the plotly library, the script creates a dose-response plot for each time point and saves them as
mortality_24h.htmlandmortality_48h.html. Benchling analyses support a number of file types; here the script is creating.htmlfiles to visually represent the data. Unlike data frames, these files are uploaded with the files endpoints:POST /api/v2-beta/fileswith aname,filename, and the analysis’sfolderIdreturns the S3 upload URL in theContent-Locationresponse header, and after the uploadPATCH /api/v2-beta/files/{file_id}setsuploadStatustoSUCCEEDED. -
Importing outputs to the analysis - Last but not least, the uploaded data frame and plots must be added to the analysis as outputs. The script calls
PATCH /api/v2-beta/analyses/{analysis_id}with a body offileIds(the two plots) anddataFrameIds(the IC50 data frame).
Outputs
With our analysis complete and our outputs uploaded to Benchling, we can see our data appearing in the analysis outputs:

This tabular data can be saved to results:

Finally, we can view our mortality plots under Files:

At which point our analysis is complete!
Conclusion
Working with a Benchling analysis, we were able to export an input dataset and perform an IC50 calculation on it using an external R script. What’s more, we could then upload the outputs back into our analysis, both as a dataset and a visual plot showing our results.
These same principles can be applied to more sophisticated integrations: Running more complex analysis on data, integrating with 3rd party analysis tools, and building user interface applications using R Shiny are all possible leveraging Benchling analyses.