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.
R interactive example
Loading interactive example…
The live example needs a desktop screen. The result it produces is shown here, and the explanation and settings below work on any screen.
| quantity | unit_price | total |
|---|---|---|
| 2 | 5 | 10 |
| 3 | 4 | 12 |
| 0 | 8 | 0 |
| 6 |
- Open the R code and find the line multiplying
quantitybyunit_price. Thetotalcolumn contains 10, 12, 0 and NULL. - Change
output_1$totaltooutput_1$line_total. Click outside the code editor, or select another block. The result column is renamed without changing its values. - Add
output_2 <- input_1on a new line. A second output port appears. Select it to inspect the originalquantityandunit_pricewithout 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.
Inputs and outputs
Ports are determined by the script. These are the ports used by the default code.
Inputs
1 port- input_1Required
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.