Reputation: 25
I'm using Shiny and want to color some of the options of radiobuttons. I'm aware this question has already been asked here R Shiny radioButtons how to change the colors of some of the choices?, however I'm asking something slightly different. The answer to OP's question explained how to color the choices in case we know which elements we want to color. I'm asking how to color some options which are variable; I can have 0, 1, 2, or more colored options depending on what the user previously chose in the interface.
Here's what I'm trying to do: The options of the radiobutton are files on the pc. Some of them are files found on the pc before the interface was run, and others are new files generated during the Shiny run. I want to color only the new files, and leave the old files black. I have the new files stored in an array called 'diff', which is the difference between the files now versus the files before running Shiny. The new collection of files is stored in an array called new_panels, and the old collection (how the files were before running shiny) is stored in an array called old_panels.
For example, if I have 3 old files, and 2 new ones were generated in the shiny run, the variables will be:
So the choices should be: diff (2 file names in red) + old_panels (3 file names in normal color)
Following OP's answer, I tried to replicate it by doing the following:
radioButtons(inputId = "gene_panel_options",
label = "Select the gene panel associated with this VCF:",
choiceNames = list(
HTML("<font color='red'>", diff,"</font>"), old_panels
),
choiceValues = new_panels,
selected = character(0), inline=TRUE)
This didn't work for 2 reasons:
HTML("<font color='red'>", diff,"</font>")
will return <font color='red'> c(file1, file2) </font>
. Obviously, this is not what I want, as I want each element in diff to be a separate choice.What I'd get instead is
Therefore I end up with 2 outputs instead of 5, and since choiceValues is an array of 5, I'll get an error saying that choiceNames and choiceValues should have the same length.
I hope I was able to demonstrate my problem, and apologize if this a dumb question, I'm still new to Shiny.
Thank you so much in advance!
Upvotes: 0
Views: 305
Reputation: 24832
Here you go.. just make a little function that returns the choiceNames list
library(shiny)
old_panels= paste0("old_file", seq(1,3),".csv")
diff = paste0("new_file",seq(1,2),".csv")
new_panels = c(diff,old_panels)
get_file_choice_names <- function(d,o,d_color="red") {
c(
lapply(d, function(x) HTML(paste0("<font color='", d_color, "'>",x,"</font>"))),
as.list(o)
)
}
ui <- fluidPage(
radioButtons(inputId = "gene_panel_options",
label = "Select the gene panel associated with this VCF:",
choiceNames = get_file_choice_names(diff,old_panels),
choiceValues = new_panels,
selected = character(0), inline=TRUE)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Upvotes: 1