繁体   English   中英

在使用 Shinydashboard 的 R Shiny 应用程序中包含从 RMarkdown 呈现的 HTML 文件会导致 tabItems 中断

[英]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 文件

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM