使bslib::card_header动态化

huangapple go评论84阅读模式
英文:

Make bslib::card_header dynamic

问题

I am building a shiny server module. The UI part is a bslib::card(), like this:

  1. my_mod_ui <- function(id) {
  2. ns <- shiny::NS(id)
  3. bslib::card(
  4. id=id,
  5. full_screen = TRUE,
  6. bslib::card_header(paste("Selected: ", shiny::textOutput(ns("selected")))),
  7. bslib::layout_sidebar(
  8. sidebar = bslib::sidebar(
  9. selectInput(ns("choice"), choices=letters(1:5))
  10. ),
  11. "Main contents"
  12. )
  13. )
  14. }

The server part in this minimal example does simply update the selected output variable:

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

This code does not work, as the following app shows:

  1. ui <- bslib::page_navbar(
  2. bslib::nav_panel("Test", my_mod_ui("id1"))
  3. )
  4. server <- function(input, output, session) {
  5. my_mod_srv("id1")
  6. }
  7. shinyApp(ui, server)

The problem seems to be that output$selected is rendered to an HTML element <div id="id1-selected" class="shiny-text-output"></div>.

How can I make the card header dynamic?

(Note that I am using the github version of bslib, the CRAN version does not have a card yet.)

英文:

I am building a shiny server module. The UI part is a bslib::card(), like this:

  1. my_mod_ui &lt;- function(id) {
  2. ns &lt;- shiny::NS(id)
  3. bslib::card(
  4. id=id,
  5. full_screen = TRUE,
  6. # the following does not work properly:
  7. bslib::card_header(paste(&quot;Selected: &quot;, shiny::textOutput(ns(&quot;selected&quot;)))),
  8. bslib::layout_sidebar(
  9. sidebar = bslib::sidebar(
  10. selectInput(ns(&quot;choice&quot;), choices=letters(1:5))
  11. ),
  12. &quot;Main contents&quot;
  13. )
  14. )
  15. }

The server part in this minimal example does simply update the selected output variable:

  1. my_mod_srv &lt;- function(id)
  2. shiny::moduleServer(id, function(input, output, session) {
  3. output$selected &lt;- renderText(input$choice)
  4. })

This code does not work, as the following app shows:

  1. ui &lt;- bslib::page_navbar(
  2. bslib::nav_panel(&quot;Test&quot;, my_mod_ui(&quot;id1&quot;))
  3. )
  4. server &lt;- function(input, output, session) {
  5. my_mod_srv(&quot;id1&quot;)
  6. }
  7. shinyApp(ui, server)

The problem seems to be that output$selected is rendered to an HTML element &lt;div id=&quot;id1-selected&quot; class=&quot;shiny-text-output&quot;&gt;&lt;/div&gt;.

How can I make the card header dynamic?

(note that I am using the github version of bslib, the CRAN version does not have a card yet.)

答案1

得分: 1

可以将 paste 部分移入 renderText

注意:假设 output$wz_title 是你所说的 output$selected

英文:

One option would be to move the paste part into renderText:

Note: Assuming that output$wz_title is what you mean by output$selected.

  1. library(bslib)
  2. library(shiny)
  3. my_mod_ui &lt;- function(id) {
  4. ns &lt;- shiny::NS(id)
  5. bslib::card(
  6. id = id,
  7. full_screen = TRUE,
  8. bslib::card_header(shiny::textOutput(ns(&quot;selected&quot;))),
  9. bslib::layout_sidebar(
  10. sidebar = bslib::sidebar(
  11. selectInput(ns(&quot;choice&quot;), &quot;&quot;, choices = letters[1:5])
  12. ),
  13. &quot;Main contents&quot;
  14. )
  15. )
  16. }
  17. my_mod_srv &lt;- function(id) {
  18. shiny::moduleServer(id, function(input, output, session) {
  19. output$selected &lt;- renderText(
  20. paste(&quot;Selected: &quot;, input$choice)
  21. )
  22. })
  23. }
  24. ui &lt;- bslib::page_navbar(
  25. bslib::nav_panel(&quot;Test&quot;, my_mod_ui(&quot;id1&quot;))
  26. )
  27. server &lt;- function(input, output, session) {
  28. my_mod_srv(&quot;id1&quot;)
  29. }
  30. shinyApp(ui, server)

使bslib::card_header动态化

huangapple
  • 本文由 发表于 2023年6月12日 23:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定