Code Class
CodeClass.Rd
Code Class
Code Class
Methods
Method new()
CodeClass
constructor
Method append()
Append CodeClass
object to a given CodeClass
object
Method set_code()
Set code in form of character
Method get_code()
Get the code for a given data names
Method eval()
Evaluates internal code within given environment
Usage
CodeClass$eval(envir = new.env(parent = parent.env(.GlobalEnv)))
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)"