Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny data table content with html tags

Tags:

r

dt

shiny

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) }) 
    }
)
like image 408
Sri Avatar asked Oct 23 '25 09:10

Sri


2 Answers

Please read the documentation ?DT::datatable (or the DT website). The escape argument is what you want.

datatable(x, escape = FALSE)
like image 53
Yihui Xie Avatar answered Oct 26 '25 00:10

Yihui Xie


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) 
          }
)
like image 45
cory Avatar answered Oct 25 '25 22:10

cory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!