only factor and shiny.tag objects are converted to character format. all other types are returned as-is. therefore, we can simplify this behavior by removing unnecessary conditions to reduce redundant checks:
convert_body <- function(body) {
if (inherits(body, "AsIs")) {
return(body)
}
if (is.factor(body) || inherits(body, "shiny.tag"))
return(as.character(body))
body
}
better handling for shiny & htmltools tags
after the refactoring, a better way to handle shiny & htmltools tags would be to use htmltools::doRenderTags(). this is so that we can handle both shiny.tag and shiny.tag.list classes without much hassle.
shiny.tag results from htmltools::tags$* eg.
x <- tags$h1("hello")
class(x)
#> [1] "shiny.tag"
shiny.tag.list results from htmltools::tagList()
x <- tagList(tags$h1("hello"), tags$h2("there"))
class(x)
#> [1] "shiny.tag.list" "list"
the reason is because currently trying to use res$send() on a tagList() throws an error.
here's a reprex:
ℹ 12-11-2024 17:52:02 GET on /
Error in run_now(timeoutMs/1000, all = FALSE) :
Not compatible with requested type: [type=list; target=raw].
Calls: <Anonymous> -> invokeCppCallback
proposed new definition
putting all that into consideration, the new definition for convert_body() would thus be:
convert_body <- function(body) {
if (inherits(body, "AsIs")) {
return(body)
}
if (is.factor(body)) {
return(as.character(body))
}
if (inherits(body, "shiny.tag") || inherits(body, "shiny.tag.list")) {
return(htmltools::doRenderTags(body))
}
body
}
refactoring
currently,
convert_body()
is defined as follows:only
factor
andshiny.tag
objects are converted to character format. all other types are returned as-is. therefore, we can simplify this behavior by removing unnecessary conditions to reduce redundant checks:better handling for shiny & htmltools tags
after the refactoring, a better way to handle shiny & htmltools tags would be to use
htmltools::doRenderTags()
. this is so that we can handle bothshiny.tag
andshiny.tag.list
classes without much hassle.shiny.tag
results fromhtmltools::tags$*
eg.shiny.tag.list
results fromhtmltools::tagList()
the reason is because currently trying to use
res$send()
on atagList()
throws an error. here's a reprex:and this is the error:
proposed new definition
putting all that into consideration, the new definition for
convert_body()
would thus be: