> This page is for Developer Documentation.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.benchling.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.benchling.com/_mcp/server.

# 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](https://github.com/benchling/R-Datasets-Example).

> **Info**
>
> ### 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](/docs/v3-api-overview) 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](/docs/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 \[#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, including `httr`, `jsonlite`, `drc`, `plotly`, and `htmlwidgets`.
* **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.csv` from the repository; in production this would be an existing dataset or results table. The scripts expect the columns `Cell Name`, `Concentration`, `Mortality 24h`, and `Mortality 48h`, which R reads as `Cell.Name`, `Concentration`, `Mortality.24h`, and `Mortality.48h`.
* **Authentication** - By default the scripts authenticate with an analysis key, so no Benchling app is needed. To use a [Benchling app](/docs/getting-started-benchling-apps) instead, set `USE_OAUTH2 <- TRUE` at the top of `get_dataframe.R` (and `app.R`), and put `BENCHLING_CLIENT_ID`, `BENCHLING_CLIENT_SECRET`, and `BENCHLING_DOMAIN` in `.Renviron` (see `.Renviron.example`). The app needs read and write access to analyses, data frames, and files.

> **Info**
>
> A visual of a dataset being created from a registration table is included below. The Benchling [Help Center](https://help.benchling.com/hc/en-us/articles/15298157390861-Creating-an-analysis) and [Analyses API Developer Guide](/docs/analyses-api-developer-guide) are great resources for working with analyses in Benchling.

![An animation of a user selecting rows in a registration table and creating an analysis dataset from them](/_fern-files/benchling-040512.docs.buildwithfern.com/97065e7ad5dde71374b7da3df6476873a6bc09de36c70dd9095ef84d0e19325d/assets/images/r-datasets-create-from-registry.gif)

## 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 dataframe
* `results_analysis.R` - A script that performs an IC50 calculation, then uploads the results and plots as outputs of the Benchling analysis
* `app.R` - A Shiny web app that runs the same workflow from a browser interface
* `utils.R` - Shared functions for the IC50 calculation and plotting
* `examples/sample_ic50_data.csv` - Sample dose-response data: 21 observations, 7 concentrations with 3 replicates each
* `examples/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](#setup-configuration)). Once that configuration is complete, you should have an analysis that looks something like this:

![A Benchling analysis showing a starting dataset created from registry data](/_fern-files/_img/6143e0530922bc0b9f9f3f530be770eede5ba8b01f55202191d3ffd717a9ec49.webp)

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`.

![An import/export analysis step with its key available to copy](/_fern-files/_img/16fe5c0803af82295b0108790a8e6de8f5528b88435ebb4c2a5adab61e389223.webp)

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:

```r
source("get_dataframe.R")     # prompts for the analysis key
source("results_analysis.R")
```

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:

1. **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_OAUTH2` set, it instead exchanges the app's client ID and secret for an access token at `POST /api/v2/token`, and uses `BENCHLING_DOMAIN` as the tenant.
2. **Querying the analysis** - The script calls `GET /api/v2-beta/analyses/{analysis_id}` and reads the input data frame's ID from `dataFrameIds`, along with the analysis's `folderId` for the uploads later. Importantly, this script assumes there is only a single input dataset.
3. **Exporting the data frame** - The script calls `GET /api/v2-beta/data-frames/{data_frame_id}` and reads the CSV straight from the URL in `manifest`, saving it as the R dataframe `df`.

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:

1. **Calculating IC50** - The first portion of the script is where the "magic" happens. Using a 4 parameter log-logistic model (`LL.4` from the `drc` package), 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.

   > **Check**
   >
   > 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!

2. **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-frames` with the CSV's file name in `manifest` and the name `Mortality_IC50`, and the response's `manifest` holds an S3 upload URL. After uploading the CSV to that URL, the script sets the data frame's `uploadStatus` to `IN_PROGRESS` with `PATCH /api/v2-beta/data-frames/{data_frame_id}`, which tells Benchling the upload is done and the file can be processed.

3. **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.html` and `mortality_48h.html`. Benchling analyses support a number of file types; here the script is creating `.html` files to visually represent the data. Unlike data frames, these files are uploaded with the files endpoints: `POST /api/v2-beta/files` with a `name`, `filename`, and the analysis's `folderId` returns the S3 upload URL in the `Content-Location` response header, and after the upload `PATCH /api/v2-beta/files/{file_id}` sets `uploadStatus` to `SUCCEEDED`.

4. **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 of `fileIds` (the two plots) and `dataFrameIds` (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:

![An import/export analysis step showing the uploaded IC50 dataset as an output](/_fern-files/_img/4b463b2daa5666023ba8450a0ca04b7dce0dc4f192903e17291d289b6f7aa6b8.webp)

This tabular data can be saved to results:

![The analysis output dataset being saved into a Benchling results table](/_fern-files/_img/e584b73a9992efe9f1206ab8da0f19346d0f75d4e29d918661c90e8192c0e9df.webp)

Finally, we can view our mortality plots under Files:

![An HTML scatter plot of cell mortality against cell concentration, shown in the analysis Files tab](/_fern-files/_img/847c2ab3935a6607dea9373cfb1aa14df8c17bc523827b25359ca34af8c5f757.webp)

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](https://help.benchling.com/hc/en-us/articles/16579394094605-Integrate-an-analytics-tool-with-Benchling), and building user interface applications using R Shiny are all possible leveraging Benchling analyses.