在R Shiny应用中呈现Markdown (.md)表格是否可能?

huangapple go评论78阅读模式
英文:

Is it possible to render a markdown (.md) table in an R Shiny app?

问题

我想在我的R Shiny应用程序中显示来自Git存储库的README.md文件。我一直在使用markdownToHTML函数来实现这一点:

library(shiny)

# 定义绘制直方图的应用程序的UI
ui <- fluidPage(
  tabPanel("How to", uiOutput("README"))
)

# 定义绘制直方图所需的服务器逻辑
server <- function(input, output) {
  output$README <- renderUI({
    HTML(markdown::markdownToHTML('README.md', fragment.only = TRUE))
  })
}

# 运行应用程序
shinyApp(ui = ui, server = server)

这对于我的README文件的大多数内容都可以正常工作,但当我添加表格时,它在我的应用程序中无法正确呈现。例如,这个表格:

|列1|列2|
|--|--|
|a|1|
|b|2|
|c|3|

在我的远程Git存储库中看起来是这样的(我使用Azure DevOps,但在GitHub、GitLab等上看起来都一样):

列1 列2
a 1
b 2
c 3

但在我的Shiny应用程序中看起来是这样的:在R Shiny应用中呈现Markdown (.md)表格是否可能?

是否有可能在我的R Shiny应用程序中正确呈现来自.md文件的表格格式?

英文:

I would like to display the README.md file from my Git repository in my R Shiny app. I have been doing this using the markdownToHTML function:

library(shiny)

# Define UI for application that draws a histogram
ui &lt;- fluidPage(
  tabPanel(&quot;How to&quot;, uiOutput(&quot;README&quot;))
)

# Define server logic required to draw a histogram
server &lt;- function(input, output) {
  output$README &lt;- renderUI({
    HTML(markdown::markdownToHTML(&#39;README.md&#39;, fragment.only = TRUE))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

This works fine with most of the content of my README file, but when I add a table it doesn't render properly in my app. For example, this table:

|Column 1|Column 2|
|--|--|
|a|1|
|b|2|
|c|3|

looks like this in my remote Git repository (I use Azure DevOps but it would look the same in GitHub, GitLab etc.):

Column 1 Column 2
a 1
b 2
c 3

but in my Shiny app it looks like this:
在R Shiny应用中呈现Markdown (.md)表格是否可能?

Is it possible to render the table formatting from a .md file properly in my R Shiny app?

答案1

得分: 1

我相信你必须使用这样的格式来创建表格:

| 花萼长度 | 花萼宽度 | 花瓣长度 | 花瓣宽度 | 种类    |
|:--------:|:--------:|:--------:|:--------:|:-------:|
|   5.1    |   3.5    |   1.4    |   0.2    | setosa  |
|   4.9    |    3     |   1.4    |   0.2    | setosa  |
|   4.7    |   3.2    |   1.3    |   0.2    | setosa  |

你可以使用pander包生成这样的表格:

dat <- iris[1:3, ]
library(pander)
pandoc.table(dat, style = "rmarkdown")

但现在还需要查看这样的表格在Github上是否能正确渲染。我没有尝试过。

英文:

I believe you have to use such a format for the table:

| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|:------------:|:-----------:|:------------:|:-----------:|:-------:|
|     5.1      |     3.5     |     1.4      |     0.2     | setosa  |
|     4.9      |      3      |     1.4      |     0.2     | setosa  |
|     4.7      |     3.2     |     1.3      |     0.2     | setosa  |

that you can generate with the pander package as follows:

dat &lt;- iris[1:3, ]
library(pander)
pandoc.table(dat, style = &quot;rmarkdown&quot;)

But now it remains to see whether such a table is correctly rendered on Github. I did not try.

huangapple
  • 本文由 发表于 2023年2月6日 17:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定