type: "io.kestra.plugin.scripts.r.Script"
Execute an R script.
Examples
Install a package and execute an R script
id: "script"
type: "io.kestra.plugin.scripts.r.Script"
script: |
library(lubridate)
ymd("20100604");
mdy("06-04-2011");
dmy("04/06/2012")
beforeCommands:
- Rscript -e 'install.packages("lubridate")'
Add an R script in the embedded VS Code editor, install required packages and execute it.
Here is an example R script that you can add in the embedded VS Code editor. You can name the script file main.R
:
library(dplyr)
library(arrow)
data(mtcars) # load mtcars data
print(head(mtcars))
final <- mtcars %>%
summarise(
avg_mpg = mean(mpg),
avg_disp = mean(disp),
avg_hp = mean(hp),
avg_drat = mean(drat),
avg_wt = mean(wt),
avg_qsec = mean(qsec),
avg_vs = mean(vs),
avg_am = mean(am),
avg_gear = mean(gear),
avg_carb = mean(carb)
)
final %>% print()
write.csv(final, "final.csv")
mtcars_clean <- na.omit(mtcars) # this line removes rows with NA values
write_parquet(mtcars_clean, "mtcars_clean.parquet")
Note that tasks in Kestra are stateless. Therefore, the files generated by a task, such as the CSV and Parquet files in the example above, are not persisted in Kestra's internal storage, unless you explicitly tell Kestra to do so. Make sure to add the outputFiles
property to your task as shown below to persist the generated Parquet file (or any other file) in Kestra's internal storage and make them visible in the Outputs tab.
To access this output in downstream tasks, use the syntax {{outputs.yourTaskId.outputFiles['yourFileName.fileExtension']}}
. Alternatively, you can wrap your tasks that need to pass data between each other in a WorkingDirectory
task — this way, those tasks will share the same working directory and will be able to access the same files.
Note how we use the read
function to read the content of the R script stored as a Namespace File.
Finally, note that the docker
property is optional. If you don't specify it, Kestra will use the default R image. If you want to use a different image, you can specify it in the docker
property as shown below.
id: r_cars
namespace: company.team
tasks:
- id: r
type: io.kestra.plugin.scripts.r.Script
warningOnStdErr: false
containerImage: ghcr.io/kestra-io/rdata:latest
script: "{{ read('main.R') }}"
outputFiles:
- "*.csv"
- "*.parquet"
Properties
interpreter
- Type: array
- SubType: string
- Dynamic: ❌
- Required: ✔️
- Default:
[ "/bin/sh", "-c" ]
- Min items:
1
Which interpreter to use.
script
- Type: string
- Dynamic: ✔️
- Required: ✔️
- Min length:
1
The inline script content. This property is intended for the script file's content as a (multiline) string, not a path to a file. To run a command from a file such as Rscript main.R
or python main.py
, use the corresponding Commands
task for a given language instead.
warningOnStdErr
- Type:
- boolean
- string
- Dynamic: ✔️
- Required: ✔️
beforeCommands
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
A list of commands that will run before the commands
, allowing to set up the environment e.g. pip install -r requirements.txt
.
containerImage
- Type: string
- Dynamic: ❌
- Required: ❌
- Default:
r-base
The task runner container image, only used if the task runner is container-based.
docker
- Type: DockerOptions
- Dynamic: ❌
- Required: ❌
Deprecated - use the 'taskRunner' property instead.
Only used if the
taskRunner
property is not set
env
- Type: object
- SubType: string
- Dynamic: ✔️
- Required: ❌
Additional environment variables for the current process.
failFast
- Type:
- boolean
- string
- Dynamic: ✔️
- Required: ❌
inputFiles
- Type:
- object
- string
- Dynamic: ✔️
- Required: ❌
The files to create on the local filesystem. It can be a map or a JSON object.
namespaceFiles
- Type: NamespaceFiles
- Dynamic: ❌
- Required: ❌
Inject namespace files.
Inject namespace files to this task. When enabled, it will, by default, load all namespace files into the working directory. However, you can use the
include
orexclude
properties to limit which namespace files will be injected.
outputDirectory
- Type: boolean
- Dynamic: ❌
- Required: ❌
- Default:
false
Whether to setup the output directory mechanism.
Required to use the expression. Note that it could increase the starting time. Deprecated, use the
outputFiles
property instead.
outputFiles
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
The files from the local filesystem to send to Kestra's internal storage.
Must be a list of glob) expressions relative to the current working directory, some examples:
my-dir/
,my-dir/*/
ormy-dir/my-file.txt
.
runner
- Type: string
- Dynamic: ❌
- Required: ❌
- Possible Values:
PROCESS
DOCKER
Deprecated - use the 'taskRunner' property instead.
Only used if the
taskRunner
property is not set
targetOS
- Type: string
- Dynamic: ✔️
- Required: ❌
- Default:
AUTO
- Possible Values:
LINUX
WINDOWS
AUTO
The target operating system where the script will run.
taskRunner
- Type: Object_
- Dynamic: ❌
- Required: ❌
- Default:
{ "type": "io.kestra.plugin.scripts.runner.docker.Docker" }
The task runner to use.
Task runners are provided by plugins, each have their own properties.
Outputs
exitCode
- Type: integer
- Required: ✔️
- Default:
0
outputFiles
- Type: object
- SubType: string
- Required: ❌
taskRunner
- Type: TaskRunnerDetailResult
- Required: ❌
vars
- Type: object
- Required: ❌
Definitions
io.kestra.core.models.tasks.NamespaceFiles
enabled
- Type:
- boolean
- string
- Dynamic: ✔️
- Required: ❌
- Type:
exclude
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
include
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
io.kestra.plugin.scripts.runner.docker.Cpu
cpus
- Type: integer
- Dynamic: ❌
- Required: ❌
io.kestra.plugin.scripts.runner.docker.Memory
kernelMemory
- Type: string
- Dynamic: ✔️
- Required: ❌
memory
- Type: string
- Dynamic: ✔️
- Required: ❌
memoryReservation
- Type: string
- Dynamic: ✔️
- Required: ❌
memorySwap
- Type: string
- Dynamic: ✔️
- Required: ❌
memorySwappiness
- Type: string
- Dynamic: ✔️
- Required: ❌
oomKillDisable
- Type: boolean
- Dynamic: ❌
- Required: ❌
io.kestra.plugin.scripts.exec.scripts.models.DockerOptions
image
- Type: string
- Dynamic: ✔️
- Required: ✔️
- Min length:
1
config
- Type:
- string
- object
- Dynamic: ✔️
- Required: ❌
- Type:
cpu
- Type: Cpu
- Dynamic: ❌
- Required: ❌
credentials
- Type: Credentials
- Dynamic: ✔️
- Required: ❌
deviceRequests
- Type: array
- SubType: DeviceRequest
- Dynamic: ❌
- Required: ❌
entryPoint
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
extraHosts
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
host
- Type: string
- Dynamic: ✔️
- Required: ❌
memory
- Type: Memory
- Dynamic: ❌
- Required: ❌
networkMode
- Type: string
- Dynamic: ✔️
- Required: ❌
privileged
- Type: boolean
- Dynamic: ✔️
- Required: ❌
pullPolicy
- Type: string
- Dynamic: ❌
- Required: ❌
- Default:
ALWAYS
- Possible Values:
IF_NOT_PRESENT
ALWAYS
NEVER
shmSize
- Type: string
- Dynamic: ✔️
- Required: ❌
user
- Type: string
- Dynamic: ✔️
- Required: ❌
volumes
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
io.kestra.plugin.scripts.runner.docker.Credentials
auth
- Type: string
- Dynamic: ✔️
- Required: ❌
identityToken
- Type: string
- Dynamic: ✔️
- Required: ❌
password
- Type: string
- Dynamic: ✔️
- Required: ❌
registry
- Type: string
- Dynamic: ✔️
- Required: ❌
registryToken
- Type: string
- Dynamic: ✔️
- Required: ❌
username
- Type: string
- Dynamic: ✔️
- Required: ❌
io.kestra.core.models.tasks.runners.TaskRunnerDetailResult
io.kestra.plugin.scripts.runner.docker.DeviceRequest
capabilities
- Type: array
- SubType: array
- Dynamic: ❌
- Required: ❌
count
- Type: integer
- Dynamic: ❌
- Required: ❌
deviceIds
- Type: array
- SubType: string
- Dynamic: ✔️
- Required: ❌
driver
- Type: string
- Dynamic: ✔️
- Required: ❌
options
- Type: object
- SubType: string
- Dynamic: ❌
- Required: ❌