0

In R Shiny, I know you can use update*Input functions to adjust the labels/placeholders/values of inputs in response to user actions via linked observeEvent calls. For example, in the code below, the label for my textInput changes as soon as you start typing anything into it.

    library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  textInput(inputId = "name", 
            label = "Enter your name!",
            placeholder = "Placeholder")
  
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

  observeEvent(input$name, {
    
    req(input$name)
    
    updateTextInput(session, 
                    "name",
                    label = "Wow, you're really doing it!")
    
  }  
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

However, I can't figure out how to change the formatting of the updated labels. In particular, I want to make them either bold or a different color so they stand out more. I've tried stuff like label = HTML(<b>"This is bold"</b>) and label = p(strong("This is bold")) and various variations on them inside the updateTextInput() call. I've looked at other related articles like How to make a label of a shiny widget the same as plain text? and How to format parts of a label bold? and How to change the pickerinput label to fine instead of bold and I'm just spinning my wheels! It looks like the update*Input functions work a bit differently than their corresponding *Input functions, so what works for one may not work for the other...

Bajcz
  • 433
  • 5
  • 20

1 Answers1

1

Yep, that doesn't work. Here is a trick. The label is bold by default so I change it to a red label instead.

library(shiny)

# UI ####
ui <- fluidPage(
  
  textInput(inputId = "name", 
            label = uiOutput("thelabel", inline = TRUE),
            placeholder = "Placeholder")
  
)

# Server ####
server <- function(input, output, session) {
  
  TheLabel <- reactiveVal("Enter your name")
  
  output[["thelabel"]] <- renderUI({
    TheLabel()
  })
  
  observeEvent(input[["name"]], {
    req(input[["name"]])
    TheLabel(span("This is red", style = "color: red;"))
  }, ignoreInit = TRUE)
  
}

# Run ####
shinyApp(ui = ui, server = server)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Nice! +1. This does as advertised and works! Although I did have to remove the ```req(input$name)``` portion because that seemed to break another event later, but that didn't cause problems to remove. I'm not totally sure what's happening here or why the trick is needed, but maybe as I get better at Shiny it'll make more sense to me :). – Bajcz Jul 14 '22 at 15:57