简体   繁体   English

从Shiny App调用时如何在Rmarkdown中呈现表格和数学

[英]How to render table and math in Rmarkdown when called from Shiny App

I have a Rmarkdown file ( info.rmd ) that looks like this: 我有一个Rmarkdown文件( info.rmd ),看起来像这样:

---
title: "Information"
theme: yeti
date: "4/1/2017"
output: html_document
---


## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.


```{r echo = FALSE, results = 'asis'}
library(knitr)
kable(mtcars[1:5, ], caption = "A knitr kable.")
```

## Formulation

Here is where we formulate
$$\sum_{i=1}^n X_i$$

And the ShinyApp that calls Rmarkdown like this: 像这样调用Rmarkdown的ShinyApp:

server.R 服务器

contains this 包含这个

  output$markdown <- renderUI({
    HTML(markdown::markdownToHTML(knit('info.rmd', quiet = TRUE), fragment.only=TRUE))
  })

ui.R 用户界面

contains this: 包含以下内容:

 fluidPage(uiOutput('markdown'))

But how come the table and math generated looks like this? 但是,如何生成表格和数学运算呢?

在此处输入图片说明

What's the right way to do it? 什么是正确的方法?


When run independently outside Shiny the info.rmd produces the table properly: 在Shiny之外独立运行时, info.rmd会正确生成表:

在此处输入图片说明


I tried this in ui.R 我在ui.R尝试过

 includeHTML("info.html")

Which shows the file html correctly, but prevent the plotting and reactivity in other tabPanel() to work. 这可以正确显示html文件,但会阻止其他tabPanel()的绘图和反应性正常工作。


Update 更新资料

Here is the new result after @Nice solution: 这是@Nice解决方案之后的新结果:

在此处输入图片说明

If you use fragment.only , the CSS and JS is not included and the table/equation is not styled. 如果您使用fragment.only ,则不包括CSS和JS,并且表/等式没有样式。

One easy way to do this is to include the full HTML, with the header, in an iframe so it does not interfere with the rest of your app. 一种简单的方法是在iframe中包含带有标头的完整HTML,以免干扰应用程序的其余部分。

output$markdown <- renderUI({
    tags$iframe(src='info.html',width="100%",frameBorder="0",height="1000px")
  })

The info.html file needs to be in the www folder of your app. info.html文件必须位于您应用程序的www文件夹中。 You can adjust the width and height of the iframe by changing the parameters in the tags$iframe . 您可以通过更改tags$iframe的参数来调整iframe的宽度和高度。

You can change the width of the main container in the iframe using CSS. 您可以使用CSS在iframe中更改主容器的宽度。 If you add this to your info.rmd file: 如果将此添加到info.rmd文件中:

```{r results="asis",echo = FALSE}
cat("
<style>
.main-container.container-fluid {
   max-width: 100%;
   padding-left:0px;
}
</style>
")
```

Editing the shiny server part with the following should help: 使用以下内容编辑闪亮的服务器部分应该会有所帮助:

output$markdown <- renderUI({
    markdown::markdownToHTML(knit('info.rmd', quiet = TRUE), fragment.only=TRUE)
    withMathJax(includeHTML("info.html"))
  })

Alternatively you can also do the following: 另外,您还可以执行以下操作:

output$markdown <- renderUI({
    markdown::markdownToHTML(knit('info.rmd', quiet = TRUE), fragment.only=TRUE)
    withMathJax(includeMarkdown("info.md"))
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM