I am really stumped. Hopefully someone has an idea how to solve this.
Here's a simplified version of my problem. Let's say I have a sentence in R like such:
"The quick brown fox jumped over the lazy dog."
Let's also say that I want the word "brown" and the word "over" to be highlighted in blue and green, respectively. Everything else should have no highlights.
In R, I have assigned a hex code per word. In my example above, the data frame would look like this:
df <- as.data.frame(c("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog."))
df$color <- c("#ffffff","#ffffff", "#a7eef9", "#ffffff", "#ffffff", "#bcdd87", "#ffffff", "#ffffff", "#ffffff")
colnames(df) <- c("word", "color")
Now, onto the Shiny App. I am able to set the background color of the entire string to one hex code, using this method:
tags$head(tags$style(HTML("#thetextoutput{background-color: #a7eef9}")))))
But how do I integrate hex codes word-by-word? I am finding this to be extremely difficult to pull off. Any insight would be appreciated.
Fully reproducible code with single highlight:
library(shiny)
library(shinyjs)
library(tidyverse)
library(htmltools)
library(tools)
df <- as.data.frame(c("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog."))
df$color <- c("#ffffff","#ffffff", "#a7eef9", "#ffffff", "#ffffff", "#bcdd87", "#ffffff", "#ffffff", "#ffffff")
colnames(df) <- c("word", "color")
example.text <- "The quick brown fox jumped over the lazy dog."
ui <- mainPanel(
fluidRow(
useShinyjs(),
h3("Hello world"),
uiOutput("thetextoutput"),
tags$head(tags$style(HTML("#thetextoutput{background-color: #a7eef9}")))))
server <- function(input, output){
output$thetextoutput <-
renderUI({
return(example.text)
})
}
shinyApp(ui, server)
Thank you very much.