Skip to content

Commit 1e42a7a

Browse files
author
nteetor
committed
per #6, use #. file$decorator syntax to refer to a decorator function found in a separate file
1 parent 035adc6 commit 1e42a7a

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

R/source-decoratees.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ source_decoratees <- function(file, into = parent.frame()) {
5050
stop('path specified by `file` does not exist', call. = FALSE)
5151
}
5252

53+
# if (!is.null(include) && !is.character(include)) {
54+
# stop('argument `include` must be character', call. = FALSE)
55+
# }
56+
57+
if (!is.environment(into)) {
58+
stop('argument `into` must be environment', call. = FALSE)
59+
}
60+
5361
src <- new.env()
5462
contents <- readLines(file)
5563

@@ -60,6 +68,15 @@ source_decoratees <- function(file, into = parent.frame()) {
6068
}
6169
)
6270

71+
# for (f in include) {
72+
# tryCatch(
73+
# source(file = f, local = src, keep.source = FALSE),
74+
# error = function(e) {
75+
# stop('problem including file "', f, '", ', e$message, call. = FALSE)
76+
# }
77+
# )
78+
# }
79+
6380
fileitr <- itr(contents)
6481
decor <- NULL
6582

@@ -87,7 +104,19 @@ source_decoratees <- function(file, into = parent.frame()) {
87104
as_text <- f
88105
for (d in decor) {
89106
split_at <- first_of(d, '(')
107+
90108
dname <- substr(d, 1, split_at - 1)
109+
if (grepl('$', dname, fixed = TRUE)) {
110+
dfile <- re_search(dname, '^[^$]+')
111+
dsrc <- file.path(dirname(file), paste0(dfile, '.R'))
112+
dname <- re_search(dname, '[^$]+$')
113+
114+
if (!file.exists(dsrc)) {
115+
stop('could not find decorator file "', dsrc, '"', call. = FALSE)
116+
}
117+
source(dsrc, local = src, keep.source = FALSE)
118+
}
119+
91120
dargs <- substr(d, split_at + 1, nchar(d))
92121
if (!grepl('^\\s*\\)\\s*$', dargs)) {
93122
dargs <- paste(',', dargs)

tests/testfiles/includes-files.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#. sample-utils$ramp_to(5)
2+
div_scale <- function() {
3+
cm.colors(3)
4+
}

tests/testfiles/nothing-will-load.R

Whitespace-only changes.

tests/testfiles/sample-utils.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# A decorator which will ramp up a color scale, expects `f()` to return colors
2+
# to interpolate.
3+
ramp_to <- function(f, n) {
4+
function(...) {
5+
colorRampPalette(f(...))(n)
6+
}
7+
}
8+
9+
as_double <- function(f) {
10+
function(...) {
11+
vapply(f(...), as.double, double(1))
12+
}
13+
}

tests/testfiles/simple-functions.R

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,23 @@ if_warning <- function(f, default) {
6565
#. if_warning(Inf)
6666
mean_inf <- mean
6767

68-
bare_variable <- 'necessities'
69-
7068
#. if_warning('whoops!')
7169
one_fish <-
7270
function(two_fish = NULL) {
7371
'red fish, blue fish'
7472
}
7573

74+
# emphasize text
75+
emph <- function(f, begin = '**', end = begin) {
76+
function(...) {
77+
paste(begin, f(...), end)
78+
}
79+
}
7680

77-
# emphasize text
78-
emph <- function(f, m = '**') {
79-
function(...) {
80-
if (is.na(m[2])) m <- rep(m, 2)
81-
paste(m[1], f(...), m[2])
82-
}
83-
}
84-
85-
#. emph
86-
my_name <- function() 'Nathan Teetor'
87-
88-
my_name()
89-
90-
#. emph(c('<b>', '</b>'))
91-
cats <- function(n) {
92-
paste(rep('cats', n), collapse = ' ')
93-
}
94-
95-
cats(5)
81+
#. emph
82+
my_name <- function() 'Nathan Teetor'
9683

84+
#. emph('<b>', '</b>')
85+
cats <- function(n) {
86+
paste(rep('cats', n), collapse = ' ')
87+
}

0 commit comments

Comments
 (0)