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

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

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

问题

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

  1. library(shiny)
  2. # 定义绘制直方图的应用程序的UI
  3. ui <- fluidPage(
  4. tabPanel("How to", uiOutput("README"))
  5. )
  6. # 定义绘制直方图所需的服务器逻辑
  7. server <- function(input, output) {
  8. output$README <- renderUI({
  9. HTML(markdown::markdownToHTML('README.md', fragment.only = TRUE))
  10. })
  11. }
  12. # 运行应用程序
  13. shinyApp(ui = ui, server = server)

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

  1. |列1|列2|
  2. |--|--|
  3. |a|1|
  4. |b|2|
  5. |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:

  1. library(shiny)
  2. # Define UI for application that draws a histogram
  3. ui &lt;- fluidPage(
  4. tabPanel(&quot;How to&quot;, uiOutput(&quot;README&quot;))
  5. )
  6. # Define server logic required to draw a histogram
  7. server &lt;- function(input, output) {
  8. output$README &lt;- renderUI({
  9. HTML(markdown::markdownToHTML(&#39;README.md&#39;, fragment.only = TRUE))
  10. })
  11. }
  12. # Run the application
  13. 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:

  1. |Column 1|Column 2|
  2. |--|--|
  3. |a|1|
  4. |b|2|
  5. |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

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

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

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

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

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

英文:

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

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

that you can generate with the pander package as follows:

  1. dat &lt;- iris[1:3, ]
  2. library(pander)
  3. 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:

确定