Adam_G
Adam_G

Reputation: 7879

Prevent selectInput from wrapping text

In a shiny app, is there a way to prevent the text of the dropdown in selectInput() from wrapping, as in the screenshot below? Each option is a long text string. I'd like the dropdown to show each long string on one line, without making a huge sidebar.

enter image description here

Upvotes: 5

Views: 2152

Answers (2)

Yurika Upadhyaya
Yurika Upadhyaya

Reputation: 11

If you do selectize=False, within

selectInput(id="id",label="label",choices=your_choices, selectize=False)

It will not wrap on your text.

Upvotes: 1

SymbolixAU
SymbolixAU

Reputation: 26248

Taking inspiration from here and here you can add some custom css to the drowpdown

Here's a working example

library(shiny)

server <- function(input, output) {
    output$distPlot <- renderPlot({
        hist(rnorm(input$obs), col = 'darkgray', border = 'white')
    })
}

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
            selectizeInput(inputId = "si",
                            label =  "select", 
                            choices = c("the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog"), 
                            selected = NULL),

            ## Custom css               
            tags$head(
                tags$style(HTML('
                                .selectize-input {
                                    white-space: nowrap;
                                }
                                .selectize-dropdown {
                                    width: 660px !important;
                                }'
                                )
                        )
            )

        ),
        mainPanel(plotOutput("distPlot"))
    )
)

shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 2

Related Questions