How to style an single individual selectInput menu in R Shiny?
I found the answer myself. Combination of determination, lots of hours on google and Stackoverflow etc with some info I found created by Dean Atali I believe, but this seems to do it:
tags$head(tags$style(HTML('.selectize-input {white-space: nowrap}
#choice+ div>.selectize-dropdown{width: 660px !important}
#choices+ div>.selectize-dropdown{width: 300px !important}')))
Shiny selectInput CSS affecting all selectInputs
I think getting the CSS to select the proper <div>
is the trick. Here's a reproducible example of the functionality using the default Shiny scaffolding:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Control CSS of Singl Selectize Input"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
tags$style(HTML("#bins+ .selectize-control.multi .selectize-input > .item:nth-child(odd) {background: #F4F4F4 ;
width: 100% !important;}
#bins+ .selectize-control.multi .selectize-input > .item:nth-child(even) {background: white ;
width: 100% !important;}")),
selectizeInput("bins",
"Number of bins:",
choices = c(1:50),
selected = 30,
multiple = TRUE),
selectizeInput("newbins",
"Number of bins:",
choices = c(1:50),
selected = 30,
multiple = TRUE)
),
# Show a plot of the generated distribution
mainPanel(
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
The CSS first finds the right id
then finds the correct div below that using class. More info on the + sign in CSS: https://www.w3schools.com/cssref/sel_element_pluss.asp
add CSS style to selectInput choices programatically
You can make a dynamic CSS style with renderUI
:
library(shiny)
names <- c("A", "B", "C")
options <- data.frame(names)
cssTemplate <- function(color){
sprintf(
".selectize-dropdown-content > .option,
.selectize-input > .item
{
color: %s;
}",
color
)
}
ui <- fluidPage(
tags$head(
uiOutput("css")
),
selectInput("apkgs", "Select a package", choices = options$names),
)
server <- function(input, output, session) {
output[["css"]] <- renderUI({
color <- ifelse(input$apkgs == "A", "green", "red")
tags$style(HTML(cssTemplate(color)))
})
}
shinyApp(ui, server)
EDIT by the OP
library(shiny)
names <- c("A", "B", "C")
installed <- c(TRUE, FALSE, FALSE)
options <- data.frame(names, installed)
ui <- fluidPage(
tags$head(
uiOutput("css")
),
div(id="algo",
selectInput("apkgs", "Select a package", choices = options$names)
)
)
server <- function(input, output, session) {
output$css <- renderUI({
tags$style(
HTML(unlist(
lapply(names, function(x){
if(options[options$names==x,]$installed) {
sprintf("#algo .selectize-dropdown-content > .option[data-value='%s'] { color: green; }", x)
} else {
sprintf("#algo .selectize-dropdown-content > .option[data-value='%s'] { color: red; }", x)
}
})
)
)
)
})
}
shinyApp(ui, server)
Shiny: Different styles for textInputs and selectInputs
One solution that worked for me is for the textInput is actually quite easy, all I needed to do is in my css:
textInput(id, "")
tags$style(HTML("
#id.form-control{color:gray;}
"))
For the selectize input is a bit more confusing:
selectizeInput(id, ....)
tags$style(HTML("
#id + div>.selectize-input.items.not-full.has-options{border-bottom: 1px solid #F2F2F2;}
#id + div>.selectize-dropdown, #id+ div>.selectize-input, #id+ div>.selectize-input input{ color: gray;}
#id + div> div> .item {color: gray;}
"))
Change font color of select input choices R Shiny
Here is a way without reactive CSS. The select input is created in the server, this easily allows to use reactive dataframes.
library(shiny)
library(jsonlite)
ui = fluidPage(
tags$head(
tags$style(
HTML(
"
.red {color: red;}
.blue {color: blue;}
"
)
)
),
br(),
uiOutput("slctzUI")
)
server <- function(input, output, session){
df <- data.frame("ID" = c("F001","N002","F003","T004","F005"))
values <- data.frame("AnimalID" = c("F001","F003","T006", "T008"))
choices <- unique(df[["ID"]])
colors <- ifelse(choices %in% values[["AnimalID"]], "blue", "red")
names(colors) <- choices
colors <- toJSON(as.list(colors))
output[["slctzUI"]] <- renderUI({
selectizeInput(
"slctz", "Select something:",
choices = choices,
options = list(
render = I(sprintf("{
item: function(item, escape) {
var colors = %s;
var color = colors[item.label];
return '<span class=\"' + color + '\">' + item.label + '</span>';
},
option: function(item, escape) {
var colors = %s;
var color = colors[item.label];
return '<span class=\"' + color + '\">' + item.label + '</span>';
}
}", colors, colors))
)
)
})
}
shinyApp(ui, server)
Coloring the options in a selectInput in R Shiny
I would recommend using CSS. For more on how to style a Shiny application with CSS see this article.
I have included a demo application which includes CSS as a string. In practice I would store CSS styles in a separate file, see the article linked above. If you are not familiar with CSS I will briefly summarize the selectors used, more on CSS selectors here.
Summary of CSS selectors,
#my_select_input
looks for a tag with idmy_select_input
~ .selectize-control
says we actually want a sibling tag of#my_select_input
and that sibling must have the classselectize-control
.option
says we want child tag(s) of the above tag(s) and those child tag(s) must have the classoption
- finally, the
:nth-child(odd)
and:nth-child(even)
let us control which of the child tags are style will apply to, whereodd
selects the first, third, and fifth (etc.) child andeven
selects the second, fourth, and sixth (etc.) child.
With all that said, here is the demo application.
library(shiny)
shinyApp(
ui = fluidPage(
tags$head(
tags$style("
#my_select_input ~ .selectize-control .option:nth-child(odd) {
background-color: rgba(30,144,255,0.5);
}
#my_select_input ~ .selectize-control .option:nth-child(even) {
background-color: rgba(205,92,92,0.5);
}
"
)
),
selectInput(
inputId = "my_select_input",
label = "Select Letter",
choices = c("A", "B", "C", "D")
)
),
server = function(input, output) {
}
)
If you wanted to apply the red/blue coloration to all selectize inputs in your application you could drop the #my_select_input ~
prefix. If you wanted to apply a variant of this style to a different selectize input you could change my_select_input
and tweak the background color.
How to change R Shiny 'selectInput' value selection display space background color when no value selected?
Some of the controls are in the css below
library(shiny)
css <- "
.selectize-dropdown-content .option {
color: blue;
}
.selectize-input .item {
color: red !important;
background-color: yellow !important;
}
::-webkit-input-placeholder { /* Chrome/Opera/Safari */
background-color:red !important;
color: white;
}
::-moz-placeholder { /* Firefox 19+ */
color: pink;
}
:-ms-input-placeholder { /* IE 10+ */
color: pink;
}
:-moz-placeholder { /* Firefox 18- */
color: pink;
}"
ui <- fluidPage(
tags$head(
tags$style(HTML(css))
),
br(),
selectInput("my_select", "Choose: ", c("Please choose a site or say anything"='',"Site 1", "Site 2","Site 3", "Site 4"),
selectize = T),
br()
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
Related Topics
Internet Explorer Button:Active Inner-Padding
Remove Ie11's "Clear Field" X Button on Windows 8
CSS Label Text Right Below Input Element
What Safari-Specific Pure CSS Hacks Are Out There
Css3 Transition Only When Class Is Added, Not When Removed
How to Print Only Parts of a Page
How to Specify the Maximum Number of Columns Repeat() Will Create Using Auto-Fit/Fill
Incremental Slides Do Not Work with a Two-Column Layout
Css3 Gradients to Reproduce an 'Inner Glow' Effect from Illustrator with Border-Radius Applied
How to Control the Width of a Table Header Cell That Contains Rotated Text
Horizontal Center Dynamic Image in Div with Absolute Position
How to Target Galaxy Nexus and Nexus 7 with Media Queries
How to Force Flex Children Not to Overflow the Container
How to Override "-Moz-User-Select: None;" on a Child Element
Datatables Pagination Buttons - Remove Unwanted Space
How to Select a Web Element by Text with Selenium Webdriver, Java
How to Get Material-Ui Drawer to 'Squeeze' Other Content When Open