使bslib::card_header动态化

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

Make bslib::card_header dynamic

问题

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

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

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

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

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

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

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

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:

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

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

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

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

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

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

ui &lt;- bslib::page_navbar(
	bslib::nav_panel(&quot;Test&quot;, my_mod_ui(&quot;id1&quot;))
)

server &lt;- function(input, output, session) {
	my_mod_srv(&quot;id1&quot;)
}

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.

library(bslib)
library(shiny)

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

my_mod_srv &lt;- function(id) {
  shiny::moduleServer(id, function(input, output, session) {
    output$selected &lt;- renderText(
      paste(&quot;Selected: &quot;, input$choice)
    )
  })
}

ui &lt;- bslib::page_navbar(
  bslib::nav_panel(&quot;Test&quot;, my_mod_ui(&quot;id1&quot;))
)

server &lt;- function(input, output, session) {
  my_mod_srv(&quot;id1&quot;)
}

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:

确定