使bslib::card_header动态化

f0brbegy  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(84)

我正在构建一个闪亮的服务器模块。UI部分是bslib::card(),如下所示:

my_mod_ui <- function(id) {
    ns <- shiny::NS(id)
    bslib::card(
        id=id,
        full_screen = TRUE, 

        # the following does not work properly:
        bslib::card_header(paste("Selected: ", shiny::textOutput(ns("selected")))), 

        bslib::layout_sidebar(
            sidebar = bslib::sidebar(
                selectInput(ns("choice"), choices=letters(1:5))
            ), 
            "Main contents"
        )
    )
}

在这个最小的例子中,服务器部分只更新了selected输出变量:

my_mod_srv <- function(id) 
    shiny::moduleServer(id, function(input, output, session) {  
        output$selected <- renderText(input$choice)
})

此代码不起作用,如以下应用程序所示:

ui <- bslib::page_navbar(
    bslib::nav_panel("Test", my_mod_ui("id1"))
)

server <- function(input, output, session) {
    my_mod_srv("id1")
}

shinyApp(ui, server)

问题似乎是output$selected被呈现为HTML元素<div id="id1-selected" class="shiny-text-output"></div>
如何使卡片标题动态化?
(note我正在使用bslib的github版本,CRAN版本还没有card

bq3bfh9z

bq3bfh9z1#

一个选项是将paste部分移动到renderText中:
注意:假设output$wz_title就是你所说的output$selected

library(bslib)
library(shiny)

my_mod_ui <- function(id) {
  ns <- shiny::NS(id)
  bslib::card(
    id = id,
    full_screen = TRUE,
    bslib::card_header(shiny::textOutput(ns("selected"))),
    bslib::layout_sidebar(
      sidebar = bslib::sidebar(
        selectInput(ns("choice"), "", choices = letters[1:5])
      ),
      "Main contents"
    )
  )
}

my_mod_srv <- function(id) {
  shiny::moduleServer(id, function(input, output, session) {
    output$selected <- renderText(
      paste("Selected: ", input$choice)
    )
  })
}

ui <- bslib::page_navbar(
  bslib::nav_panel("Test", my_mod_ui("id1"))
)

server <- function(input, output, session) {
  my_mod_srv("id1")
}

shinyApp(ui, server)

相关问题