I have a datatable in a Shiny app. This datatable has a column with multiple strings. I need to format one string pattern in this column to bold using regex. The following reprex needs to bold string "H2", but I need a regex solution.
library(shiny)
library(DT)
data <- data.frame(V1 = 1:3, V2 = c("H1, H2, H3", "H5, H2, H6", "H4, H3, H5"))
ui <- fluidPage(
"How can I make only 'H2' bold??",
DTOutput("table", width = 500)
)
server <- function(input, output, session) {
output$table <- renderDT(datatable(data))
}
shinyApp(ui, server)
One option would be to use e.g. gsub
to wrap the text you want bold inside an HTML b
tag and set escape=FALSE
in datatable()
.
EDIT: Following the suggestion from @DavidJorquera I generalized the code a bit by adding the serach pattern and the tag as variables.
library(shiny)
library(DT)
data <- data.frame(V1 = 1:3, V2 = c("H1, H2, H3", "H5, H2, H6", "H4, H3, H5"))
ui <- fluidPage(
"How can I make only 'H2' bold??",
DTOutput("table", width = 500)
)
server <- function(input, output, session) {
output$table <- renderDT({
pattern <- "H2"
data$V2 <- gsub(pattern, paste0("<b>", pattern, "</b>"), data$V2)
datatable(data, escape = FALSE)
})
}
shinyApp(ui, server)
#>
#> Listening on http://127.0.0.1:5145