---
title: "Run R code in Strata | Oplema"
description: "Run your own R in a Strata flow. Read input tables as data.frames and assign each output, in your browser or in a hosted run."
url: "https://oplema.com/docs/strata/developer/r"
updated: "2026-11-08"
---

# R

Write R that reads each input as input\_1, input\_2 and so on, and assigns each output to output\_1, output\_2 and so on. Each is a data.frame. Numbered names in your code create the matching ports. Code runs in your browser, or in a hosted run in your region, and imported packages appear in the package table.

## Calculate a total in R

Manual Input has four rows with quantity and unit\_price. R copies the input data frame and adds total.

Result
| quantity | unit\_price | total |
| --- | --- | --- |
| 2 | 5 | 10 |
| 3 | 4 | 12 |
| 0 | 8 | 0 |
|  | 6 |  |

1. Open the R code and find the line multiplying `quantity` by `unit_price`. The `total` column contains 10, 12, 0 and NULL.
2. Change `output_1$total` to `output_1$line_total`. Click outside the code editor, or select another block. The result column is renamed without changing its values.
3. Add `output_2 <- input_1` on a new line. A second output port appears. Select it to inspect the original `quantity` and `unit_price` without the calculated column.

## When to use

Use R when no block does what you need, or when you want an R package. Your code receives input tables as data.frames.

## Read the output

The R example copies `input_1` into `output_1` and adds a column:

```
output_1  <-  input_1
output_1 $ total  <-  input_1 $ quantity  *  input_1 $ unit_price
```

The output keeps `quantity` and `unit_price` and adds `total`. Two items at 5 produce 10; three at 4 produce 12. A zero quantity produces 0, and the missing quantity shows as `NULL`.

## Inputs and outputs follow the code

Writing `input_1` creates an input port with that name. Connect the upstream table to it. Writing `output_1` creates the matching output port. Assign a table to it to pass that result downstream.

Use `input_2` for a second incoming table or `output_2` for a second result. Ports follow numbered names found in the active code, including names in comments or strings. Removing a name removes its port, so check connections after editing code.

## Variable previews

Hover a variable in the editor to inspect its type, dimensions or length, and a small sample from the current preview run. Dimensions describe the full variable in that run, which may use sampled input data.

Values are recorded after each top-level statement, so a variable shows the value it had at that line. Loops and other compound statements show their final state at their last line. Values inside functions and single loop iterations aren't traced. Changing the code or input clears old previews until new results arrive.

## Errors and execution

Code errors and missing output assignments make the block fail. Fix the code and run it again. Each run gets the input tables connected at that moment. Use url()/download.file() for network requests.

Custom code runs in your browser or in an isolated hosted run.

To write the step in Python, use [Python](https://oplema.com/docs/strata/developer/python).

## Inputs and outputs

Ports are determined by the script. These are the ports used by the default code.

### Inputs

1 port

**input\_1 Required**

Accepts: Tables.

Accepts one connection on this port.

### Outputs

1 port

**output\_1**

## Configure the block

**R**

Read input\_N as data.frames and assign output\_N. Imports automatically populate the package table. Edit version constraints there; installation and compilation use your usage.

Default: `# input_1: data.frame output_1 <- input_1`

**Store dependencies in Cloud**

Cache additional dependencies or compiled WebAssembly in your personal Cloud. Bundled dependencies need no storage. Cloud storage limits apply. Without caching, each run reinstalls or recompiles the same locked artifacts. Change package constraints to resolve a new lock. Delete unused cached artifacts in Cloud.

Default: `false`

## R FAQs

**When should I use R instead of a built-in block?**

Use R for custom algorithms or package operations that the existing blocks don't cover. Formula could do this too. The example is kept small to show how code blocks work.

**What happens if I don't assign an output?**

Assign a result table to every output variable in the active code. An unassigned output causes an error.

**Can I change the number of rows?**

Yes. Your code decides the output's rows and columns. This example keeps all four rows, but a script can filter, aggregate or reshape them.

### Prepare your data. Build it in Strata.

Strata opens in your browser, with no account.

[Open Strata](https://oplema.com/editor?path=%2F%3Fopen%3Dstrata) [About Strata](https://oplema.com/strata)
