【问题标题】:How to use html in radio button choices r shiny如何在单选按钮选择中使用 html r 闪亮
【发布时间】:2020-06-20 07:04:27
【问题描述】:

我正在尝试使用 HTML 代码更新服务器端的单选按钮列表,但没有成功。
我正在使用这个例子让你了解我想要做什么。我再次提到必须在服务器端,因为我列表中的元素将与用户进行的其他输入相关。

有人可以帮忙弄清楚它是如何制作的吗? 谢谢

## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(
radioButtons("rb", "Choose one:",
             choiceNames = list("icon", "html", "text"),
             choiceValues = c(1,2,3)),
 textOutput("txt")
) 


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


a<-HTML("<p style='color:red;'>option2</p>")
list1=as.list(c("option1",a,"option3"))

  updateRadioButtons(session, "rb", choiceNames = list1, choiceValues = c(1,2,3))



output$txt <- renderText({
  paste("You chose", input$rb)
})

}

shinyApp(ui, server)
}

【问题讨论】:

    标签: html r shiny shinyapps


    【解决方案1】:

    你的代码有一些问题:

    choiceValues 必须是原子向量,而不是列表

    ui <- fluidPage(
      radioButtons("rb", "Choose one:",
                   choiceNames = list("icon", "html", "text"),
                   choiceValues = c(1,2,3)),
      textOutput("txt")
    )
    

    如果您使用updateXXX,则必须将参数session 设置为server 函数:

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

    updateRadioButtons 中,您必须同时设置choiceNameschoiceValues

      if(TRUE){  
        list=list(icon("calendar"),
                  HTML("<p style='color:red;'>Red Text</p>"),
                  "Normal text"
        )
        updateRadioButtons(session, "rb", choiceNames = list, choiceValues = c(1,2,3))
      }
    

    icon 不起作用。

    【讨论】:

    • 谢谢,我已经更新了示例以便您更好地理解问题。当我将 HTML 内容连接到向量中时,它不起作用。
    • @HugoSilva 是的,你不能那样做。做list("option1", a, "option3").
    猜你喜欢
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多