Skip to content

Commit bcaddb5

Browse files
committed
tidy
1 parent 6d728cd commit bcaddb5

File tree

5 files changed

+95
-271
lines changed

5 files changed

+95
-271
lines changed

tools/archive/make.R

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,8 @@ if(!"source:tools/utils.R" %in% search()) envir::attach_source("tools/utils.R")
55

66

77

8-
## ---- Deferred ----
9-
10-
# TODO: k_fft() should really accept and return complex tensors too.
11-
# TODO: activation_resolve() or activation_get() as alias of keras.activation.get() ?
12-
# TODO: the 2-translated.Rmd should include a chunk w/ the function def (for easy seeing while editing)
13-
# with chunk options (include = FALSE)
14-
# TODO: add PR for purrr::rate_throttle("3 per minute")
158
# TODO: layer_feature_space should take a formula, and dispatch to the features as required.
169
# ~ scale(foo) * bar
17-
# TODO: layer_feature_space() needs massaging.
18-
# TODO: Add arg Layer(composing = TRUE)?
19-
# TODO: custom keras.Model wrapper that patches add_weight(), build(), etc
20-
# with proper shape() coercion
21-
22-
23-
24-
## Rejected ----
25-
# TODO: k_array() should take a 'shape' argument
26-
# TODO: r_name autogen: move "set" to tail, so have config_floatx(), config_floatx_set()
27-
# TODO: implement dim() S3 generic. (will use shape() instead)
2810

2911

3012
## Implemented but Questioning ----
File renamed without changes.

tools/knit.R

Lines changed: 94 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33

44
knit_keras_init <- function(backend = NULL) {
5+
6+
# reticulate::use_virtualenv("r-keras")
57
if(!is.null(backend))
68
keras3::use_backend(backend)
7-
# reticulate::use_virtualenv("r-keras")
89
options(width = 76)
910

1011
keras_init <- function() {
@@ -16,97 +17,6 @@ knit_keras_init <- function(backend = NULL) {
1617
reticulate:::py_register_load_hook("keras", keras_init)
1718
}
1819

19-
yaml.load <- getExportedValue("yaml", "yaml.load")
20-
as.yaml <- getExportedValue("yaml", "as.yaml")
21-
22-
knit_man <- function(input, ..., output_dir) {
23-
24-
cli::cli_alert('knit_man_src("{.file {input}}")')
25-
# message("knit_man_src(", glue::double_quote(input), ")")
26-
library(keras3)
27-
input <- normalizePath(input)
28-
dir <- dirname(input)
29-
withr::local_dir(dir)
30-
input <- basename(input)
31-
32-
fig.path <- paste0(basename(dir), "-")
33-
unlink(Sys.glob(paste0("../../man/figures/", fig.path, "*.svg")))
34-
unlink(Sys.glob(paste0("../../man/figures/", fig.path, "*.png")))
35-
# unlink(Sys.glob(paste0(fig.path, "*.svg")))
36-
unlink(Sys.glob("*.svg"))
37-
38-
og_knitr_chunks <- knitr::opts_chunk$get()
39-
on.exit(do.call(knitr::opts_chunk$set, og_knitr_chunks), add = TRUE)
40-
41-
knitr::render_markdown()
42-
knitr::opts_chunk$set(
43-
# error = FALSE,
44-
# fig.path = fig.path,
45-
fig.width = 7, fig.height = 7,
46-
dev = "svg"
47-
)
48-
49-
# og_output_hook <- knitr::knit_hooks$get("output")
50-
# output <- function(x, options) {
51-
# x <- .keras_knit_process_chunk_output(x, options)
52-
# og_output_hook(x, options)
53-
# }
54-
55-
output <- input |> fs::path_ext_set("md")
56-
57-
# knitr::render_markdown()
58-
# on.exit(knitr::knit_hooks$restore())
59-
60-
knit_keras_init()
61-
62-
knitr::knit(input, output, quiet = TRUE,
63-
envir = new.env(parent = globalenv()))
64-
65-
# figs <- Sys.glob(paste0(fig.path, "*.svg"))
66-
figs <- Sys.glob("*.svg")
67-
68-
if (length(figs)) {
69-
link_path <- fs::path("../../man/figures", basename(figs))
70-
link_target <- fs::path_rel(figs, dirname(link_path))
71-
fs::link_create(link_target, link_path)
72-
message("creating link ", link_path, " -> ", link_target)
73-
}
74-
75-
# x <- readLines("3-rendered.md")
76-
x <- readLines(output)
77-
x <- trimws(x, "right")
78-
79-
if(x[1] == "---") {
80-
stopifnot(x[3] == "---")
81-
x <- x[-(1:3)]
82-
while(x[1] == "") x <- x[-1]
83-
}
84-
85-
# x <- process_chunk_output(x)
86-
87-
writeLines(x, output, useBytes = TRUE)
88-
89-
}
90-
91-
92-
knit_keras_process_chunk_output <- function(x, options) {
93-
# this hook get called with each chunk output.
94-
# x is a single string of collapsed lines, terminated with a final \n
95-
final_new_line <- endsWith(x[length(x)], "\n")
96-
x <- x |> strsplit("\n") |> unlist() |> trimws("right")
97-
98-
# strip object addresses; no noisy diff
99-
x <- sub(" at 0[xX][0-9A-Fa-f]{9,16}>$", ">", x, perl = TRUE)
100-
101-
# remove reticulate hint from exceptions
102-
x <- x[!grepl(r"{## .*rstudio:run:reticulate::py_last_error\(\).*}", x)]
103-
x <- x[!grepl(r"{## .*reticulate::py_last_error\(\).*}", x)]
104-
105-
x <- paste0(x, collapse = "\n")
106-
if(final_new_line && !endsWith(x, "\n"))
107-
x <- paste0(x, "\n")
108-
x
109-
}
11020

11121

11222
knit_vignette <- function(input, ..., output_dir, external = FALSE) {
@@ -231,15 +141,102 @@ knit_vignette <- function(input, ..., output_dir, external = FALSE) {
231141
}
232142
}
233143

144+
145+
knit_man <- function(input, ..., output_dir) {
146+
147+
cli::cli_alert('knit_man_src("{.file {input}}")')
148+
# message("knit_man_src(", glue::double_quote(input), ")")
149+
library(keras3)
150+
input <- normalizePath(input)
151+
dir <- dirname(input)
152+
withr::local_dir(dir)
153+
input <- basename(input)
154+
155+
fig.path <- paste0(basename(dir), "-")
156+
unlink(Sys.glob(paste0("../../man/figures/", fig.path, "*.svg")))
157+
unlink(Sys.glob(paste0("../../man/figures/", fig.path, "*.png")))
158+
# unlink(Sys.glob(paste0(fig.path, "*.svg")))
159+
unlink(Sys.glob("*.svg"))
160+
161+
og_knitr_chunks <- knitr::opts_chunk$get()
162+
on.exit(do.call(knitr::opts_chunk$set, og_knitr_chunks), add = TRUE)
163+
164+
knitr::render_markdown()
165+
knitr::opts_chunk$set(
166+
# error = FALSE,
167+
# fig.path = fig.path,
168+
fig.width = 7, fig.height = 7,
169+
dev = "svg"
170+
)
171+
172+
# og_output_hook <- knitr::knit_hooks$get("output")
173+
# output <- function(x, options) {
174+
# x <- .keras_knit_process_chunk_output(x, options)
175+
# og_output_hook(x, options)
176+
# }
177+
178+
output <- input |> fs::path_ext_set("md")
179+
180+
# knitr::render_markdown()
181+
# on.exit(knitr::knit_hooks$restore())
182+
183+
knit_keras_init()
184+
185+
knitr::knit(input, output, quiet = TRUE,
186+
envir = new.env(parent = globalenv()))
187+
188+
# figs <- Sys.glob(paste0(fig.path, "*.svg"))
189+
figs <- Sys.glob("*.svg")
190+
191+
if (length(figs)) {
192+
link_path <- fs::path("../../man/figures", basename(figs))
193+
link_target <- fs::path_rel(figs, dirname(link_path))
194+
fs::link_create(link_target, link_path)
195+
message("creating link ", link_path, " -> ", link_target)
196+
}
197+
198+
# x <- readLines("3-rendered.md")
199+
x <- readLines(output)
200+
x <- trimws(x, "right")
201+
202+
if(x[1] == "---") {
203+
stopifnot(x[3] == "---")
204+
x <- x[-(1:3)]
205+
while(x[1] == "") x <- x[-1]
206+
}
207+
208+
# x <- process_chunk_output(x)
209+
210+
writeLines(x, output, useBytes = TRUE)
211+
212+
}
213+
214+
215+
knit_keras_process_chunk_output <- function(x, options) {
216+
# this hook get called with each chunk output.
217+
# x is a single string of collapsed lines, terminated with a final \n
218+
final_new_line <- endsWith(x[length(x)], "\n")
219+
x <- x |> strsplit("\n") |> unlist() |> trimws("right")
220+
221+
# strip object addresses; no noisy diff
222+
x <- sub(" at 0[xX][0-9A-Fa-f]{9,16}>$", ">", x, perl = TRUE)
223+
224+
# remove reticulate hint from exceptions
225+
x <- x[!grepl(r"{## .*rstudio:run:reticulate::py_last_error\(\).*}", x)]
226+
x <- x[!grepl(r"{## .*reticulate::py_last_error\(\).*}", x)]
227+
228+
x <- paste0(x, collapse = "\n")
229+
if(final_new_line && !endsWith(x, "\n"))
230+
x <- paste0(x, "\n")
231+
x
232+
}
233+
234234
if(!interactive())
235235
evalq({
236236
.Last <- function() { message("Finished!") }
237237
}, .GlobalEnv)
238238

239239

240-
# o_knitr.graphics.rel_path <- options(knitr.graphics.rel_path = FALSE)
241-
# on.exit(options(o_knitr.graphics.rel_path), add = TRUE)
242-
243240

244241
yaml_front_matter <- function(infile, lines = readLines(infile)) {
245242
end_fm_i <- which(lines == "---")[2]
@@ -248,147 +245,6 @@ yaml_front_matter <- function(infile, lines = readLines(infile)) {
248245
fm
249246
}
250247

251-
# update absolute figure links so they're relative links to the vignette dir
252-
# lines <- sub(paste0("](", dirname(fig.path), "/"), "](", lines, fixed = TRUE) # md formatting
253-
## could probably move this a wrapped plot hook: knit_hooks$get('plot')
254-
255-
# # html output, as generated by knitr::include_graphics()
256-
# lines <- sub(paste0('src="', dirname(fig.path), "/"), 'src="', lines, fixed = TRUE)
257-
258-
259-
# TODO: move these out of the package namespace, we don't want a knitr dep on cran
260-
# knit_man_src <- function(input, ..., output_dir) {
261-
# library(keras3)
262-
# dir <- dirname(input)
263-
# withr::local_dir(dir)
264-
# message("rendering: ", dir)
265-
# keras$utils$clear_session()
266-
# # Set knitr options to halt on errors
267-
# knitr::opts_chunk$set(error = FALSE)
268-
# file.symlink("man/figures", paste0("../../man/figures/", basename(dir)))
269-
# knitr::opts_chunk$set(fig.path=paste0("man/figures/", basename(dir)))
270-
# knitr::knit("2-translated.Rmd", "3-rendered.md",
271-
# quiet = TRUE, envir = new.env(parent = globalenv()))
272-
# x <- readLines("3-rendered.md")
273-
# x <- trimws(x, "right")
274-
# # TODO: these filters should be confined to chunk outputs only,
275-
# # probably as a knitr hook
276-
# # strip object addresses; no noisy diff
277-
# if(x[1] == "---") {
278-
# stopifnot(x[3] == "---")
279-
# x <- x[-(1:3)]
280-
# while(x[1] == "") x <- x[-1]
281-
# }
282-
# figs <- list.files("man/figures", full.names = TRUE)
283-
# figs_dir <- "man/figures"
284-
# figs_dir2 <- fs::dir_create("../../man/figures/", basename(dir))
285-
#
286-
#
287-
# file.rename(figs, new_figs_loc)
288-
#
289-
# new_figs_loc <- paste0("../../man/figures/", basename(dir), basename(figs))
290-
# file.rename(figs, new_figs_loc)
291-
# file.symlink(figs, new_figs_loc)
292-
#
293-
# x <- sub(" at 0x[0-9A-F]{9}>$", ">", x, perl = TRUE)
294-
# x <- x[!grepl(r"{## .*rstudio:run:reticulate::py_last_error\(\).*}", x)]
295-
# x <- x[!grepl(r"{## .*reticulate::py_last_error\(\).*}", x)]
296-
#
297-
# writeLines(x, "3-rendered.md")
298-
#
299-
# message("Done! file.edit('", file.path(dir, "3-rendered.md"), "')")
300-
#
301-
# }
302-
303-
304-
305-
# x <- sub("](figures/", "](", x, fixed = TRUE)
306-
# x <- sub("](man/figures/", "](", x, fixed = TRUE)
307-
308-
# figs <- list.files("man/figures", full.names = TRUE)
309-
# figs_dir <- "man/figures"
310-
# figs_dir2 <- fs::dir_create("../../man/figures/", basename(dir))
311-
# # source(here::here("tools/knit.R"))$knit_man_src
312-
#
313-
# file.rename(figs, new_figs_loc)
314-
#
315-
# new_figs_loc <- paste0("../../man/figures/", basename(dir), basename(figs))
316-
# file.rename(figs, new_figs_loc)
317-
# file.symlink(figs, new_figs_loc)
318-
# link_create(
319-
# fs::path_rel(figs, "../../man/figures")
320-
# fs::path("../../man/figures", figs)
321-
# )
322-
# browser()
323-
# if(!length(Sys.glob(paste0("figures/", basename(dir), "-*")))) {
324-
# # unlink(true_figs_dir)
325-
# unlink(fake_figs_dir)
326-
# }
327-
# environment()
328-
329-
# if(FALSE) {
330-
#
331-
# true_figs_dir <- paste0("../../man/figures/", basename(dir))
332-
# fake_figs_dir <- paste0("man/figures/", basename(dir))
333-
#
334-
# message('Sys.readlink("man/figures") ', Sys.readlink("man/figures"))
335-
# # unlink(Sys.readlink("man/figures"), recursive = TRUE, force = TRUE)
336-
# # unlink("man/figures", recursive = TRUE, force = TRUE)
337-
# unlink("man", recursive = TRUE, force = TRUE)
338-
# unlink(true_figs_dir, recursive = TRUE, force = TRUE)
339-
# dir.create(true_figs_dir, recursive = TRUE)
340-
# dir.create(dirname(fake_figs_dir), recursive = TRUE)
341-
# file.symlink(paste0("../../", true_figs_dir),
342-
# fake_figs_dir)
343-
# }
344-
if(FALSE) {
345-
346-
if(FALSE) {
347-
348-
true_figs_dir <- paste0("../../man/figures/")
349-
fake_figs_dir <- paste0("man/figures/")
350-
351-
unlink(Sys.glob(paste0("../../man/figures/", basename(dir), "-*")))
352-
# message('Sys.readlink("man/figures") ', Sys.readlink("man/figures"))
353-
# unlink(Sys.readlink("man/figures"), recursive = TRUE, force = TRUE)
354-
# unlink("man/figures", recursive = TRUE, force = TRUE)
355-
unlink("man", recursive = TRUE, force = TRUE)
356-
unlink("figure", recursive = TRUE, force = TRUE)
357-
# unlink(true_figs_dir, recursive = TRUE, force = TRUE)
358-
dir.create(true_figs_dir, recursive = TRUE, showWarnings = FALSE)
359-
dir.create(dirname(fake_figs_dir), recursive = TRUE)
360-
fs::link_create(
361-
paste0("../", true_figs_dir),
362-
fake_figs_dir
363-
)
364-
# file.symlink(paste0("../", true_figs_dir),
365-
# fake_figs_dir)
366-
}
367-
system("ls -alR")
368-
# normalizePath(fake_figs_dir)
369-
if(FALSE) {
370-
371-
true_figs_dir <- paste0("../../man/figures/")
372-
fake_figs_dir <- paste0("figures/")
373-
374-
375-
unlink(Sys.glob(paste0("../../man/figures/", basename(dir), "-*")))
376-
# message('Sys.readlink("man/figures") ', Sys.readlink("man/figures"))
377-
# unlink(Sys.readlink("man/figures"), recursive = TRUE, force = TRUE)
378-
# unlink("man/figures", recursive = TRUE, force = TRUE)
379-
unlink("man", recursive = TRUE, force = TRUE)
380-
unlink("figures", recursive = TRUE, force = TRUE)
381-
# unlink(true_figs_dir, recursive = TRUE, force = TRUE)
382-
# dir.create(true_figs_dir, recursive = TRUE, showWarnings = FALSE)
383-
# dir.create(dirname(fake_figs_dir), recursive = TRUE)
384-
fs::link_create( true_figs_dir, "figures")
385-
# file.symlink(paste0("../", true_figs_dir),
386-
# fake_figs_dir)
387-
# }
388248

389-
knitr::opts_chunk$set(
390-
fig.path = paste0("figures/", basename(dir), "-"),
391-
# fig.width = 3, fig.height = 3, dev = "png"
392-
)
393-
}
394-
}
249+
yaml.load <- getExportedValue("yaml", "yaml.load")
250+
as.yaml <- getExportedValue("yaml", "as.yaml")

tools/make-vignettes.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
library(envir)
2+
23
attach_source("tools/utils.R")
34
# attach_source("tools/common.R")
45

0 commit comments

Comments
 (0)