首页 > 解决方案 > 如何在 R Shiny SelectInput 下拉列表中呈现不等式

问题描述

我使用该shinydashboard包创建了一个 R Shiny 应用程序。selectInput使用该函数,我似乎无法在下拉框中出现不等式或 unicode 字符。谁能帮我弄清楚如何让大于或大于或等于符号(≥)出现在下拉列表中?这是一个演示该问题的示例:

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- shinyUI(dashboardPage(
  dashboardHeader(),
  dashboardSidebar(collapsed = FALSE,

                   selectInput("Age", "4. Select Age Group:",
                               c("\u2265 6 months" = "_6plus",
                                 "&ge 6 months" = "_6plus2",
                                 "&#8805; 6months" = "_6plus3",
                                 "&#8805; 6months" = "_6plus3",
                                 "6 months - 4 years" = "_6_4"))),

  dashboardBody(
    useShinyjs()
  )
))

server <- shinyServer(function(input, output, session) {
  #addClass(selector = "body", class = "sidebar-collapse")
})

shinyApp(ui = ui, server = server)

然而,这似乎没有任何尝试奏效。我会很感激任何帮助。谢谢。

标签: rencodingshinycharacter-encoding

解决方案


您可以使用render选项selectizeInput在选项名称中使用 HTML 代码:

library(shiny)

render <- "
{
  option: function(data, escape){return '<div class=\"option\">'+data.label+'</div>';},
  item: function(data, escape){return '<div class=\"item\">'+data.label+'</div>';}
}"
  
ui <- fluidPage(
  selectizeInput(
    "age", "Age category", 
    choices = c(
      "&le; 6 months" = 1,
      "6 months - 17 years" = 2,
      "&ge; 18 years" = 3
    ),
    options = list(
      render = I(render)
    )
  )
)

server <- function(input, output){}

shinyApp(ui, server)

推荐阅读