RMarkdown在闪亮的应用程序中 [英] RMarkdown in Shiny Application
问题描述
是否有一种方法可以在闪亮的应用程序中插入(和评估)RMarkdown脚本. (请注意,我不是在RMarkdown中寻找一个闪亮的应用程序,而此处对此也没有解释,寻找闪亮的Markdown脚本(请参阅闪亮的画廊Markdown ))
Is there a way to insert (and evaluate) an RMarkdown script in a shiny application. (Note, I am not looking for a shiny application in RMarkdown that is explained here, nor am I looking for Markdown scripts in shiny (see Shiny Gallery Markdown))
我正在构建一个包含文本,方程式,代码块,图和交互式元素的应用程序.为了方便起见,我将Markdown文件用于文本和方程式,并希望有时在两者之间进行绘制(即在RMarkdown中编写大多数内容).由于闪亮的应用程序较为复杂(我使用shinydashboard
包括其许多独特功能),因此我希望使用不使用
I am building an application that has text, equations, code-chunks, plots, and interactive elements. For convenience I use Markdown files for the text and equations and would like to have a plot sometimes in between (i.e. write most stuff in RMarkdown). As the shiny-app is more complex (I use shinydashboard
including many of its unique features), I would prefer an option that does not use the approach described in the first link.
一个最小的工作示例是:
A minimum working example would be:
R文件:
library(shiny)
ui <- shinyUI(
fluidPage(
includeMarkdown("RMarkdownFile.rmd")
)
)
server <- function(input, output) {}
shinyApp(ui, server)
和"RMarkdownFile.rmd"在同一文件夹中:
and "RMarkdownFile.rmd" in the same folder:
This is a text
$$ E(x) = 0 $$
```{r, eval = T}
plot(rnorm(100))
```
结果:
如果我编织rmd
文件,我想要的是输出:
What I want to have is the output if I knit the rmd
-file:
特别是,我想获得代码块的评估(绘制一些东西...),并且我想获得渲染的数学方程式.
Specifically, I want to get the evaluation of the code-chunks (plot something...), and I want to get the rendered math equations.
有什么想法吗?
感谢@Bunk的输入,我选择使用命令knit
将所有rmd
文件渲染为md
文件,然后将md
文件包含在闪亮的应用程序中(我使用markdown而不是html因为后者产生了一些方程问题.最后,将includeMarkdown
包裹在withMathJax
中,以确保正确显示方程式.
Thanks to the input of @Bunk, I chose to render all rmd
files to md
files with the command knit
and then include the md
files in the shiny app (I use markdown instead of html as the latter produced some issues with equations). Lastly, the includeMarkdown
is wrapped in withMathJax
to ensure the proper display of equations.
最终代码如下:
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)
推荐答案
我认为编织并呈现UI应该可以.
I think knitting it and rendering a UI should work.
library(shiny)
library(knitr)
ui <- shinyUI(
fluidPage(
uiOutput('markdown')
)
)
server <- function(input, output) {
output$markdown <- renderUI({
HTML(markdown::markdownToHTML(knit('RMarkdownFile.rmd', quiet = TRUE)))
})
}
shinyApp(ui, server)
这篇关于RMarkdown在闪亮的应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!