Variety of glossy dropdown designs

Can the text within a shiny drop-down be customized to display only part in italics? For example, I would like the Latin names in italics as shown below:

Red Oak - Quercus rubra (11671)

Bur Oak - Quercus macrocarpa (2705)

White Oak - Quercus alba (2437)

library(shiny)
library(tidyverse)

ui <- fluidPage(
  tags$head(
    tags$style(HTML("
                    .item {
                    font-style: italic;
                    }
                    .selectize-dropdown-content  {
                    font-style: italic;
                    }
                    "))
    ),
  mainPanel(
    uiOutput(outputId = "tree"),

    # Print selected tree
    verbatimTextOutput("selection")
  )
)


server <- function(input, output){

  my_list <- reactive({
    data <- c("Red Oak - Quercus rubra (11671)",
                "Bur Oak - Quercus macrocarpa (2705)",
                "White Oak - Quercus alba (2437)"
    )
    my_list <- as.character(data)

  })

  output$tree <- renderUI({
        selectInput(inputId = "tree", label = "Select a Tree", choices = my_list())
  })

  # Need reactive function to display variable that holds selected tree
  output$selection <- renderPrint({
        input$tree
  })
}

shinyApp(ui = ui, server = server)

Answer №1

To achieve italicized text only in the list, you can follow these steps:

library(shiny)

ui <- fluidPage(
  mainPanel(
    selectizeInput(inputId = "tree", label = "Select a Tree", choices = NULL),

    # Display selected tree
    verbatimTextOutput("selection")
  )
)    

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

  my_list <- reactive({
    list(`Red Oak - Quercus rubra (11671)` = "red",
         `Bur Oak - Quercus macrocarpa (2705)` = "bur")
  })

  observe({
    updateSelectizeInput(session, "tree", 
                         choices = my_list(),
                         options = list(render = I(
                           '{
    option: function(item, escape) {
    var splittedLabel = escape(item.label).split(" - ");
    return "<div>" + splittedLabel[0] + " - <i>" + splittedLabel[1] + "</i></div>"
    }
  }'
                         )))
  })

  output$selection <- renderPrint({
    input$tree
  })
}

shinyApp(ui = ui, server = server)

https://i.sstatic.net/9TM4Y.png

If you're looking to add a style for the selected option as well, use this technique found here:

EDIT

You can directly include HTML code in the list like this:

  my_list <- reactive({
    list(`Red Oak - <i>Quercus rubra</i> (11671)` = "red",
         `Bur Oak - <i>Quercus macrocarpa</i> (2705)` = "bur")
  })

Then use the following Javascript code:

  observe({
    updateSelectizeInput(session, "tree", 
                         choices = my_list(),
                         options = list(render = I(
                           '{
                           item: function(item, escape) {
                           return "<div>" + item.label + "</div>"
                           },
                           option: function(item, escape) {
                           return "<div>" + item.label + "</div>"
                           }
  }'
                         ))
                         )
    })

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

Mastering the Art of Resizing Video Controls: A

https://i.stack.imgur.com/jTgap.png https://i.stack.imgur.com/Exf6Y.png On the initial screenshot, the video player displays normal controls. However, on the same website with identical CSS, the second video player shows different control settings. // C ...

Alignment issue detected

I was attempting to center these 4 divs inside a container both horizontally and vertically, but they are stuck at the top edge of the <div>. They aren't moving down and remain fixed at the top. /* Footer */ #footer { width: 100%; hei ...

Display a remote page on hover using Javascript, CSS, and HTML without providing any clickable link

How can I display a preview of another page within a div container without actually making the text clickable? I want the preview to show when the mouse hovers over specific text, but I need the original text to stay on the main page. I've experiment ...

Adding a CSS style to a specific child element

Here is an example snippet <html> <head> <style> .test > input { //this selector is incorrect. color:red; } </style> </head> <body> <div class="test"> <di ...

Creating a responsive card layout in Bootstrap 4 that utilizes the vertical space of the viewport with a scrollbar

We are working on a page that has specific requirements: The page should not have a scroll bar. The card must expand vertically to fill the remaining space, even if there is no content in the card-body. We need a scroll bar for the card-body only when the ...

The flexbox container is not displaying properly

I have rows containing two flex divs each. One div displays an image, while the other has text. I'm puzzled as to why some divs display correctly while others have overflowing text. Refreshing the page resolves this issue, but it resurfaces if I clea ...

Mysterious obsidian container lurking in the background of the video on Chrome version 67.0.3396

Displayed on the page is an HTML Video tag, which streams a video from the speaker (WebRTC). <div id="remoteVideoContainer"> <video id="remotevideo" autoplay="autoplay" controls="" loop="loop" preload="true" height="500" width="100%"> ...

Image can be centered, but unable to center div in IE7

When trying to center an element both vertically and horizontally, everything seems to be working correctly except for one issue I'm facing in IE7. I am able to center an img vertically but not a div. What style is being applied by IE to the image tha ...

I am having trouble with my custom-button class not successfully overriding the btn background color property. Can anyone provide insight

Utilizing the bootstrap5 variant-button mixin, I aim to create a custom-colored button. While I have successfully altered the default hover effect color, I am encountering difficulty in setting the background color of the button itself. Upon inspecting the ...

Display an image from the API that rotates when hovered over with the mouse

Currently, I am fetching data from pokeapi.co and dynamically inserting it into a table. This data includes various stats and an image. My goal is to make the image rotate when hovered over. While creating the table, I added an id="pokeImage" dynamically. ...

What could be causing the lack of color change in my boxes even after confirming the prompt?

function changeColor() { if (confirm("Press a button!") == true) { if(this.style.backgroundColor == "white") this.style.backgroundColor = "yellow"; else if(this.style.backgroundColor == "yellow") this.style.backgroundColor = "red"; else i ...

the child div within a Bootstrap 3 column is not being properly styled

Experiencing a strange issue with a relative div within a column in Bootstrap 3. Here's my code: <div class="col-lg-6"> <div class="large" style="background: url(img/bg.jpg);"> <h1 class="grid-header">Content 1</h1> ...

Enhance R loops for optimized time performance with large datasets exceeding half a million rows

I find my R code to be quite time-consuming, as it takes around 10-20 minutes to run. The dataset comprises a data frame with about 30 columns and 500,000 rows. The objective of the loop is to determine the appropriate bin for a specific value. Despite my ...

Ensure that the child element maintains equal height and width measurements within a row that is distinct

Is there a way to arrange 4 items inside a flex container in a 2 X 2 grid layout while ensuring that they all have the same width and height? I've tried using flex-grow: 1;, but since the children are in different flex containers, they do not obey th ...

Increasing the size of iframe in a Flexbox layout

I'm facing a challenge in making the iframe element stretch to occupy the remaining space within my web application. While I have ensured that the maximum space is allocated for the div by setting a border, the iframe's height does not automatica ...

Determine the width of a div by utilizing SASS and following a series of incremental steps

Let's discuss the current scenario: I have a parent div that consists of multiple child elements which vary in number. (2, 3, 4) The goal is to determine the appropriate width for each step element dynamically: For 2 steps: 50% For 3 steps: 33.3% ...

What sets apart the "+" and "~" selectors in CSS?

I've tried using both of these selectors, but I'm having trouble distinguishing between them. It appears that they are functioning in the same way. There must be a difference that I'm overlooking. ...

Transforming an R dataframe into the appropriate format for utilizing the `rep` function

I am currently working with two data frames. The first data frame, A, is structured as follows: A = data.frame(c(1485,1486,1701,1808)) names(A) <- c("ID") The second data frame, B, is structured like this: B = data.frame(1:12) names(B) <- "value" ...

What is preventing SVG textPath from displaying properly? [Empty output in devtools]

I found this interesting code snippet on Mozilla and decided to incorporate it into a Vue component. <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <path id="MyPath" fill="none" stroke="red" d="M10,90 Q90,90 90,45 Q90,10 ...

Certain divs have an opacity of zero while others do not

Currently in the process of creating a bootstrap template, which is being hosted at this link: Encountering a peculiar bug where the title spice road seems to be blocking the lorem ipsum text, yet is allowing the text in the gray jumbotron div to show thr ...