Styling elements based on their index in R Shiny

In my R Shiny app, I am looking to color elements based on their index number (first element blue, second yellow, third red).

Here is a reproducible example:

library(shiny)

ui <- fluidPage(
  tags$style(".control-label:nth-of-type(1) {background-color:blue;}"),
  tags$style(".control-label:nth-of-type(2) {background-color:yellow;}"),
  tags$style(".control-label:nth-of-type(3) {background-color:red;}"),

  lapply(
    letters[1:3], 
    FUN = function(name){
      selectizeInput(
        inputId = paste0("type_", name),
        label = paste0(name),
        choices = "a",
        selected = "a",
        multiple = TRUE,
        options = list(create = TRUE)
      )
    }
  )

)

server <- function(input, output, session) {

}

shinyApp(ui, server)

This is what I have tried so far:

Based on: Stack Overflow CSS Selector for First Element I explored some options:

  • Trying to use the (~) Operator for overwriting with the first match, but unsure how to do it efficiently for the subsequent elements.
  • Experimenting with :nth-of-type(1), facing difficulties in implementation as seen in the example above.

Answer №1

I am unfamiliar with a CSS resolution for this issue. However, I can provide a JavaScript workaround:

library(shiny)

js <- '
$(document).ready(function(){
  var labels = $(".control-label");
  labels[0].style.backgroundColor = "red";
  labels[1].style.backgroundColor = "green";
  labels[2].style.backgroundColor = "blue";
});
'

ui <- fluidPage(
  tags$head(tags$script(HTML(js))),
  lapply(
    letters[1:3], 
    FUN = function(name){
      selectizeInput(
        inputId = paste0("type_", name),
        label = paste0(name),
        choices = "a",
        selected = "a",
        multiple = TRUE,
        options = list(create = TRUE)
      )
    }
  )

)

server <- function(input, output, session) {}

shinyApp(ui, server)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

how to create an R formula with certain independent variables containing spaces

Currently, I am utilizing the ucm function in R with a formula argument. In this scenario, I have a response variable named y and two predictors labeled as "abc def" and jkl. My aim is to structure the formula as y ~ abc def + jkl, but the space between "a ...

issue installing ecogbm package

After downloading a package from this link: ecogbm I attempted to install it using the following command: setwd("d:/RStuff") install.packages("ecogbm_1.01.tar.gz", repos = NULL, type="source") Unfortunately, I encountered the following error message: ...

Display a background color behind an <img> element when the image's dimensions are smaller than the page

Currently, I am utilizing the following CSS for styling an image - check it out here img { border: 3px solid #ddd; margin-left: auto; display: block; margin-right: auto; background: red; } If you would like to view the output, click on this link - see it ...

Vertical alignment to the top is not possible for Inline-Block divs

When attempting to create a responsive 'grid' with two (div) panels that appear side by side on wide screens and stacked on top of each other on smaller screens, I came across an issue where the divs align at the bottom when displayed as 'bl ...

How to vertically align multiple lines of text using CSS

Struggling with vertical alignment of text in a tabbed layout on my website. Some tabs have single-line text while others span two lines, making it challenging to position the text consistently in the middle of each tab without aligning by the first line. ...

Select delayed values in R

In a given dataset displayed in the example below, I am interested in retaining only the Difference column for values greater than 2 based on the Unique_ID, without removing the NA rows. My_modified_table <- structure(list(Unique_ID = structure(c(1L, 1 ...

Why is the CSS not correctly linked to the index.html file?

Currently, I am working on a project involving a "is it newyear page" that should provide a simple yes or no answer. While the functionality of the page is not the issue, I am facing a problem with the link placement. I have included a link inside the < ...

Div displaying white blank space due to Internet Explorer 11 issue

While working on creating a print-friendly version of a webpage, I encountered a puzzling issue with Internet Explorer 11. Both the webpage itself and the print preview are showing some unexplained blank space below the div elements. I have meticulously c ...

Is it possible to apply a style change to several components at once using a single toggle switch

I am looking to implement a day/night feature on my web app, triggered by a simple toggle click. While I can easily add this feature to a single component using the navigation menu, I am faced with the challenge of incorporating it into multiple component ...

Can a different div or HTML element be used in place of the text "Hello World"?

<body onload="myfunction('target')"><div id="target"> "Hey there!" </div></body> Can we replace the text "Hey there!" with another HTML element or div? This is currently a left-to-right marquee text. <script language= ...

Why am I encountering issues with variables not being detected in R?

I've recently started using R and created a script that was functioning perfectly. However, out of the blue, it stopped recognizing my variables. Although I can still access the dataset, I am unable to perform any analysis. tba_hba <- read_excel(k ...

Which slider was featured in the unity3d.com/5 website?

While browsing the internet, I came across the website and was intrigued by the slider effect featured in the "graphics" section. I am eager to figure out how to replicate that stunning visual effect. ...

What is the best way to reduce the spacing between text?

I am trying to figure out how to reduce the spacing between my text so that it aligns like the example below. Take a look at the text "Welcome to my portfolio". Currently, when I run my code, there is a large gap between "Welcome to my" and "Portfolio". ...

Opting for a CSS toggle on a div as opposed to an input field

I was on the hunt for a way to create a toggle button and stumbled upon one that fit my needs perfectly. However, the example I found uses the toggle on an input field whereas I want to implement it on a div element. Despite attempting various modification ...

Attempting to implement a jQuery function to toggle the visibility of the submenu

Hey there! After spending some time digging into this, I've hit a roadblock. I'm struggling to figure out how to keep the sub-menu active even when the mouse is no longer hovering over it, similar to how the bootstrap menu functions. Here' ...

Ways to secure a floating item onto a backdrop picture

Currently I am working on editing this page. When you hover over the cat, its eyes blink or two black dots randomly appear. My goal is to prevent the random blinking dots from appearing. I want to keep the hover object attached to the cat/background image ...

Inline-block display causing divs to act unpredictably

I am working with a main div that contains three smaller divs inside. Each of these divs has been assigned a width of 30%, and they are all perfectly centered within the main div. To ensure that the three divs appear side by side, I used the display: inli ...

Spinning triangle around central point using CSS

I'm working on a project that includes the following code snippet: body { background-color: #312a50; font-family: Helvetica Neue; color: white; } html, body { height: 100%; } .main { height: 100%; width: 100%; display: table; } .wr ...

Prevent the line break from going beneath the ::before pseudo element

p::before { content: "1. "; } p { width: 200px; } <p>This is an example of a long text that requires a line break: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam ...

Tips for connecting elements at the same index

.buttons, .weChangeColor { width: 50%; height: 100%; float: left; } .weChangeColor p { background: red; border: 1px solid; } .toggleColor { background: green; } <div class="buttons"> <p><a href="#">FirstLink</a></ ...