vignettes/a01_single-rdo-without-dependencies.Rmd
a01_single-rdo-without-dependencies.RmdLet’s create a single RDO without any dependencies. Our ROD object will be encapsulating mtcars dataset under different name data_mtcars and the reproducible R code.
# library(RDO)
data_mtcars <- RDO::RDO$new(name = "data_mtcars")
data_mtcars
#> <RDO>
#> Name: data_mtcars
#> Dependencies: none.
#> Status:
#> - created: 2020-03-09 13:59:47 UTC
#> - last changed: 2020-03-09 13:59:47 UTC
#> - last touched: (never)
#> - last validated: (never)
#> - last run time:
#> - run time total: 0
#> - is validated? FALSE
#> - is locked? FALSE
#> - cache size: 0 Mb
#> - cached total: 0 MbPrinting the object gives you the name of the object, dependencies if there are any, and statuses.
Let’s add the reproducible code. The code needs to be of expression class. We need simply to wrap our code in expression({ }).
data_mtcars$code <- expression({
data_mtcars <- mtcars
})
data_mtcars
#> <RDO>
#> Name: data_mtcars
#> Dependencies: none.
#> Status:
#> - created: 2020-03-09 13:59:47 UTC
#> - last changed: 2020-03-09 13:59:47 UTC
#> - last touched: 2020-03-09 13:59:47 UTC
#> - last validated: (never)
#> - last run time:
#> - run time total: 0
#> - is validated? FALSE
#> - is locked? FALSE
#> - cache size: 0 Mb
#> - cached total: 0 MbIf we changing the code or the cached data the timestamp last changed is updated. Reading the cache changes the last touched timestamp, so we know when the RDO was last used.
We can access the cached data via active binding cache.
The cache is NULL as the code has never been run yet and the data haven’t been cached yet. Let’s do it for the first time…
data_mtcars$run()
#> Evaluating RDO: data_mtcars ... done!
#> ...evaluation of data_mtcars completed.If we don’t want to see messages printed out to the console we can turn it off in RDO methods using verbose parameter (default is TRUE).
data_mtcars$run(verbose = FALSE)
head(data_mtcars$cache)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
data_mtcars
#> <RDO>
#> Name: data_mtcars
#> Dependencies: none.
#> Status:
#> - created: 2020-03-09 13:59:47 UTC
#> - last changed: 2020-03-09 13:59:47 UTC
#> - last touched: 2020-03-09 13:59:47 UTC
#> - last validated: 2020-03-09 13:59:47 UTC
#> - last run time: 0.0170009136199951
#> - run time total: 0.0170009136199951
#> - is validated? TRUE
#> - is locked? FALSE
#> - cache size: 0.0069 Mb
#> - cached total: 0.0069 MbNow the data cache is available. We can see how big is the size of the RDO cache.
After the code was evaluated and the result was cached, the status is validated? changed to TRUE. This is called implicit validation. We can confirm it with is_validated() method.
We can access the name of the RDO using get_name() method.
The get_status method returns the list of different statuses.
data_mtcars$get_status()
#> $name
#> [1] "data_mtcars"
#>
#> $created
#> [1] "2020-03-09 13:59:47 UTC"
#>
#> $changed
#> [1] "2020-03-09 13:59:47 UTC"
#>
#> $touched
#> [1] "2020-03-09 13:59:47 UTC"
#>
#> $validated
#> [1] "2020-03-09 13:59:47 UTC"
#>
#> $is_validated
#> [1] TRUE
#>
#> $is_locked
#> [1] FALSE
#>
#> $run_time
#> Time difference of 0 secs
#>
#> $run_time_total
#> [1] 0
#>
#> $cache_size
#> 7208 bytes
#>
#> $cache_size_total
#> 7208 bytesTo access the code as a text we can use code active binding. It returns the RDO (self) but before that it prints out to the console the RDO code.
data_mtcars$code
#> <RDO code>
#> data_mtcars <- mtcars
#> <RDO>
#> Name: data_mtcars
#> Dependencies: none.
#> Status:
#> - created: 2020-03-09 13:59:47 UTC
#> - last changed: 2020-03-09 13:59:47 UTC
#> - last touched: 2020-03-09 13:59:47 UTC
#> - last validated: 2020-03-09 13:59:47 UTC
#> - last run time: 0
#> - run time total: 0
#> - is validated? TRUE
#> - is locked? FALSE
#> - cache size: 0.0069 Mb
#> - cached total: 0.0069 MbThe print_code() method can give you similar effect.
If you want to get your code as R expressions you can use get_code() method. The returned expression can be than evaluated manually. You can also use run_code(cache = FALSE) method for that purpose. The code will be evaluated but result will not be cached.
data_mtcars$get_code()
#> expression(data_mtcars = {
#> data_mtcars <- mtcars
#> })
eval_result <- eval(data_mtcars$get_code(), envir = new.env())
head(eval_result)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
head(data_mtcars$run(cache = FALSE))
#> Evaluating RDO: data_mtcars ... done!
#> ...evaluation of data_mtcars completed.
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1You can reset your cache to by setting its value to NULL, but you cannot do it if the RDO is locked.
data_mtcars$cache <- NULL
data_mtcars$run()$lock()$is_locked()
#> Evaluating RDO: data_mtcars ... done!
#> ...evaluation of data_mtcars completed.
#> Locking RDO: 'data_mtcars' ... done.
#> RDO: 'data_mtcars' is LOCKED!
#> [1] TRUE
data_mtcars$cache <- NULL
#> Error in (function (value) : This RDO is locked! Cannot overwrite the RDO's cache.
data_mtcars$unlock()
#> Unlocking RDO: 'data_mtcars' ... done.Running the code and caching the result implicitly validating the RDO, but there is also an explicit validation. Explicit Validation is done using validate() method. The method is evaluating the reproducible code and the result is compare to the cached data. If they are the same, the RDO is validated.
data_mtcars$cache <- iris
data_mtcars$validate()$is_validated()
#> Validating RDO: 'data_mtcars' ... NOT validated!
#> RDO: 'data_mtcars' is NOT VALIDATED!
#> [1] FALSE
data_mtcars$cache <- mtcars
data_mtcars$validate()$is_validated()
#> Validating RDO: 'data_mtcars' ... done.
#> RDO: 'data_mtcars' is validated.
#> [1] TRUE