如何在R Shiny的shinyBS弹出框中嵌入rpivotTable
Embed rpivotTable into shinyBS Popover in Shiny
Got it, let's figure out how to put an rpivotTable inside a shinyBS popover! The main trick is that we can't just pass the pivot table directly as plain content—we need to render it in the UI first (hidden by default) and then reference it in the popover. Here's a complete working example:
Full Code
library(shiny) library(shinyBS) library(rpivotTable) ui <- fluidPage( # Action button to trigger the popover actionButton("CR1_S1", "Show Pivot Table Popover"), # Hidden container for the rpivotTable (won't show on main page) div( id = "pivot_container", style = "display: none;", rpivotTableOutput("pivot_table") ), # Popover configuration bsPopover( id = "CR1_S1", title = "My Embedded Pivot Table", # Reference the hidden pivot table using its ID (HTML enabled) content = HTML("#pivot_container"), placement = "right", options = list( container = "body", html = TRUE # Critical: allows rendering HTML/Shiny elements ) ) ) server <- function(input, output) { # Render the rpivotTable with sample data (mtcars) output$pivot_table <- renderRpivotTable({ rpivotTable( data = mtcars, rows = "cyl", cols = "gear", vals = "mpg", aggregatorName = "Average", rendererName = "Table" ) }) } shinyApp(ui, server)
Key Details to Note:
- Hidden Container: We wrap the
rpivotTableOutputin a hidden<div>so it doesn't appear on the main Shiny page—only inside the popover when triggered. - HTML Enabled: Setting
html = TRUEin the popover options is essential. This tells shinyBS to parse the content as HTML, which lets us reference the hidden pivot table container. - Container Option: Using
container = "body"helps avoid CSS styling conflicts between the popover and the main Shiny UI, ensuring the pivot table renders correctly. - Dynamic Rendering: The rpivotTable is rendered server-side just like any other Shiny output, so it will update if your data changes (just adjust the
renderRpivotTablelogic as needed).
内容的提问来源于stack exchange,提问作者Adam Shaw
相关产品推荐
相关产品推荐

