【问题标题】:updateSelectInput label doesn't accept HTML tags?updateSelectInput 标签不接受 HTML 标签?
【发布时间】:2017-11-15 04:18:21
【问题描述】:

我的 Shiny 应用程序中的“selectInput”和“updateSelectInput”组合出现问题(我是 Shiny 的新手,但似乎无法在任何地方找到该问题的答案)。我想用 html 标签格式化标签,如下面的基本示例所示(例如,分成两行,更改字体大小)。这适用于“selectInput”,但“updateSelectInput”无法消化相同的标签并输出“[object Object]”。在我看来,它无法处理 html 标签。有什么解决方法吗???谢谢!

ui.R:

# Load libraries needed for app
library(shiny)
library(shinydashboard)

# Define the overall UI with a dashboard page template
shinyUI(
   dashboardPage(
      dashboardHeader(title = "dashboard header"),
      dashboardSidebar( 
         #Create first dropdown box
         selectInput("choice1", "First choice:",1:5,selected=NULL),

         #Create second dropdown box
         selectInput("choice2",  p("Then, make your ", tags$br(), tags$small("second choice")), c("a","b","c","d","e"))
      ),
      dashboardBody()
   ) 
)

server.R:

# Load libraries needed for app
library(shiny)
library(shinydashboard)

# Define server for the Shiny app
shinyServer(function(input, output,session) {
   # populate second dropdown box when a choice in first dropdown box is made
   observe({
      updateSelectInput(session, "choice2", p("Then, make your ", tags$br(), tags$small("second choice")), c("a","b","c","d","e"))
   })
}) 

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    在您的描述/代码中不清楚您想要完成什么。话虽如此,您注意到在 updateSelectInput 中更改了标签,因此无需在更新命令中重复标签。你的观察中也没有输入(,它什么也不做。我将代码更改为对 input$choice1 做出反应,但是你需要添加一些代码来更新choice2中的选择。

    您也可以使用 renderUI/uiOutput 来更新服务器中的控件,这样就可以避免标签问题。

    # Load libraries needed for app
    library(shiny)
    library(shinydashboard)
    
    # Define server for the Shiny app
    server <- shinyServer(function(input, output,session) {
       # populate second dropdown box when a choice in first dropdown box is made
       observeEvent( input$choice1  ,{
          updateSelectInput(session = session,
                            inputId =  "choice2",
                           # label = p("Then, make your ", tags$br(), tags$small("second choice")),
                            choices = c("a","b","c","d","e"))
       })
    }) 
    
    
    # Define the overall UI with a dashboard page template
    ui <- shinyUI(
       dashboardPage(
          dashboardHeader(title = "dashboard header"),
          dashboardSidebar( 
             #Create first dropdown box
             selectInput(inputId = "choice1",
                         label = "First choice:",
                         choices = 1:5,
                         selected=NULL),
    
             #Create second dropdown box
             selectInput(inputId = "choice2",
                         label = p("Then, make your ",    tags$br(),  tags$small("second choice")),
                         choices =  c("a","b","c","d","e"))
          ),
          dashboardBody()
       ) 
    )
    
    shinyApp(ui, server)
    

    【讨论】:

    • 嗨罗恩,非常感谢您的快速回复。首先,注释掉 updateSelectInput 中的标签解决了我的问题!!!虽然我仍然不明白为什么它不接受 selectInput 首先接受的同样的东西 - 有什么想法吗?我没有在观察中包含任何内容,因为它不是重点,但我想我当时可以完全删除它。再次感谢!
    • updateSelectInput 很棘手。您还可以在 selectInput 中设置 label= NULL 并将标签添加为 UI 中控件上方的 html 段落,或使用 renderUI/uiOutput 在服务器中。我通常这样做更灵活。
    • stackoverflow.com/questions/40341804/… 提供了使用shinyjs::html("labelText", "&lt;b&gt;second choice&lt;/b&gt;") 更新标签的解决方案,只要您将标签放入例如div("First choice:", id = "labelText").
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2016-05-22
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多