Shiny: Highlight text input in a certain color

I have this Shiny App where a user would write text into a textInput field and whenever the text is in my dataframe the text will be displayed in a tableOutput. The textInput from the user should be then highlighted in a certain color lets say red in the output table.
How would i do this?

library(shiny)

df = tibble(Text=c("The quick brown fox", "jumps over", "the lazy dog"))

ui =fluidPage(
    
    fluidRow(
        
        textInput("input", "Textinput"),
        tableOutput("output")
    )
)

server = function(input, output){
    
    df_reactive = reactive({
        df %>%
            filter(str_detect(text, input$input))
    })
    
    output$output = renderTable({
        df_reactive()["text"]
    })
}

shinyApp(ui = ui, server = server)

for clarification: if a user types 'dog', only the rows in which 'dog' occurs should be displayed in the table output and only dog should be coloured red

You could use the renderDataTable() from the DT package to display your df. It allows for search highlighting:

library(DT)

ui =fluidPage(
  
  fluidRow(
    
    dataTableOutput("table")
  )
)

server = function(input, output){
  
  df_reactive = reactive({
    
    df = data.frame(Text=c("The quick brown fox", "jumps over", "the lazy dog"),
                       stringsAsFactors = FALSE)
  return(df)  

  })
  
output$table = renderDataTable(
    df_reactive(),
    rownames = FALSE,
    options = list(searchHighlight = TRUE)
  )
}

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.