rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 68 forks source link

Can't use shiny inputs inside withTags call #319

Open ismirsehregal opened 2 years ago

ismirsehregal commented 2 years ago

Using shiny inputs inside a withTags call results in:

Error in $: object of type 'closure' is not subsettable

Writing the input to a temporary variable avoids the issue.

Please check the following example:

library(shiny)
library(htmltools)

ui <- fluidPage(
  p("withTags test"),
  numericInput(
    inputId = "testinput",
    label = "testinput",
    value = 1,
    min = 1,
    max = 10
  ),
  hr(),
  uiOutput("withoutWithTagsOut"),
  hr(),
  uiOutput("withTagsOut1"),
  hr(),
  uiOutput("withTagsOut2"),
)

server <- function(input, output, session) {
  # Using tags$ each time
  output$withoutWithTagsOut <- renderUI({
    tags$div(class = "myclass",
             tags$h3("header"),
             tags$p(paste0("text", input$testinput))
    )
  })

  # Equivalent to above, but using withTags
  output$withTagsOut1 <- renderUI({
    withTags(
      div(class = "myclass",
          h3("header"),
          p(paste0("text", input$testinput)) # Error in $: object of type 'closure' is not subsettable
      )
    )
  })

  output$withTagsOut2 <- renderUI({
    tmp <- input$testinput
    withTags(
      div(class = "myclass",
          h3("header"),
          p(paste0("text", tmp)) # works
      )
    )
  })
}

shinyApp(ui, server)

Coming from here.