File: runexamples.R

package info (click to toggle)
r-cran-rdpack 2.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,156 kB
  • sloc: sh: 13; makefile: 4
file content (268 lines) | stat: -rw-r--r-- 9,045 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

.comment <- function(x){
    x
    invisible(NULL)
}

## see also > utils::example
run_examples <- function(excontent,     # package = NULL, lib.loc = NULL, character.only = FALSE,
                         give.lines = FALSE,
                         local,            # in utils::example()  it is:  local = FALSE,
                         echo = TRUE,
                         verbose = getOption("verbose"),
                         setRNG = FALSE,
                         ask = FALSE,             # in utils::example() it is:  getOption("example.ask"),
                         ## prompt.prefix = "Rdpack" # in utils::example() it is:  abbreviate(topic, 6),
                         prompt.echo = "Rdpack> ",
                         continue.echo = prompt.echo,
                         run.dontrun = FALSE,
                         run.donttest = FALSE,     # in utils::example() it is:  interactive()
                         Rdsection = "examples",
                         escape = TRUE
                         ){
    ## set the environment where source() evaluates the code;
    ## 2018-08-13 todo: it is not clear if this is the best choice;
    ##     maybe could pass an environment from the macro \printExample, similarly
    ##     to the citation macros (but first need to check if the cause is the choice
    ##     made here:
    if(missing(local))
        local <- parent.frame()

    # dontshow <- function(x){
    #     x
    # }

        # saveRDS(excontent, "excontent.RDS")

    excontent <- deparse(excontent)
    excontent <- gsub("^[ ]*\\{", "", excontent)
    excontent <- gsub("\\}[ ]*$", "", excontent)

    excontent <- gsub(paste0("^[ ]*", ".comment", "[ ]*\\(\"(.*)\"\\)[ ]*$"), "\\1", excontent)

    ## if(interactive())
    ##     browser()


    # file <- tempfile(fileext = ".Rd")

    tf <- tempfile("Rex")
    ## tools::Rd2ex(.getHelpFile(file), tf, commentDontrun = !run.dontrun,
    ##              commentDonttest = !run.donttest)
    writeLines(excontent, tf)
    ## if (!file.exists(tf)) {
    ##     if (give.lines)
    ##         return(character())
    ##     warning(gettextf("%s has a help file but no examples",
    ##         sQuote(topic)), domain = NA)
    ##     return(invisible())
    ## }

    on.exit(unlink(tf))
    ## if (give.lines)
    ##     return(readLines(tf))

    ## if (pkgname != "base")
    ##     library(pkgname, lib.loc = lib, character.only = TRUE)
    if (!is.logical(setRNG) || setRNG) {
        if ((exists(".Random.seed", envir = .GlobalEnv))) {
            oldSeed <- get(".Random.seed", envir = .GlobalEnv)
            on.exit(assign(".Random.seed", oldSeed, envir = .GlobalEnv),
                add = TRUE)
        }
        else {
            oldRNG <- RNGkind()
            on.exit(RNGkind(oldRNG[1L], oldRNG[2L]), add = TRUE)
        }
        if (is.logical(setRNG)) {
            RNGkind("default", "default")
            set.seed(1)
        }
        else eval(setRNG)
    }
    zz <- readLines(tf, n = 1L)
    skips <- 0L
    ## if (echo) {
    ##     zcon <- file(tf, open = "rt")
    ##     while (length(zz) && !length(grep("^### \\*\\*", zz))) {
    ##         skips <- skips + 1L
    ##         zz <- readLines(zcon, n = 1L)
    ##     }
    ##     close(zcon)
    ## }
    if (ask == "default")
        ask <- echo && grDevices::dev.interactive(orNone = TRUE)
    if (ask) {
        if (.Device != "null device") {
            oldask <- grDevices::devAskNewPage(ask = TRUE)
            if (!oldask)
                on.exit(grDevices::devAskNewPage(oldask), add = TRUE)
        }
        op <- options(device.ask.default = TRUE)
        on.exit(options(op), add = TRUE)
    }

    ## TODO: argument 'spaced' (and maybe others?) were introduced in R-3.4.0.
    wrk <- capture.output(
        if(getRversion() >= '3.4.0')
            source(tf, local, echo = echo,
                   prompt.echo = prompt.echo,     # paste0(prompt.prefix, getOption("prompt")),
                   continue.echo = continue.echo, # paste0(prompt.prefix, getOption("continue")),
                   spaced = FALSE, # do not print empty line before each source line
                   verbose = verbose, max.deparse.length = Inf,
                   encoding = "UTF-8", skip.echo = skips, keep.source = TRUE)
        else
            source(tf, local, echo = echo,
                   prompt.echo = prompt.echo,     # paste0(prompt.prefix, getOption("prompt")),
                   continue.echo = continue.echo, # paste0(prompt.prefix, getOption("continue")),
                   # spaced = FALSE, - no such argument before R-3.4.0
                   verbose = verbose, max.deparse.length = Inf,
                   encoding = "UTF-8", skip.echo = skips, keep.source = TRUE)
    )


        # \Sexpr[stage=build,results=rd]{
        #     paste0("\\\\examples{", {
        #         pf <- "Rdpack"
        #         tmp <- capture.output(utils::example("ES", prompt.prefix = pf))
        #         flags <- grepl("Rdpack> ", tmp)
        #         tmp[!flags] <- paste0("#> ", tmp[!flags])
        #         tmp <- gsub("Rdpack> ", "", tmp)
        #         res <- paste0(tmp, collapse = "\n")
        #     }, "}", collapse = "\n")}

    # browser()

    flags <- grepl(paste0("^", prompt.echo), wrk)
    outflags <- !flags & !grepl("^[ ]*$", wrk)
    wrk[outflags] <- paste0("##: ", wrk[outflags])
    wrk <- gsub(paste0("^", prompt.echo, "[ ]*"), "", wrk)
    ## wrk <- gsub(paste0("^[ ]*", "identity", "[ ]*\\(\"(.*)\"\\)[ ]*$"), "\\1", wrk)
    wrk <- gsub("^## *$", "", wrk)

    res <- paste0(paste0(wrk, collapse = "\n"), "\n")

    if(escape)  # 2018-08-25
        res <- .bspercent(res)

    ## TODO: prefix with spaces to indent from surrounding text?
    if(is.character(Rdsection)){
        res <- paste0("\\", Rdsection, "{", res, "}\n")
    }

#    if(interactive())
#        browser()
    res
}


## cat(Rdpack:::run_examples(quote({2+2
## ## trig
## sin(pi)})))
##
## withAutoprint(quote({2+2
## ## trig
## sin(pi)}))
##
## e1 <-
## (quote({2+2
## ## trig
## sin(pi)}))
##
## attr(e1, "wholeSrcref")
##
##
## e2 <-
## quote({2+2
## ## trig
## sin(pi)})
##
## attr(e2, "wholeSrcref")
##
## e2a <-
## quote({2+2
## sin(pi)})
##
## attr(e2a, "wholeSrcref")
##
## unclass(attr(e2a, "wholeSrcref"))
##
## e3 <-
## quote(2+2)
##
## attr(e3, "wholeSrcref")


## Rdpack:::run_examples(quote({cvar::VaR(qnorm, x = c(0.01, 0.05), dist.type = "qf"); 2*3;  2 + 2; a <- 2 - 2; b <- 2/3 }))


insert_fig <- function(file, package, code, insert = TRUE){
    res <- if(insert)
               paste0("\\figure{", file, "}")
           else
               file

    dirs <- c("./man/", file.path(".", package, "man"))
    w <- sapply(dirs, dir.exists)
    dcur <- dirs[w]
    if(length(dcur) == 2) { # both, ./man and ./package/man exist
        warning(paste0("Ambiguity: both, './man' and './", package, "/man' exist,",
                       "choosing the latter."))
        dcur <- dcur[2]
    } else if(length(dcur) == 0) {
        ## try harder
        pat <- paste0("^", package, ".+") # dir name is pkg followed by something else (e.g. version)
        wrk <- intersect(dir(pattern = pat), list.dirs(full.names = FALSE, recursive = FALSE))

        if(length(wrk) == 0) {
            warning("Unable to locate 'man/figure/' to write the graphics,\n",
                    "please contact the maintainer of 'Rdpack';\n\n",
                    "using previously created figure, if available.")

            return(res)
        }

        man_dir <- character(0)
        for(wdir in wrk) {
            dirs <- c(file.path(".", wdir, "man"), file.path(".", wdir, package, "man"))
            w <- sapply(dirs, dir.exists)
            man_dir <- c(man_dir, dirs[w])
        }

        if(length(man_dir) == 1) {
            dcur <- man_dir
        } else if(length(man_dir) > 1) {
            warning("Ambiguity: more than one package directories found:\n",
                    "    ", paste(man_dir, collapse = ", "), "\n",
                    "please contact the maintainer of 'Rdpack';\n\n",
                    "writing to the last directory found.")

            ## TODO: probably should not write to a potentially unexpected directory;
            ##       should just return(res), as above.
            dcur <- man_dir[length(man_dir)]
        } else { # length(man_dir) == 0
            warning("Unable to locate 'man/figure/' to write the graphics,\n",
                    "please contact the maintainer of 'Rdpack';\n\n",
                    "using previously created figure, if available.")

            return(res)
        }
    }
  
    figpath <- file.path(dcur, "figures")
    if(!dir.exists(figpath)) {
        flag <- dir.create(figpath)
        if(!flag) {
            warning("Unable to create 'man/figure/' to write the graphics,\n",
                    "using previously created figure, if available.")
            return(res)
        }
    }

    grDevices::png(file.path(figpath, file))
    on.exit(grDevices::dev.off())
  
    force(code)

    res
}