You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rshinydashboard中使用includeHTML后sidebarMenu无法收缩的问题求助

Fix: sidebarMenu Items Won't Collapse After Using includeHTML in shinydashboard

I’ve run into this exact headache before! The root cause is duplicate JavaScript/CSS libraries being loaded when you use includeHTML to import a full HTML document from rmarkdown. Your shinydashboard app already ships with jQuery, Bootstrap, and other core dependencies, and the imported HTML file adds another copy of these tools—this breaks the event bindings that make the sidebar menu’s expand/collapse functionality work.

Why This Happens

When you render an html_document with rmarkdown, the output includes a complete HTML structure: a <head> section with all required JS/CSS (like jQuery and Bootstrap) and a <body> with your content. When you drop this full document into your shiny app via includeHTML, you’re loading those libraries a second time. This conflict overwrites or disrupts the original event handlers that control the sidebar menu’s behavior.

Fix 1: Use HTML Fragments Instead of Full Documents

The cleanest solution is to generate an HTML fragment (no full page structure) from your rmarkdown file, so you only import your content without duplicate libraries.

Step 1: Update Your RMD File

Modify the output format to html_fragment (this skips the full page wrapper):

---
title: "test"
output: html_fragment
---
## Test HTML Document
This is just a test.

Step 2: Render the Fragment

Render it the same way—you’ll get a file with only your content, no <head> or <body> tags:

rmarkdown::render(
  input = "~/test.rmd",
  output_format = "html_fragment",
  output_file = file.path(tempdir(), "Test_Fragment.html")
)

Step 3: Update Your Shiny App

Use the fragment with includeHTML—no more conflicts:

server <- function(input, output) {
  output$texta <- renderText("showing tab A")
  output$textaa <- renderText("showing tab AA")
  output$textab <- renderText("showing tab AB")
  # Load the fragment instead of the full HTML document
  output$uia <- renderUI(includeHTML(path = file.path(tempdir(), "Test_Fragment.html")))
}

Fix 2: Embed the HTML in an iframe

If you need to keep the full HTML document (for custom styling or other reasons), embedding it in an iframe isolates it from the app’s JS/CSS, avoiding conflicts entirely:

output$uia <- renderUI({
  tags$iframe(
    src = file.path(tempdir(), "Test.html"),
    width = "100%",
    height = "400px",
    frameBorder = 0
  )
})

The embedded HTML runs in its own isolated context, so its libraries won’t interfere with the shinydashboard’s functionality.

Fix 3: Manually Extract Content from the Full HTML

If you already have a full HTML file and can’t re-render it as a fragment, you can extract only the content inside the <body> tag using rvest:

library(rvest)

# Extract content from the full HTML file
html_content <- read_html(file.path(tempdir(), "Test.html")) %>%
  html_element("body") %>%
  html_children() %>%
  html_parse()

output$uia <- renderUI(HTML(as.character(html_content)))

Any of these fixes should resolve the sidebar menu collapse issue by eliminating library conflicts or isolating the external HTML content.

内容的提问来源于stack exchange,提问作者Jason

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:05:27