Skip to contents

Code Class

Code Class

Active bindings

code

(list) Derive the code of the dataset.

Methods


Method new()

CodeClass constructor

Usage

CodeClass$new(
  code = character(0),
  dataname = character(0),
  deps = character(0)
)

Arguments

code

(character) vector of code text to be set

dataname

optional, (character) vector of datanames to assign code to. If empty then the code is considered to be "global"

deps

optional, (character) vector of datanames that given code depends on

Returns

object of class CodeClass


Method append()

Append CodeClass object to a given CodeClass object

Usage

CodeClass$append(x)

Arguments

x

(CodeClass) object to be appended

Returns

changed CodeClass object


Method set_code()

Set code in form of character

Usage

CodeClass$set_code(code, dataname = character(0), deps = character(0))

Arguments

code

(character) vector of code text to be set

dataname

optional, (character) vector of datanames to assign code to. If empty then the code is considered to be "global"

deps

optional, (character) vector of datanames that given code depends on

Returns

changed CodeClass object


Method get_code()

Get the code for a given data names

Usage

CodeClass$get_code(dataname = NULL, deparse = TRUE)

Arguments

dataname

optional, (character) vector of datanames for which the code is extracted. If NULL then get the code for all data names

deparse

optional, (logical) whether to return the deparsed form of a call

Returns

character or list of calls


Method eval()

Evaluates internal code within given environment

Usage

CodeClass$eval(envir = new.env(parent = parent.env(.GlobalEnv)))

Arguments

envir

(environment) environment in which code will be evaluated

Returns

invisibly NULL


Method clone()

The objects of this class are cloneable with this method.

Usage

CodeClass$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

cc <- teal.data:::CodeClass$new()
cc$set_code(c("foo <- function() {1}", "foo2 <- function() {2}"))
cc$get_code()
#> [1] "foo <- function() {\n    1\n}\nfoo2 <- function() {\n    2\n}"
cc$get_code(deparse = FALSE)
#> [[1]]
#> foo <- function() {
#>     1
#> }
#> 
#> [[2]]
#> foo2 <- function() {
#>     2
#> }
#> 

cc$set_code(c("DF <- data.frame(x = 1:10)", "DF$y <- 1"), "DF")
cc$set_code("DF$a <- foo()", "DF")

# dependent dataset
cc$set_code(c("DF2 <- data.frame(x2 = 1:10)", "DF2$y2 <- DF$y"), "DF2", deps = "DF")

cc$set_code("baz <- function() {2}")
cc$set_code("DF2$a <- baz()", "DF2")

cc$get_code()
#> [1] "foo <- function() {\n    1\n}\nfoo2 <- function() {\n    2\n}\nDF <- data.frame(x = 1:10)\nDF$y <- 1\nDF$a <- foo()\nDF2 <- data.frame(x2 = 1:10)\nDF2$y2 <- DF$y\nbaz <- function() {\n    2\n}\nDF2$a <- baz()"
cc$get_code("DF")
#> [1] "foo <- function() {\n    1\n}\nfoo2 <- function() {\n    2\n}\nDF <- data.frame(x = 1:10)\nDF$y <- 1\nDF$a <- foo()"
cc$get_code("DF2")
#> [1] "foo <- function() {\n    1\n}\nfoo2 <- function() {\n    2\n}\nDF <- data.frame(x = 1:10)\nDF$y <- 1\nDF$a <- foo()\nDF2 <- data.frame(x2 = 1:10)\nDF2$y2 <- DF$y\nbaz <- function() {\n    2\n}\nDF2$a <- baz()"


x1 <- teal.data:::CodeClass$new()
x1$set_code("DF <- data.frame(x = 1:10)", "DF")
x1$get_code()
#> [1] "DF <- data.frame(x = 1:10)"

x2 <- teal.data:::CodeClass$new()
x2$set_code(c("DF2 <- data.frame(x2 = 1:10)", "DF2$x2 <- DF$x"), "DF2", deps = "DF")
x2$get_code()
#> [1] "DF2 <- data.frame(x2 = 1:10)\nDF2$x2 <- DF$x"

x <- teal.data:::CodeClass$new()
x$append(x1)
x$append(x2)

x$get_code()
#> [1] "DF <- data.frame(x = 1:10)\nDF2 <- data.frame(x2 = 1:10)\nDF2$x2 <- DF$x"
x$get_code("DF")
#> [1] "DF <- data.frame(x = 1:10)"
x$get_code("DF2")
#> [1] "DF <- data.frame(x = 1:10)\nDF2 <- data.frame(x2 = 1:10)\nDF2$x2 <- DF$x"
x$get_code(c("DF", "DF2"))
#> [1] "DF <- data.frame(x = 1:10)\nDF2 <- data.frame(x2 = 1:10)\nDF2$x2 <- DF$x"

x3 <- teal.data:::CodeClass$new()
x3$set_code("DF3 <- data.frame(x3 = 1:10) ", "DF3")
x3$get_code()
#> [1] "DF3 <- data.frame(x3 = 1:10)"

x$append(x3)
x$get_code("DF3")
#> [1] "DF3 <- data.frame(x3 = 1:10)"

# mutation simulation
x$set_code("DF3$x <- foo(DF$x)", "DF3", deps = "DF")
x$get_code("DF3")
#> [1] "DF <- data.frame(x = 1:10)\nDF3 <- data.frame(x3 = 1:10)\nDF3$x <- foo(DF$x)"