Problem
我正在尝试在 Shiny 应用程序中渲染 R Markdown 文档,我已经使用 David 在这篇文章中非常有用的解决方案成功实现了这一点(Shiny 应用程序中的 RMarkdown)。但是,我无法使用 .bib 文件中的引用来呈现该文档,该文件与 Shiny 应用程序和 R Markdown 文档位于同一目录中。请在下面找到一个最小的可重现示例。
R Markdown 文档
RMarkdownFile.rmd
---
title: "RMarkdownFile"
author: "Test Author"
date: "15/10/2020"
output: html_document
bibliography: bibliography.bib
link-citations: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Statement
ggplot2 [@wickham2016ggplot2] is a great package!
## References
参考书目
参考书目.bib
@book{wickham2016ggplot2,
title={ggplot2: elegant graphics for data analysis},
author={Wickham, Hadley},
year={2016},
publisher={springer}
}
闪亮的应用程序
app.R
library(shiny)
library(knitr)
rmdfiles <- c("RMarkdownFile.rmd")
sapply(rmdfiles, knit, quiet = T)
ui <- shinyUI(
fluidPage(
withMathJax(includeMarkdown("RMarkdownFile.md"))
)
)
server <- function(input, output) { }
shinyApp(ui, server)
Result
在应用程序外部编织 RMarkdownFile.rmd 效果很好,产生以下输出
RMarkdownFile
Test Author
15/10/2020
Statement
ggplot2 (Wickham 2016) is a great package!
References
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. springer.
然而,如上所述在 Shiny 应用程序中渲染 RMarkdownFile.md 无法从 bibliography.bib 文件生成文档中的引文和参考文献,如下所示
Statement
ggplot2 [@wickham2016ggplot2] is a great package!
References
更新 - 解决方案
在尝试了几种不同的方法之后,最简单的方法最终成功了。下面是更新后的 Shiny 应用程序,其中包括从 R markdown 文档呈现的 html 文档以及引用。
app.R
library(shiny)
library(knitr)
rmarkdown::render("RMarkdownFile.Rmd")
ui <- shinyUI(
fluidPage(
includeHTML("RMarkdownFile.html")
)
)
server <- function(input, output) { }
shinyApp(ui, server)
附加问题
然而......虽然这适用于从 R markdown 生成的基本 HTML 文档,但在 RMarkdown 文档的 YAML 块中包含“self_contained: false”时,如下所示
---
title: "RMarkdownFile"
author: "Test Author"
date: "15/10/2020"
output:
html_document:
self_contained: false
bibliography: bibliography.bib
link-citations: yes
---
导致放置在补充文件夹中的项目(在本例中标题为“RMarkdownFile_files”)无法被 Shiny 应用程序找到。通过 Rstudio devtools 检查 Shiny 中缺失的元素会发现以下错误:
无法加载资源:服务器响应状态为 404(未找到)
相反,当在 YAML 块中包含“self_contained: true”时,元素不再丢失,但我无法访问使用闪亮仪表板的非 reprex Shiny 应用程序中的多个选项卡。
编辑:我现在已经在另一篇文章中记录了这个问题使用闪亮仪表板在 R Shiny 应用程序中包含从 RMarkdown 渲染的 HTML 文件会导致 tabItems 损坏
正确方法
编辑:这些问题现已使用下面的代码得到解决,请注意,RMarkdownFile.html 文件必须放置在 Shiny 应用程序目录中的“www”文件夹中,除非直接在那里渲染。
library(shiny)
library(knitr)
rmarkdown::render("RMarkdownFile.Rmd")
ui <- shinyUI(
fluidPage(
htmltools::tags$iframe(src = "RMarkdownFile.html", width = '100%', height = 1000, style = "border:none;"))
)
)
server <- function(input, output) { }
shinyApp(ui, server)