Search code examples
rshinymarkdown

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


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: enter image description here

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


Solution

  • 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.