【问题标题】:Bibliography not working when rendering a R Markdown document within an R Shiny App在 R Shiny App 中渲染 R Markdown 文档时,参考书目不起作用
【发布时间】:2021-01-29 18:38:12
【问题描述】:

问题

我正在尝试在 Shiny 应用程序中呈现 R Markdown 文档,我在这篇文章 (RMarkdown in Shiny Application) 中使用 David 非常有用的解决方案成功地实现了这一点。但是,我无法使用 .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)

结果

在应用程序外部编织 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 文档,并带有引用。

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”时,元素不再丢失,但我无法访问我的使用 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)

【问题讨论】:

  • 您的意思是在 app.R 中写入“.rmd”:withMathJax(includeMarkdown("RMarkdownFile.md"))
  • 嗨休伯特,感谢您的评论,我不这么认为,这个解决方案 (stackoverflow.com/questions/33499651/…) 在将 .Rmd 文件包含在 Shiny 应用程序中之前将其编织为 .md,我错误地采用了这个方法首先为一个 HTML 文档!我刚刚用解决方案更新了帖子,这揭示了更多问题......

标签: r shiny r-markdown knitr bibtex


【解决方案1】:

正如我在上面更新的帖子中所详述的,要在 Shiny 应用程序中包含 HTML 文档,请使用以下代码(事后看来很明显!):

library(shiny)
library(knitr)

rmarkdown::render("RMarkdownFile.Rmd")

ui <- shinyUI(
    fluidPage(
        includeHTML("RMarkdownFile.html")
  )
)
server <- function(input, output) { }

shinyApp(ui, server)

这适用于一个非常基本的闪亮应用程序,但是使用闪亮仪表板会产生额外的问题,这里详述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)

【讨论】:

  • 我建议:rmarkdown::render("markdown.Rmd", output_dir = "www")
猜你喜欢
  • 2018-10-07
  • 1970-01-01
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多