使用shinydashboard在R Shinny应用程序中包含从RMarkdown呈现的HTML文件会导致tabItems中断 问题
当使用shinydashboard在ShinyApp中包含从RMarkdown呈现的HTML文档时,只有当RMarkdown文件的YAML块中的设置“self_contained:”设置为true时,才会正确呈现HTML文档。但是,这样做会导致无法从shinydashboard的侧边栏菜单中选择tabItems 相反,当设置“self_contained:”设置为false时,HTML文档的元素,如绘图和浮动目录 缺少(附加文件中存在非HTML元素),但您可以从侧边栏菜单中选择tabItems,应用程序的其余部分工作正常 理想情况下,您将能够在shinydashboard中包含从RMarkdown呈现的功能齐全的HTML文件,同时在应用程序的其余部分保留功能 我之前关于如何在一个基本的闪亮应用程序中包含HTML的文章提到了这个额外的问题() 请在下面找到一个最小的可复制示例 最小重现性示例 r标记文件 RMarkdownFile.Rmd使用shinydashboard在R Shinny应用程序中包含从RMarkdown呈现的HTML文件会导致tabItems中断 问题,html,r,shiny,r-markdown,shinydashboard,Html,R,Shiny,R Markdown,Shinydashboard,当使用shinydashboard在ShinyApp中包含从RMarkdown呈现的HTML文档时,只有当RMarkdown文件的YAML块中的设置“self_contained:”设置为true时,才会正确呈现HTML文档。但是,这样做会导致无法从shinydashboard的侧边栏菜单中选择tabItems 相反,当设置“self_contained:”设置为false时,HTML文档的元素,如绘图和浮动目录 缺少(附加文件中存在非HTML元素),但您可以从侧边栏菜单中选择tabItems,
---
title: "RMarkdownFile"
author: "Test Author"
date: "15/10/2020"
output:
html_document:
toc: true
toc_float: true
number_sections: true
self_contained: true
bibliography: bibliography.bib
link-citations: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
```
# Statement
ggplot2 [@wickham2016ggplot2] is a great package!
```{r plot, message=FALSE}
ggplot2::ggplot(data = mpg) +
ggplot2::geom_point(mapping = aes(x = displ, y = hwy))
```
## References
参考文献
参考书目
@book{wickham2016ggplot2,
title={ggplot2: elegant graphics for data analysis},
author={Wickham, Hadley},
year={2016},
publisher={springer}
}
闪亮应用
应用程序R
库(闪亮)
图书馆(shinydashboard)
图书馆(rmarkdown)
rmarkdown::render(“RMarkdownFile.Rmd”)
ui在上面的评论中,Stéphane解决了这个问题
请在下面找到最小可复制示例应用程序R的工作版本:
library(shiny)
library(shinydashboard)
library(rmarkdown)
rmarkdown::render("RMarkdownFile.Rmd")
ui <- dashboardPage(
dashboardHeader(title = "Test"),
dashboardSidebar(
sidebarMenu(id = "sidebarmenu",
menuItem("Test Section 1", tabName = "testitem1",
menuSubItem("Test Section 1a", tabName = "testitem1a"),
menuSubItem("Test Section 1b", tabName = "testitem1b")
),
menuItem("Test Section 2", tabName = "testitem2",
menuSubItem("Test Section 2a", tabName = "testitem2a"),
menuSubItem("Test Section 2b", tabName = "testitem2b")
),
menuItem("Test Section HTML", tabName = "testitemhtml"
)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "testitem1a",
fluidRow(
box(title = "Test Section 1a",
width = 12))
),
tabItem(tabName = "testitem1b",
fluidRow(
box(title = "Test Section 1b",
width = 12))
),
tabItem(tabName = "testitem2a",
fluidRow(
box(title = "Test Section 2a",
width = 12))
),
tabItem(tabName = "testitem2b",
fluidRow(
box(title = "Test Section 2b",
width = 12))
),
tabItem(tabName = "testitemhtml",
fluidPage(
htmltools::tags$iframe(src = "RMarkdownFile.html", width = '100%', height = 1000, style = "border:none;"))
)
)
)
)
server <- function(input, output) { }
shinyApp(ui, server)
库(闪亮)
图书馆(shinydashboard)
图书馆(rmarkdown)
rmarkdown::render(“RMarkdownFile.Rmd”)
用户界面使用iframe
(标记$iframe
),而不是包含TML
。你好,谢谢你的回复。这非常有效,我已经更新了上面的帖子,将您的解决方案的实现包含在我的最小可复制示例中。再次感谢!你可以发布自己问题的答案。这不是禁止的,这会更干净:)谢谢Stéphane,我现在将解决方案移到了答案:)很好:)仅供参考,includeHTML
不起作用,因为它包含文件的所有内容,包括加载JavaScript库、CSS的head
标记,和body
和html
标记,它们已经存在于闪亮的应用程序中,并且这些标记必须是唯一的。这造成了一片混乱。另一方面,iframe
工作是因为iframe
的内容与文档的其他内容完全隔离。
library(shiny)
library(shinydashboard)
library(rmarkdown)
rmarkdown::render("RMarkdownFile.Rmd")
ui <- dashboardPage(
dashboardHeader(title = "Test"),
dashboardSidebar(
sidebarMenu(id = "sidebarmenu",
menuItem("Test Section 1", tabName = "testitem1",
menuSubItem("Test Section 1a", tabName = "testitem1a"),
menuSubItem("Test Section 1b", tabName = "testitem1b")
),
menuItem("Test Section 2", tabName = "testitem2",
menuSubItem("Test Section 2a", tabName = "testitem2a"),
menuSubItem("Test Section 2b", tabName = "testitem2b")
),
menuItem("Test Section HTML", tabName = "testitemhtml"
)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "testitem1a",
fluidRow(
box(title = "Test Section 1a",
width = 12))
),
tabItem(tabName = "testitem1b",
fluidRow(
box(title = "Test Section 1b",
width = 12))
),
tabItem(tabName = "testitem2a",
fluidRow(
box(title = "Test Section 2a",
width = 12))
),
tabItem(tabName = "testitem2b",
fluidRow(
box(title = "Test Section 2b",
width = 12))
),
tabItem(tabName = "testitemhtml",
fluidPage(
htmltools::tags$iframe(src = "RMarkdownFile.html", width = '100%', height = 1000, style = "border:none;"))
)
)
)
)
server <- function(input, output) { }
shinyApp(ui, server)