首页 > 解决方案 > 在闪亮的应用程序中渲染 r markdown 的 html 输出

问题描述

我正在尝试使用 r markdown 生成用于演示的 HTML 文档。现在,当我使用独立运行时,它似乎工作正常。但是当我在一个似乎不起作用的闪亮应用程序中使用它时。到目前为止,我已经在UI

includeHTML("mkslides.html")

并在服务器中使用它来呈现降价。

out <- render('mkslides.Rmd')

当我在闪亮的应用程序加载时看到控制台时,似乎会呈现降价。但我看到的只是 HTML 文件,不需要 css 和 js。我怎样才能解决这个问题?

标签: rshinyr-markdown

解决方案


我不能 100% 确定您的目标,因此将尝试解决上述两点。

  1. 渲染 HTML 文档ShinyApp

这非常简单,您需要做的就是includeHTML在您UI.R的 ShinyApp 部分中使用,不需要服务器端组件。

http://shiny.rstudio.com/gallery/including-html-text-and-markdown-files.html

注意: includeHTML不呈现您的*.Rmd文件。

  1. 在 a 中渲染 .Rmd 文件ShinyApp

这需要knitand markdownToHTML,请参见下面的线程。

闪亮应用程序中的 RMarkdown


示例代码片段

示例 .Rmd 文件

---
title: "An example Knitr/R Markdown document"
output: html_document
---


{r chunk_name, include=FALSE}
x <- rnorm(100)
y <- 2*x + rnorm(100)
cor(x, y)
{r scatterplot, fig.width=8, fig.height=6}
plot(x,y)

上面另存为:test_presentation.Rmdknit作为test_presentation.html

1. 将HMTL文件包含在Shiny

library(shiny)

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

shinyApp(ui, server)

2.将上述*.Rmd文件渲染在Shiny

代码形式:https ://stackoverflow.com/a/33500524/5996972

library(shiny)
library(knitr)

ui <- shinyUI(
  fluidPage(
    uiOutput('markdown')
  )
)
server <- function(input, output) {
  output$markdown <- renderUI({
    HTML(markdown::markdownToHTML(knit('test_presentation.rmd', quiet = TRUE)))
  })
}

shinyApp(ui, server)

推荐阅读