Miguel
Miguel

Reputation: 436

R Shiny: Formatting text within tableOutput

Consider the following very simple shiny app that outputs a table of text stored in the dataframe df:

library(shiny)

df <- data.frame(id=1:3,
                   text=c('It was a good day today', 'It is good to hear from you', 'I am feeling good'),
                   stringsAsFactors = FALSE)

ui <- fluidPage(
  tableOutput("freetext")
)

server <- function(input, output){
  output$freetext <- renderTable({ df })
}

shinyApp(ui=ui, server=server)

I would like the word "good" in each line to appear in red. Is this possible using tableOutput?

I have seen posts such as this one that suggest replacing textOutput with htmlOutput in the ui function, but I am not sure how to extend this to a table of text.

Upvotes: 0

Views: 1004

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84519

If you use htmlTable, you can include some HTML in the table. For example:

library(shiny)
library(htmlTable)

df <- data.frame(
  id=1:3,
  text=c('It was a <span style="color:red;">good</span> day today', 
         'It is good to hear from you', 
         'I am feeling good'),
  stringsAsFactors = FALSE)

ui <- fluidPage(
  htmlTableWidgetOutput("freetext")
)

server <- function(input, output){
  output$freetext <- renderHtmlTableWidget({ 
    htmlTableWidget(df) 
  })
}

shinyApp(ui=ui, server=server)

enter image description here

Upvotes: 3

Related Questions