在R Shiny App中渲染R Markdown文档时,书目不起作用 [英] Bibliography not working when rendering a R Markdown document within an R Shiny App

查看:110
本文介绍了在R Shiny App中渲染R Markdown文档时,书目不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Shiny App中渲染R Markdown文档,在本文中,我使用David的非常有用的解决方案成功实现了R Markdown文档(

I am attempting to render an R Markdown document within a Shiny App, which I have achieved successfully using David's very helpful solution in this post (RMarkdown in Shiny Application). However, I have been unable to render the document with citations from a .bib file which is placed in the same directory as both the Shiny app and R Markdown document. Please find a minimum reproducible example below.

RMarkdownFile.rmd

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

参考书目

bibliography.bib

Bibliography

bibliography.bib

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

发光的应用程序

app.R

Shiny App

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可以正常工作,产生以下输出

Result

Knitting the RMarkdownFile.rmd outside of the app works perfectly, producing the following output

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文件生成文档中的引文和引用,如下所示

However rendering RMarkdownFile.md within the Shiny app as above fails to generate the citations and references in the document from the bibliography.bib file, as below

Statement
ggplot2 [@wickham2016ggplot2] is a great package!

References

更新-解决方案

在尝试了几种不同的方法之后,最简单的方法最终奏效了.以下是更新的Shiny App,其中包括从R markdown文档渲染的html文档,并带有引用.

Update - Solution

After playing around with a few different methods, the simplest method ended up working. Below is the updated Shiny App which includes the html document rendered from R markdown document, with citations.

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文档,但当包含"self_contained:false"时,在RMarkdown文档的YAML块中,如下所示

Additional problem

However..... whilst this works with basic HTML documents produced from R markdown, when including "self_contained: false" in the YAML chunk of the RMarkdown document as follows

---
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中缺少的元素会发现以下错误:

results in items that are placed in a supplementary folder, which in this case would be entitled "RMarkdownFile_files", failing to be found by the Shiny app. Inspecting the missing elements in Shiny via Rstudio devtools reveals the following error:

无法加载资源:服务器的响应状态为404(未找到)

Failed to load resource: the server responded with a status of 404 (Not Found)

相反,当包含"self_contained:true"时,在YAML块中,这些元素不再丢失,但是我无法使用我的使用Shinydashboard的非reprex Shiny应用程序中的多个选项卡.

Conversely when including "self_contained: true" in the YAML chunk, the elements are no longer missing, but I lose access to multiple tabs in my non-reprex Shiny app which uses shinydashboard.

我现在在另一篇文章

I have now documented this problem in an additional post Including a HTML file rendered from RMarkdown in R Shiny apps using shinydashboard is causing tabItems to break

现在已经使用下面的代码解决了这些问题,请注意,必须将RMarkdownFile.html文件放在"www"目录中.文件夹,除非直接在闪亮"应用程序目录中显示.

these problems have now been resolved using the code below, note that the RMarkdownFile.html file must be placed in the "www" folder in the Shiny app directory unless rendered there directly.

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文档,请使用以下代码(事后看来很明显!):

As detailed in my updated post above, to include a HTML document in a Shiny app use the following code (obvious in hindsight!):

library(shiny)
library(knitr)

rmarkdown::render("RMarkdownFile.Rmd")

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

shinyApp(ui, server)

这适用于非常基本的闪亮应用程序,但是使用Shinydashboard会产生此处详细介绍的其他问题,

This works for a very basic shiny app, however using shinydashboard produces additional problems detailed here, Including a HTML file rendered from RMarkdown in R Shiny apps using shinydashboard is causing tabItems to break

现在已经使用下面的代码解决了这些问题,请注意,必须将RMarkdownFile.html文件放在"www"目录中.文件夹,除非直接在闪亮"应用程序目录中显示.

these problems have now been resolved using the code below, note that the RMarkdownFile.html file must be placed in the "www" folder in the Shiny app directory unless rendered there directly.

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 Shiny App中渲染R Markdown文档时,书目不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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