Hi, I recently encountered a weird side effect, when using the htmltools::tagQuery()
function to create and modify custom html tags for my shiny application.
Scenario: My goal was to create a function for bootstrap 4 cards as containers for my UI output.
I have already successfully done so in the past using "classic" manipulation of shiny tags along with functions like tagAppendChildren()
or tagAppendAttributes()
.
Now, I just wanted to refactor my code a little bit and use the above mentioned tagQuery approach.
Consider the following app now:
library(shiny)
library(bslib)
library(htmltools)
library(reactable)
myWellPanel <- function(...) {
panel <- wellPanel(...)
panel <- tagQuery(panel)$allTags()
panel
}
ui <- fluidPage(
theme = bs_theme(version = 4),
fluidRow(
column(
6,
wellPanel(reactableOutput("test1"))
),
column(
6,
myWellPanel(reactableOutput("test2"))
)
)
)
server <- function(input, output, session) {
output$test1 <- renderReactable(
reactable(iris)
)
output$test2 <- renderReactable(
reactable(iris)
)
}
shinyApp(ui, server)
For the sake of demonstration, I didn't create any custom card function, but just wrote a wrapper function around shiny::wellPanel()
, simply calling tagQuery()
on it and returning all tags.
The example above still works, but myWellPanel
behaves weird in the following scenario:
- It contains an htmlwidget output (here a reactable) and
- Is used at least twice (when used once like in the example, it works).
I.e. when I change the above UI code and only use myWellPanel
instead of wellPanel
no output will be shown at all:
# Does not work
ui <- fluidPage(
theme = bs_theme(version = 4),
fluidRow(
column(
6,
myWellPanel(reactableOutput("test1"))
),
column(
6,
myWellPanel(reactableOutput("test2"))
)
)
)
I want to emphasize that this seems to be the case only when the output is an htmlwidget.
E.g. when I render a static plot, these issues don't encounter at all.
The only thing I know so far is that calling tagQuery(mytag)$allTags()
seems to change the structure of the shiny tag:
str_normal <- wellPanel(reactableOutput("test1"))
str_custom <- myWellPanel(reactableOutput("test2"))
View(str_normal)
View(str_custom)
In particular, it seems to remove one list wrapper from the tag's children, which might in some way be the cause (however I am just not expert enough in shiny or web development to telll what exactly it is).
I would be very grateful if someone could explain to me, what exactly causes this weird behaviour and if it's rather me, who needs to learn more about tagQuery and modify the code or if this might be some issue around htmltools::tagQuery()
.
Thanks in advance and kind regards
David