[英]Including a HTML file rendered from RMarkdown in R Shiny apps using shinydashboard is causing tabItems to break
當使用 Shinydashboard 在 ShinyApp 中包含從 RMarkdown 呈現的 HTML 文檔時,僅當 RMarkdown 文件的 YAML 塊中的設置“self_contained:”設置為 true 時,HTML 文檔才能正確呈現。 但是,這樣做會導致您無法從 Shinydashboard 的 sidebarMenu 中選擇 tabItems。
相反,當設置 "self_contained:" 設置為 false 時,HTML 文檔的元素(例如繪圖和浮動目錄)將丟失(附加文件中存在非 HTML 元素),但您可以從中選擇 tabItems sidebarMenu 和應用程序的其余部分工作正常。
理想情況下,您可以在 Shinydashboard 中包含從 RMarkdown 呈現的功能齊全的 HTML 文件,同時保留應用程序其余部分的功能。
我之前關於如何在基本的 Shiny 應用程序中包含 HTML 的帖子提到了這個額外的問題( 在 R Shiny 應用程序中呈現 R Markdown 文檔時參考書目不起作用)。
請在下面找到一個最小的可重現示例。
RMarkdown文件.Rmd
---
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
參考書目.bib
@book{wickham2016ggplot2,
title={ggplot2: elegant graphics for data analysis},
author={Wickham, Hadley},
year={2016},
publisher={springer}
}
應用程序
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(
box(title = "Test Section HTML",
width = 12,
includeHTML("RMarkdownFile.html")))
)
)
)
)
server <- function(input, output) { }
shinyApp(ui, server)
任何解決此問題的幫助將不勝感激!
這個問題由 Stéphane 在上面的評論中解決了!
請在下面找到最小可重現示例 app.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)
要使此解決方案起作用,必須將“RMarkdownFile.html”文件手動放置在 Shiny 應用程序目錄中名為“www”的文件夾中,除非將“RMarkdownFile.Rmd”文件直接呈現到此“www”文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.