Skip to contents

[Experimental]

Usage

mutate_dataset(x, ...)

# S3 method for TealDataset
mutate_dataset(
  x,
  code = character(0),
  script = character(0),
  vars = list(),
  ...
)

# S3 method for TealDatasetConnector
mutate_dataset(
  x,
  code = character(0),
  script = character(0),
  vars = list(),
  ...
)

# S3 method for TealDataAbstract
mutate_dataset(
  x,
  dataname,
  code = character(0),
  script = character(0),
  vars = list(),
  ...
)

Arguments

x

(TealDataset)
object.

...

not used, only for support of S3

code

(character)
Code to mutate the dataset. Must contain the dataset$dataname. Or can also be an object of class PythonCodeClass returned by python_code.

script

(character)
file that contains R Code that can be read using read_script. Preferred before code argument.

vars

(named list))
In case when this object code depends on other TealDataset object(s) or other constant value, this/these object(s) should be included as named element(s) of the list. For example if this object code needs ADSL object we should specify vars = list(ADSL = <adsl object>). It's recommended to include TealDataset or TealDatasetConnector objects to the vars list to preserve reproducibility. Please note that vars are included to this object as local vars and they cannot be modified within another dataset.

dataname

(character)
Dataname to be mutated.

Value

modified x object

Examples

library(scda)
library(magrittr)

ADSL <- synthetic_cdisc_data("latest")$adsl

ADSL_dataset <- dataset(
  dataname = "ADSL",
  x = ADSL,
  label = "AdAM subject-level dataset",
  code = "ADSL <- synthetic_cdisc_data(\"latest\")$adsl"
)
ADSL_mutated <- ADSL_dataset %>%
  mutate_dataset(code = "ADSL$new_variable <- 1")

ADSL_mutated$get_raw_data()$new_variable[1]
#> [1] 1

# Use an R script to mutate the data
file_example <- tempfile(fileext = ".R")
writeLines(
  text = c(
    "ADSL <- ADSL %>%
      dplyr::mutate(new_variable = new_variable * 2)"
  ),
  con = file_example
)

ADSL_mutated <- ADSL_mutated %>%
  mutate_dataset(script = file_example)

ADSL_mutated$get_raw_data()$new_variable[1]
#> [1] 2

ADSL_mutated <- ADSL_mutated %>%
  mutate_dataset(code = read_script(file_example))

ADSL_mutated$get_raw_data()$new_variable[1]
#> [1] 4