Skip to content

Commit

Permalink
factor out unlist(lapply())
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Aug 28, 2024
1 parent efc27da commit 4ec5b54
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions R/fuse.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ crack = function(input, text = NULL) {
j = grepl(rx_inline, m[9, ])
}
m = m[, j, drop = FALSE]
n_start = unlist(lapply(res, function(x) x$lines[1])) # starting line numbers
n_start = uapply(res, function(x) x$lines[1])) # starting line numbers
j = findInterval(m[3, ], n_start) # find which block each inline code belongs to
for (i in seq_len(ncol(m))) {
b = res[[j[i]]]; l = b$lines
Expand Down Expand Up @@ -488,7 +488,7 @@ fiss = function(input, output = '.R', text = NULL) {
output = auto_output(input, output, NULL)
blocks = crack(input, text)
# TODO: what should we do for non-R code? also consider eval=FALSE and error=TRUE
res = unlist(lapply(blocks, function(b) {
res = uapply(blocks, function(b) {
if (b$type == 'code_chunk' && !isFALSE(b$options$purl) && b$options$engine == 'r')
c(b$source, '')
}))
Expand Down Expand Up @@ -637,7 +637,7 @@ fuse_code = function(x, blocks) {
}

# fuse child documents (empty the `child` option to avoid infinite recursion)
if (length(opts$child)) return(unlist(lapply(reactor(child = NULL)$child, function(.) {
if (length(opts$child)) return(uapply(reactor(child = NULL)$child, function(.) {
child = .env$child; .env$child = TRUE; on.exit(.env$child <- child)
fuse(., output = 'markdown', envir = fuse_env(), quiet = TRUE)
})))
Expand All @@ -656,7 +656,7 @@ fuse_code = function(x, blocks) {
} else if (test_source('code')) {
x$source = opts$code
} else if (test_source('ref.label')) {
x$source = unlist(lapply(blocks[opts$ref.label], `[[`, 'source'))
x$source = uapply(blocks[opts$ref.label], `[[`, 'source'))
}

lab = opts$label
Expand Down
2 changes: 1 addition & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ vig_filter = function(ifile, encoding) {
res = lapply(res, function(x) {
if (x$type == 'code_chunk') return(rep('', length(x$source)))
if (is.character(x$source)) x$source else {
one_string(unlist(lapply(x$source, function(s) {
one_string(uapply(x$source, function(s) {
if (is.character(s)) s else ''
})), '')
}
Expand Down
20 changes: 11 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dropNULL = function(x) x[!vapply(x, is.null, logical(1))]

is_lang = function(x) is.symbol(x) || is.language(x)

uapply = function(...) unlist(lapply(...))

#' Convert some ASCII strings to HTML entities
#'
#' Transform ASCII strings `(c)` (copyright), `(r)` (registered trademark),
Expand Down Expand Up @@ -338,7 +340,7 @@ lang_files = function(package, path, langs) {
if (length(l) == 0) return()
# check if language files exist on CDN
d = paste0(dirname(u), '/languages/')
l1 = unlist(lapply(l, function(z) {
l1 = uapply(l, function(z) {
if (downloadable(sprintf('%s%s.min.js', d, z))) z
}))
l2 = setdiff(l, l1)
Expand All @@ -358,7 +360,7 @@ lang_files = function(package, path, langs) {
x = x[1:2]
x = lapply(match_full(x, '([[:alnum:]_-]+):(\\["[^]]+\\]|"[^"]+")'), function(z) {
z = gsub('[]["]', '', z)
unlist(lapply(strsplit(z, '[:,]'), function(y) {
uapply(strsplit(z, '[:,]'), function(y) {
set_names(list(y[-1]), y[1])
}), recursive = FALSE)
})
Expand All @@ -373,12 +375,12 @@ lang_files = function(package, path, langs) {
c(lapply(deps, resolve_deps), lang)
}
# all languages required for this page
l1 = unique(unlist(lapply(langs, resolve_deps)))
l1 = unique(uapply(langs, resolve_deps)))
# languages that are officially supported
l2 = c(names(x1), unlist(x1), x2)
# for unknown languages, check if they exist on CDN
d = sub('/plugins/.+', '/components/', u)
l3 = unlist(lapply(setdiff(l1, l2), function(z) {
l3 = uapply(setdiff(l1, l2), function(z) {
if (!downloadable(sprintf('%sprism-%s.min.js', d, z))) z
}))
l4 = setdiff(l1, l3)
Expand Down Expand Up @@ -417,7 +419,7 @@ add_citation = function(x, bib, format = 'html') {
'(?<!\\{)(\\{\\[\\}@[-;@ [:alnum:]]+\\{\\]\\}|@[-[:alnum:]]+)'
# [@key] for citep, and @key for citet
x = match_replace(x, r, function(z) {
z2 = unlist(lapply(strsplit(z, '[;@ {}]+'), function(keys) {
z2 = uapply(strsplit(z, '[;@ {}]+'), function(keys) {
bracket = any(grepl('^\\[', keys))
if (bracket) keys = gsub('^\\[|\\]$', '', keys)
keys = keys[keys != '']
Expand Down Expand Up @@ -467,7 +469,7 @@ cite_link = function(key, text, class = '') {
# html bibliography
bib_html = function(bib, keys) {
bib = sort(bib[unique(keys)])
keys = unlist(lapply(bib, function(x) attr(unclass(x)[[1]], 'key')))
keys = uapply(bib, function(x) attr(unclass(x)[[1]], 'key')))
res = format(bib, 'html')
paste0('<p id="ref-', keys, '"', sub('^<p', '', res))
}
Expand Down Expand Up @@ -928,7 +930,7 @@ jsd_version = local({
}
})

jsd_versions = function(pkgs) unlist(lapply(pkgs, jsd_version))
jsd_versions = function(pkgs) uapply(pkgs, jsd_version))

# resolve the implicit latest version to current latest version
jsd_resolve = function(x) {
Expand Down Expand Up @@ -1071,7 +1073,7 @@ base64_url = function(url, code, ext) {
if (length(p) == 1) code = match_replace(
code, '(?<=src:\'url\\(")(%%URL%%/[^"]+)(?="\\))', function(u) {
u = sub('%%URL%%', paste(d, p, sep = '/'), u, fixed = TRUE)
unlist(lapply(u, function(x) download_cache$get(x, 'base64')))
uapply(u, function(x) download_cache$get(x, 'base64')))
}
) else warning(
'Unable to determine the font path in MathJax. Please report an issue to ',
Expand All @@ -1087,7 +1089,7 @@ base64_url = function(url, code, ext) {
z3 = gsub(r, '\\3', z)
i = !is_https(z2)
z2[i] = paste(d, z2[i], sep = '/')
z2 = unlist(lapply(z2, function(x) {
z2 = uapply(z2, function(x) {
if (is_https(x)) download_cache$get(x, 'base64') else base64_uri(x)
}))
paste0(z1, z2, z3)
Expand Down

0 comments on commit 4ec5b54

Please sign in to comment.