在闪亮的 selectInput 中自定义下拉宽度

Ada*_*m_G 5 html jquery r shiny

下面的代码取自此问题,可防止下拉菜单换行文本,并设置所有下拉菜单的宽度。

有没有办法自定义每个下拉菜单的宽度selectInput

library(shiny)

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", c(1,2,3),
                  selected=1),
      selectInput("LongInput", "Long Strings", c("This is a long long string that is long.",
                                                 "This is a long long string that is longer."))
    ),

    # allows for long texts to not be wrapped, and sets width of drop-down
    tags$head(
      tags$style(HTML('
                      .selectize-input {
                      white-space: nowrap;
                      }
                      .selectize-dropdown {
                      width: 660px !important;
                      }'
              )
            )
          )
        )
      ))

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

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

Bat*_*hek 5

如果我理解你的权利,你需要类似的东西

   library(shiny)

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", c(1,2,3),
                  selected=1),
      selectInput("LongInput", "Long Strings", c("This is a long long string that is long.",
                                                 "This is a long long string that is longer."))
    ),
    
    # allows for long texts to not be wrapped, and sets width of drop-down
    tags$head(
      tags$style(HTML('
                      .selectize-input {
                      white-space: nowrap;
                      }
                      #LongInput + div>.selectize-dropdown{
                      width: 660px !important;
                      }
                      #userInput + div>.selectize-dropdown{
                                            width: 300px !important;
                      }
                      '
              )
      )
      )
      )
      ))

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

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

它设置为 660pxLongInput和 300pxuserInput

更新

你也可以做双重的,例如你有 df 输入名称和大小

df1=data.frame(name=c("LongInput","userInput"),px=c(600,300))
Run Code Online (Sandbox Code Playgroud)

所以尝试一下

library(shiny)

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", c(1,2,3),
                  selected=1),
      selectInput("LongInput", "Long Strings", c("This is a long long string that is long.",
                                                 "This is a long long string that is longer."))
    ),
    
    uiOutput("din_css")
    
      )
      ))

server <- function(input, output, session) {
  df1=data.frame(name=c("LongInput","userInput"),px=c(600,300))
  
output$din_css=renderUI({
    tags$head(
      tags$style(HTML(paste0('
                      .selectize-input {
                      white-space: nowrap;
                      }',
                      paste(apply(df1,1,function(i){
                           paste0("#",i[["name"]],"+ div>.selectize-dropdown{
                            width: ",i[["px"]],"px !important;
                           }")
                      })
                      ,collapse='/n')      )
      )
      )
    )
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)