问题
我正在尝试在 Shiny App 中呈现 R Markdown 文档,我已经使用 David 在这篇文章 (RMarkdown in Shiny Application) 中非常有用的解决方案成功实现了这一点。但是,我无法使用 .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}
}
Shiny 应用
应用.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)
结果
在应用程序外部编织 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 App,其中包含从 R markdown 文档呈现的 html 文档,以及引用。
应用.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 block 中包含“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 block 中包含“self_contained: true”时,元素不再丢失,但我无法访问使用 shinydashboard 的非 reprex Shiny 应用程序中的多个选项卡。
编辑:我现在已经在另一篇文章中记录了这个问题 Including a HTML file rendered from RMarkdown in R Shiny apps using shinydashboard is causing tabItems to break
正确的方法
编辑:这些问题现在已经使用下面的代码解决了,请注意 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)
最佳答案
如我上面更新的帖子中所述,要在 Shiny 应用程序中包含 HTML 文档,请使用以下代码(事后看来很明显!):
library(shiny)
library(knitr)
rmarkdown::render("RMarkdownFile.Rmd")
ui <- shinyUI(
fluidPage(
includeHTML("RMarkdownFile.html")
)
)
server <- function(input, output) { }
shinyApp(ui, server)
这适用于非常基本的 shiny 应用程序,但是使用 shinydashboard 会产生其他问题,此处详述,Including a HTML file rendered from RMarkdown in R Shiny apps using shinydashboard is causing tabItems to break
编辑:这些问题现在已经使用下面的代码解决了,请注意 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)
关于r - 在 R Shiny App 中呈现 R Markdown 文档时引用书目不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64368840/