Styling text with renderText not working

Not sure how to handle the dynamic color from the server at the time of generating the text.

The simple example below throws an error on text2. I'm looking for an alternative to solve the problem to change the style attribute based on a certain condition.

Any suggestions?

library(shiny)

ui <- fluidPage(
    br(),
    textOutput("text1"),
    br(),
    textOutput("text2")
    )
    
server <- function(input, output, session) {
    
    value <- 80
    
    # plain text
    output$text1 <- renderText(paste0(value, " percent"))
    
    # dynamic html text based on the condition - not working
    output$text2 <- renderText(
        ifelse(
            value > 50, 
            div(style="color: green;", paste0(value, " percent")), 
            div(style="color: red;", paste0(value, " percent"))
            )
        )
    
    }
    
shinyApp(ui, server)

library(shiny)

ui <- fluidPage(
  br(),
  textOutput("text1"),
  br(),
  uiOutput("text2")
)

server <- function(input, output, session) {
  
  value <- 80
  
  # plain text
  output$text1 <- renderText(paste0(value, " percent"))
  
  output$text2 <- renderUI(
    HTML(ifelse(
        value > 50, 
        as.character(div(style="color: green;", paste0(value, " percent"))), 
        as.character(div(style="color: red;", paste0(value, " percent"))))))
}

shinyApp(ui, server)
1 Like

This topic was automatically closed 7 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.