Can't use shiny inputs inside withTags call

Open
#319 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
r
Domain
web-dev

Research direction

Start with the reproducible shinyApp example and inspect htmltools::withTags, especially how div, h3, and p are evaluated. Confirm the fix by rerunning the example and verifying that withTagsOut1 renders the same text as withoutWithTagsOut without assigning input$testinput to tmp.

Written by the indexing model from the issue text.

Description

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.

Dominant language
R
Stars
225
Forks
73
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rstudio/htmltools

All issues in rstudio/htmltools

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.