【问题标题】:sidebarMenu does not function properly when using includeHTML使用 includeHTML 时,sidebarMenu 无法正常工作
【发布时间】:2018-10-29 22:41:55
【问题描述】:

我正在使用 Rshinydashboard,当我尝试使用 includeHTML 在我的应用程序中包含一个 html 文档时遇到了问题。一旦 menuItems & menSubItems 被展开,它们就不能被收回。我探索了其他解决方案,但没有找到。如果您知道可能是什么问题,或者有另一种在应用程序中包含 html 报告的方式,我将不胜感激。请查看下面的代码,如果可以,请提供帮助!

创建一个 RMD 文件来创建一个 html 报告(如果你没有一个在附近)

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

构建测试 html 报告

# Build Test HTML file
rmarkdown::render(
  input = "~/test.rmd",
  output_format = "html_document",
  output_file = file.path(tempdir(), "Test.html")
)

构建测试应用

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebarmenu",
      menuItem(
        "A", tabName = "a",  icon = icon("group", lib="font-awesome"),
        menuSubItem("AA", tabName = "aa"),
        conditionalPanel(
          "input.sidebarmenu === 'aa'",
          sliderInput("b", "Under sidebarMenu", 1, 100, 50)
        ),
        menuSubItem("AB", tabName = "ab")
      )
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "a", textOutput("texta")),
      tabItem(tabName = "aa", textOutput("textaa"), uiOutput("uia")),
      tabItem(tabName = "ab", textOutput("textab"))
    )
  )
)

server <- function(input, output) {
  output$texta <- renderText("showing tab A")
  output$textaa <- renderText("showing tab AA")
  output$textab <- renderText("showing tab AB")
  output$uia <- renderUI(includeHTML(path = file.path(tempdir(), "Test.html")))
}

shinyApp(ui, server)

【问题讨论】:

  • 我认为 rmarkdown 文件中的一个或多个 javascript 库导入与从 Shiny 加载的 javascript 库冲突。你可以渲染("~/test.rmd", html_document(self_contained = F))。注释掉所有 行,bug 就消失了,但这不是一个很好的解决方案。
  • 请参阅 groups.google.com/forum/#!topic/shiny-discuss/0UiiHcdhN4k 了解有关此问题的讨论。

标签: r shiny shinydashboard


【解决方案1】:

那是因为你在闪亮的 UI 中包含了一个完整的 HTML 文件,并且 你应该只包含&lt;body&gt;&lt;/body&gt;之间的内容(引自yihui)

一种解决方案可能是在运行rmarkdown::render() 后运行额外的一行来自动修复您的Test.html

xml2::write_html(rvest::html_node(xml2::read_html("Test.html"), "body"), file = "Test2.html")

然后有

output$uia &lt;- renderUI(includeHTML(path = file.path(tempdir(), "Test2.html")))

【讨论】:

  • 非常感谢您的帮助。该解决方案对我来说非常有效,并且很有帮助!我认为@Ben 是正确的,这是与 javascript 库冲突的问题。此解决方案通过仅获取 html 文件的主体来避免将 Rmarkdown javascript 库包含在 html 文件中。
  • @RolandASc 如果 标签有一些为 body 使用的特定代码定义的 JS、CSS 怎么办?如何解决?感谢您的回复
  • 同样的问题,解决方案对我有用,但我定义了一些我想保留的 JS 和 CSS ..
【解决方案2】:

你忘了花括号 - renderUI 需要一个表达式作为参数。

renderUI({ includeHTML(...) })

代码

  output$uia <- renderUI({includeHTML(path = file.path(tempdir(), "Test.html"))})

工作正常。

或者你可以使用这个代码

output$uia <- renderUI(includeMarkdown(path = file.path("test.rmd")))

在这种情况下,您需要指定文件test.rmd的路径,这里它与源文件位于同一目录。

【讨论】:

  • 感谢您抽出宝贵时间,但我发现这并没有改变任何东西,我看不出大括号会如何改变任何东西。通常,大括号用于包含一个代码块而不是跨越多行。 menuItems 和 menuSubItems 仍然不能正常工作。一旦 menuSubItems 显示出来,它们就不能再次隐藏(通过单击“A”选项卡)。
  • @Jason,我们如何在documentation 中看到,方法“renderUI”有一个参数 expr,它是一个表达式。这意味着在任何情况下我们都必须使用大括号——将函数 includeHTML 自动转换为未提供的表达式。
  • @Jason,也许这会有所帮助:output$uia &lt;- renderUI(includeMarkdown(path = file.path("test.rmd"))) 在这种情况下,您需要指定文件 test.rmd 的路径,这里它与源文件位于同一目录中。跨度>
  • 我也看不到大括号应该做什么。但是使用降价文件的解决方法对我有用。
  • 另外,我认为在闪亮的 Rmarkdown 代码中包含 html 文件也可以。
猜你喜欢
  • 2013-03-03
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
相关资源
最近更新 更多