updateTextInput contain link

Hi all,

I've got a text input looking for a completion code that has a label that links to another page, and it works beautifully. I'm trying to set up my Shiny page so that when the wrong code is entered, it tells the user by updating the text (so something like going from "Task A" to "Task A - Code incorrect, please try again"). I've used updateTextInput to change the text itself and it works perfectly, but when the text changes, the link disappears and I can't quite figure out how to get the link included when the label changes. Does anyone have any tips on how to do it? EDIT: I've attached a reproducible example in the comments below.

Thanks in advance!

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Attached is a reprex -- the link leads to Google. If you enter "test" in the input, it greys out the text input so you are unable to change it. However, if you enter any 4 character string but "test" (ie if you put in the wrong code, or make a typo), the label changes (indicating that the code is incorrect) and you are still able to type in the box, but the link is no longer active. I'm trying to make it so that if the code is entered incorrectly, you are still able to click the link (I'm imagining a use case where someone clicks the link, performs the task and then accidentally pastes the code into the wrong box, which would mean that they no longer have the option to click the link to the next task).


library(shiny)

ui <- fluidPage(
    shinyjs::useShinyjs(),
    # Application title
    titlePanel("Title"),
    
    # instructions
    mainPanel(
        fluidRow(
            column(7, 
                   textInput("taskA",a("Task A", href = 'https://www.google.com', target = "_blank"))   
            ))
    )
    
)

server <- function(input, output, session) {
    observeEvent(input$taskA,{
        if (input$taskA == "test"){
            updateTextInput(session,"taskA", label = "Task A")
            shinyjs::toggleState("taskA")
        }    else if(nchar(input$taskA) >= 4){
            updateTextInput(session,"taskA", label = "Task A - please try code again")
        }   
        
    })

}

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


split out the label as its own element that you construct


library(shiny)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  # Application title

  titlePanel("Title"),
  
  # instructions
  mainPanel(
    fluidRow(
      column(7, 
             uiOutput("label_taskA"),
             textInput("taskA",label = NULL)   
      ))
  )
  
)

server <- function(input, output, session) {
  observeEvent(input$taskA,{
    if (input$taskA == "test"){
      shinyjs::toggleState("taskA")
    } 
  })
  
  output$label_taskA <- renderUI({
    lbl <- a("Task A", href = 'https://www.google.com', target = "_blank")
    if(input$taskA == "test"){
      lbl <-"Task A - Try Again"
      } else if(nchar(input$taskA) >= 4){
      lbl <- "Task A - please try code again"
      }  
    tags$label(lbl)
  })
}

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

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.