dreamRs / shinyWidgets

shinyWidgets : Extend widgets available in shiny
https://dreamrs.github.io/shinyWidgets/
GNU General Public License v3.0
825 stars 152 forks source link

Support HTML for the `update` methods that have a `label` arg #624

Closed yogat3ch closed 1 week ago

yogat3ch commented 1 year ago

Hi shinyWidgets devs, It would be awesome if the update methods allowed HTML for the label arg similar to the UI component call. This currently errors if a shiny.tag is passed to label on the update method. Wrapping the tag with htmltools::doRenderTags, the update method escapes the HTML and shows it as is (shown in this example):

library(shiny)

ui <- fluidPage(
  shinyWidgets::noUiSliderInput(
    "slider",
    label = tags$span("Some HTML"),
    min = 0,
    max = 100,
    value = 55,
    step = 1,
    inline = TRUE
  ),
  actionButton(
    "update",
    "Update the label"
  )
)

server <- function(input, output, session) {

  observeEvent(input$update, {
    shinyWidgets::updateNoUiSliderInput(
      inputId = "slider",
      label = htmltools::doRenderTags(tags$p("Some mo HTML"))
    )
  })

}

shinyApp(ui, server)
yogat3ch commented 1 year ago

For anyone landing here looking to do this, here's a hack with shinyjs:


library(shiny)
library(shinyjs)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  shinyWidgets::noUiSliderInput(
    "slider",
    label = tags$span("Some HTML"),
    min = 0,
    max = 100,
    value = 55,
    step = 1,
    inline = TRUE
  ),
  actionButton(
    "update",
    "Update the label"
  ),
  actionButton(
        "hack",
        "shinyjs the label"
      )
)

server <- function(input, output, session) {

  observeEvent(input$update, {
    shinyWidgets::updateNoUiSliderInput(
      inputId = "slider",
      label = htmltools::doRenderTags(tags$p("Some mo HTML"))
    )
  })
  observeEvent(input$hack, {
    shinyjs::html("slider-label", htmltools::doRenderTags(tags$h3("Hack that label")), asis = TRUE)

  })

}

shinyApp(ui, server)