英文:
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 |
是否有可能在我的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 <- fluidPage(
tabPanel("How to", uiOutput("README"))
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$README <- renderUI({
HTML(markdown::markdownToHTML('README.md', 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:
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 <- iris[1:3, ]
library(pander)
pandoc.table(dat, style = "rmarkdown")
But now it remains to see whether such a table is correctly rendered on Github. I did not try.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论