I have a data table in which column a is a character field. I need to make some strings withing the column to appear with different color(just the beginning, I need to search and replace multiple strings with different colors ultimately). I'm attempting to do it the following way but unsuccessful.
Below I'm attempting to put html tags within the column values, but I'm not sure how to make the browser treat those as html tags while displaying the data table. Any ideas?
library(shiny)
library(DT)
x<-data.table(a=c("srinivas asfsis asdfsadf","vassri asdf asdfasdf","csdasdsriasfasf"))
x$a<-as.data.table(sapply(x$a,function(x)gsub("sri",'<strong style="color:red">sri</strong>',x)))
shinyApp( ui = dataTableOutput("table1"),
server = function(input, output) {
output$table1<-renderDataTable({ datatable(x) })
}
)
Please read the documentation ?DT::datatable (or the DT website). The escape argument is what you want.
datatable(x, escape = FALSE)
You've got conflicting packages that each have functions with the same name. It doesn't appear that you need anything more than the shiny package for this...
library(shiny)
x<-data.frame(a=c("srinivas asfsis asdfsadf","vassri asdf asdfasdf","csdasdsriasfasf"))
x$a<-gsub("sri",'<strong style="color:red">sri</strong>',x$a)
shinyApp( ui = fluidPage(shiny::dataTableOutput("table1")),
server = function(input, output) {
output$table1<-shiny::renderDataTable(x, escape=FALSE)
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With