Skip to content

Commit c090c6a

Browse files
committed
Merge pull request rstudio#410 from wch/faster-tags-2
Faster tags
2 parents 84da0be + 8add9f7 commit c090c6a

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

R/tags.R

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ tagWrite <- function(tag, textWriter, indent=0, eol = "\n") {
190190
# optionally process a list of tags
191191
if (!isTag(tag) && is.list(tag)) {
192192
tag <- dropNullsOrEmpty(flattenTags(tag))
193-
sapply(tag, function(t) tagWrite(t, textWriter, indent))
193+
lapply(tag, tagWrite, textWriter, indent)
194194
return (NULL)
195195
}
196196

@@ -212,7 +212,9 @@ tagWrite <- function(tag, textWriter, indent=0, eol = "\n") {
212212
# Convert all attribs to chars explicitly; prevents us from messing up factors
213213
attribs <- lapply(tag$attribs, as.character)
214214
# concatenate attributes
215-
attribs <- lapply(split(attribs, names(attribs)), paste, collapse = " ")
215+
# split() is very slow, so avoid it if possible
216+
if (anyDuplicated(names(attribs)))
217+
attribs <- lapply(split(attribs, names(attribs)), paste, collapse = " ")
216218

217219
# write attributes
218220
for (attrib in names(attribs)) {
@@ -235,8 +237,8 @@ tagWrite <- function(tag, textWriter, indent=0, eol = "\n") {
235237

236238
# special case for a single child text node (skip newlines and indentation)
237239
if ((length(children) == 1) && is.character(children[[1]]) ) {
238-
tagWrite(children[[1]], textWriter, 0, "")
239-
textWriter(paste("</", tag$name, ">", eol, sep=""))
240+
textWriter(paste(normalizeText(children[[1]]), "</", tag$name, ">", eol,
241+
sep=""))
240242
}
241243
else {
242244
textWriter("\n")
@@ -261,11 +263,13 @@ tagWrite <- function(tag, textWriter, indent=0, eol = "\n") {
261263

262264
doRenderTags <- function(ui, indent = 0) {
263265
# Render the body--the bodyHtml variable will be created
264-
htmlResult <- NULL
265-
conn <- textConnection("htmlResult", "w", local = TRUE)
266-
connWriter <- function(text) cat(text, file = conn)
267-
tryCatch(
268-
tagWrite(ui, connWriter, indent),
266+
conn <- file(open="w+")
267+
connWriter <- function(text) writeChar(text, conn, eos = NULL)
268+
htmlResult <- tryCatch({
269+
tagWrite(ui, connWriter, indent)
270+
flush(conn)
271+
readLines(conn)
272+
},
269273
finally = close(conn)
270274
)
271275
return(HTML(paste(htmlResult, collapse = "\n")))
@@ -292,26 +296,10 @@ rewriteTags <- function(ui, func, preorder) {
292296
if (preorder)
293297
ui <- func(ui)
294298

295-
if (!isTag(ui) && is.list(ui)) {
296-
if (length(ui) > 0) {
297-
for (i in 1:length(ui)) {
298-
newVal <- rewriteTags(ui[[i]], func, preorder)
299-
if (is.null(newVal))
300-
ui[i] <- list(NULL)
301-
else
302-
ui[[i]] <- newVal
303-
}
304-
}
305-
} else if (isTag(ui)) {
306-
if (length(ui$children) > 0) {
307-
for (i in 1:length(ui$children)) {
308-
newVal <- rewriteTags(ui$children[[i]], func, preorder)
309-
if (is.null(newVal))
310-
ui$children[i] <- list(NULL)
311-
else
312-
ui$children[[i]] <- newVal
313-
}
314-
}
299+
if (isTag(ui)) {
300+
ui$children[] <- lapply(ui$children, rewriteTags, func, preorder)
301+
} else if (is.list(ui)) {
302+
ui[] <- lapply(ui, rewriteTags, func, preorder)
315303
}
316304

317305
if (!preorder)
@@ -331,7 +319,7 @@ rewriteTags <- function(ui, func, preorder) {
331319
# different).
332320
surroundSingletons <- local({
333321
surroundSingleton <- function(uiObj) {
334-
if (is(uiObj, "shiny.singleton")) {
322+
if (inherits(uiObj, "shiny.singleton")) {
335323
sig <- digest(uiObj, "sha1")
336324
class(uiObj) <- class(uiObj)[class(uiObj) != "shiny.singleton"]
337325
return(tagList(
@@ -354,7 +342,7 @@ surroundSingletons <- local({
354342
# HTML objects and also the list of known singletons.
355343
takeSingletons <- function(ui, singletons=character(0), desingleton=TRUE) {
356344
result <- rewriteTags(ui, function(uiObj) {
357-
if (is(uiObj, "shiny.singleton")) {
345+
if (inherits(uiObj, "shiny.singleton")) {
358346
sig <- digest(uiObj, "sha1")
359347
if (sig %in% singletons)
360348
return(NULL)

shiny.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ StripTrailingWhitespace: Yes
1717

1818
BuildType: Package
1919
PackageUseDevtools: Yes
20+
PackageInstallArgs: --with-keep.source
2021
PackageCheckArgs: --as-cran --no-manual
2122
PackageRoxygenize: rd,collate,namespace

0 commit comments

Comments
 (0)