在使用 Shinydashboard 的 R Shiny 应用程序中包含从 RMarkdown 呈现的 HTML 文件会导致 tabItems 中断 [英] Including a HTML file rendered from RMarkdown in R Shiny apps using shinydashboard is causing tabItems to break

查看:52
本文介绍了在使用 Shinydashboard 的 R Shiny 应用程序中包含从 RMarkdown 呈现的 HTML 文件会导致 tabItems 中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 Shinydashboard 在 ShinyApp 中包含从 RMarkdown 呈现的 HTML 文档时,HTML 文档仅在设置self_contained:"时才能正确呈现.在 RMarkdown 文件的 YAML 块中设置为 true.但是,这样做会导致您无法从 Shinydashboard 的 sidebarMenu 中选择 tabItems.

When including a HTML document rendered from RMarkdown in a ShinyApp using shinydashboard, the HTML document is only rendered correctly when the setting "self_contained:" in the YAML chunk of the RMarkdown file is set to true. However, doing this causes you to be unable to select tabItems from the sidebarMenu in shinydashboard.

相反,当设置self_contained:"时设置为 false,HTML 文档的元素,例如绘图和浮动目录缺少(附加文件中存在非 HTML 元素),但您可以从 sidebarMenu 中选择 tabItems,并且应用的其余部分工作正常.

Conversely, when the setting "self_contained:" is set to false, elements of the HTML document such as plots and a floating table of contents are missing (non-HTML elements present in the additional files), but you are able to select tabItems from the sidebarMenu, and the rest of the app works fine.

理想情况下,您可以在 Shinydashboard 中包含从 RMarkdown 呈现的功能齐全的 HTML 文件,同时保留应用程序其余部分的功能.

Ideally, you would be able to include a fully functioning HTML file rendered from RMarkdown in shinydashboard, whilst retaining functionality in the rest of the app.

我之前关于如何在基本 Shiny 应用程序中包含 HTML 的帖子提到了这个额外的问题(在 R Shiny 应用程序中呈现 R Markdown 文档时参考书目不起作用).

My previous post regarding how to include a HTML in a basic Shiny app refers to this additional problem (Bibliography not working when rendering a R Markdown document within an R Shiny App).

请在下面找到一个最低限度的可重现示例.

Please find a minimum reproducible example below.

RMarkdownFile.Rmd

RMarkdownFile.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

Bibliography

bibliography.bib

@book{wickham2016ggplot2,
  title={ggplot2: elegant graphics for data analysis},
  author={Wickham, Hadley},
  year={2016},
  publisher={springer}
}

闪亮应用

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(
                box(title = "Test Section HTML",
                    width = 12,
                    includeHTML("RMarkdownFile.html")))
              )
      )
    )
)
  
  
  

server <- function(input, output) { }

shinyApp(ui, server)

对解决此问题的任何帮助将不胜感激!

Any help in solving this problem would be greatly appreciated!

推荐答案

Stéphane 在上面的评论中解决了这个问题!

This question was resolved by Stéphane in the comments above!

请在下面找到最小可重现示例 app.R 的工作版本:

Please find a working version of the minimum reproducible example app.R below:

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"文件必须手动放置在名为www"的文件夹中.在 Shiny 应用程序目录中,除非RMarkdownFile.Rmd"文件被直接渲染到这个www"文件夹.

For this solution to work the "RMarkdownFile.html" file must be manually placed in a folder called "www" in the Shiny app directory, unless the "RMarkdownFile.Rmd" file is rendered directly to this "www" folder.

这篇关于在使用 Shinydashboard 的 R Shiny 应用程序中包含从 RMarkdown 呈现的 HTML 文件会导致 tabItems 中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆