pickerInputから選択肢をテキストで折り返す方法、選択肢の長さが長い場合、選択肢は画面の外に出ることがよくあります

Vedha viyash

pickerInputの選択肢は、常に1行で表示されます。それらを次の行に持っていく方法はありますか?これは、選択肢の長さが非常に長く、選択肢が画面から消えてしまう場合に問題になります。ライブ検索があり、すべてを選択/すべての機能を選択解除するため、特にpickerInputが必要です。

library("shiny")
library("shinyWidgets")
ui <- fluidPage(
  pickerInput(inputId="id",label="Some name",
    choices=c("Choice 1 is small","Choice 2 is average sized",
    "But choice 3 is very  big and sometimes when the length of the qption is long it leaves the screen, so I need a UI fix to wrap the question to fit the width of the pickerInput. I want pickerInput because it has select all/deselect all button."),
    selected=NULL,multiple=T,options=list(`actions-box`=TRUE,size=10,`selected-text-format`="count > 3")
  )
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
ビクタープ

これが2つの解決策です。どちらもchoicesOpt引数を使用して、サーバー側の値が変更されないようにします。

1.文字列を切り捨てて幅を固定します

私が使用したstringr::str_trunc

library("shiny")
library("shinyWidgets")

my_choices <- c(
  "Choice 1 is small","Choice 2 is average sized",
  "But choice 3 is very  big and sometimes when the length of the qption is long it leaves the screen, so I need a UI fix to wrap the question to fit the width of the pickerInput. I want pickerInput because it has select all/deselect all button."
)

ui <- fluidPage(

  pickerInput(
    inputId = "id",
    label = "Some name",
    choices = my_choices,
    selected = NULL,
    multiple = TRUE, 
    options = list(
      `actions-box` = TRUE, size = 10, `selected-text-format` = "count > 3"
    ),
    choicesOpt = list(
      content = stringr::str_trunc(my_choices, width = 75)
    )
  ),
  verbatimTextOutput(outputId = "res")
)

server <- function(input, output, session) {
  output$res <- renderPrint(input$id)
}

shinyApp(ui = ui, server = server)

ここに画像の説明を入力してください

2.ブレークラインを挿入します

以前stringr::str_wrapは、テキストの段落を行に分割してstringr::str_replace_allから\n<br>(HTMLバージョンの\nに置き換えていました。

library("shiny")
library("shinyWidgets")

my_choices <- c(
  "Choice 1 is small","Choice 2 is average sized",
  "But choice 3 is very  big and sometimes when the length of the qption is long it leaves the screen, so I need a UI fix to wrap the question to fit the width of the pickerInput. I want pickerInput because it has select all/deselect all button."
)
my_choices2 <- stringr::str_wrap(my_choices, width = 80)
my_choices2 <- stringr::str_replace_all(my_choices2, "\\n", "<br>")

ui <- fluidPage(

  # tags$style(".text {width: 200px !important; word-break: break-all; word-wrap: break-word;}"),
  pickerInput(
    inputId = "id",
    label = "Some name",
    choices = my_choices,
    selected = NULL,
    multiple = TRUE, 
    options = list(
      `actions-box` = TRUE, size = 10, `selected-text-format` = "count > 3"
    ),
    choicesOpt = list(
      content = my_choices2
    )
  ),
  verbatimTextOutput(outputId = "res")
)

server <- function(input, output, session) {
  output$res <- renderPrint(input$id)
}

shinyApp(ui = ui, server = server)

ここに画像の説明を入力してください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ