Debian Codegraph @

inst/doc/evals.R

raw
1 ## ---- echo = FALSE, message = FALSE-------------------------------------------
2 knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
3 library(pander)
4 library(logger)
5 evalsOptions('graph.name', 'test')
6 evalsOptions('graph.dir', 'my_plots')
7 evalsOptions('graph.output', 'jpg')
8
9 ## -----------------------------------------------------------------------------
10 evals('1:10')
11
12 ## -----------------------------------------------------------------------------
13 evals('1:10', output = c('result', 'output'))
14
15 ## -----------------------------------------------------------------------------
16 evals('x')[[1]]$msg
17 evals('as.numeric("1.1a")')[[1]]$msg
18
19 ## -----------------------------------------------------------------------------
20 evals('plot(mtcars)')[[1]]$result
21
22 ## -----------------------------------------------------------------------------
23 evals('plot(mtcars)', graph.dir = 'my_plots', graph.output = 'jpg')[[1]]$result
24
25 ## -----------------------------------------------------------------------------
26 ## generating dataset
27 set.seed(1)
28 df <- mtcars[, c('hp', 'wt')]
29 df$factor <- sample(c('Foo', 'Bar', 'Foo bar'), size = nrow(df), replace = TRUE)
30 df$factor2 <- sample(c('Foo', 'Bar', 'Foo bar'), size = nrow(df), replace = TRUE)
31 df$time <- 1:nrow(df)
32
33 ## ----echo=FALSE---------------------------------------------------------------
34 ## loading packages
35 require(ggplot2, quietly = TRUE)
36 require(lattice, quietly = TRUE)
37
38 ## -----------------------------------------------------------------------------
39 evalsOptions('graph.unify', TRUE)
40 evals('histogram(df$hp, main = "Histogram with lattice")')[[1]]$result
41 evals('ggplot(df) + geom_histogram(aes(x = hp), binwidth = 50) + ggtitle("Histogram with ggplot2")')[[1]]$result
42 evalsOptions('graph.unify', FALSE)
43
44 ## -----------------------------------------------------------------------------
45 x <- evals('1:10', log = 'foo')
46
47 ## -----------------------------------------------------------------------------
48 evalsOptions('log', 'evals')
49 log_threshold(TRACE, namespace = 'evals')
50 x <- evals('1:10', cache.time = 0)
51
52 ## -----------------------------------------------------------------------------
53 t <- tempfile()
54 log_appender(appender_file(t), namespace = 'evals')
55 x <- evals('1:10', log = 'evals')
56 readLines(t)
57 # revert back to console
58 log_appender(appender_stdout, namespace = 'evals')
59
60 ## -----------------------------------------------------------------------------
61 evalsOptions('cache.time', 0)
62 evalsOptions('log', 'evals')
63 log_threshold(TRACE, 'evals')
64
65 ## -----------------------------------------------------------------------------
66 system.time(evals('1:1e5'))
67 system.time(evals('1:1e5'))
68
69 ## -----------------------------------------------------------------------------
70 res <- evals('1:1e5', cache.mode = 'disk', cache.dir = 'cachedir')
71 list.files('cachedir')
72
73 ## -----------------------------------------------------------------------------
74 x <- mtcars$hp
75 y <- 1e3
76 system.time(evals('sapply(rep(x, y), mean)'))
77
78 ## -----------------------------------------------------------------------------
79 f <- sapply
80 g <- rep
81 h <- mean
82 X <- mtcars$hp * 1
83 Y <- 1000
84 system.time(evals('f(g(X, Y), h)'))
85
86 ## -----------------------------------------------------------------------------
87 x <- 1
88 res <- evals('x <- 1:10;')
89
90 ## -----------------------------------------------------------------------------
91 evals('x <- 1:10; x[3]')[[2]]$result
92
93 ## -----------------------------------------------------------------------------
94 system.time(evals('plot(mtcars)'))
95 system.time(evals('plot(mtcars)'))
96
97 ## ---- echo = FALSE, message = FALSE-------------------------------------------
98 unlink('cachedir', recursive = TRUE, force = TRUE)
99 unlink('my_plots', recursive = TRUE, force = TRUE)
100