【问题标题】:add break line in checkboxGroupInput choices - Shiny在 checkboxGroupInput 选项中添加断线 - 闪亮
【发布时间】:2021-09-17 09:27:25
【问题描述】:

我想在一个选项中添加一个换行符,因为它太长了。按钮标签有类似的question,但该解决方案对我不起作用,请参见以下代码

ui <- fluidPage(
  splitLayout(cellWidths = c(170,80,160),
              tagList(uiOutput("example")))
)

server <- function(input, output) {
  choices_vector <- c("choice 1","choice 2",HTML("I want a line break here <br/> since the label is too long"),"choice 4")
  output$example <- renderUI({
    checkboxGroupInput(inputId = "choices_selec",
                       label = "", choices = choices_vector, selected = F)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

我也尝试使用 paste0collapse(参见以下代码)并将 uiOutput 更改为 htmlOutput,但结果相同

server <- function(input, output) {
  choices_vector <- c("choice 1","choice 2",paste0(c("I want a line break here ", "since the label is too long"), collapse = "<p/>"),"choice 4")
  output$example <- renderUI({
    checkboxGroupInput(inputId = "choices_selec",
                       label = "", choices = choices_vector, selected = F)
  })
}

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: html r shiny


    【解决方案1】:

    问题是checkboxGroupInput 只接受字符串为choices。除非您更改 Shiny 源代码,否则我们无能为力。尝试跳出框框思考,为什么我们需要在 R 预处理级别进行。我们可以在客户端(浏览器)使用js进行后期处理。

    方法如下,照常做,只是在复选框后添加这个非常简单的 js 代码:

    library(shiny)
    ui <- fluidPage(
      splitLayout(cellWidths = c(170,80,160),
                  tagList(uiOutput("example")))
    )
    
    server <- function(input, output) {
      choices_vector <- c("choice 1"="c1","choice 2"="c2", "I want a line break here<br/> since the label is too long"="c3","choice 4"="c4")
      output$example <- renderUI({
        tagList(
          checkboxGroupInput(inputId = "choices_selec",
                             label = "", choices = choices_vector, selected = F),
          tags$script(
            "
            $('#choices_selec .checkbox label span').map(function(choice){
                this.innerHTML = $(this).text();
            });
            "
          )
        )
      })
      
      observe({
        print(input$choices_selec)
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
    

    此代码将允许您使用任何 HTML 标记作为字符串输入。呈现复选框后将解析 HTML。

    我还将名称添加到选择向量中,这样您就可以在服务器端更好地了解选择了哪些选项。

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多