3

I am trying to embed markdown within a shinydashboard box, to be later deployed on a shiny server. Using the solution here, I have created the following:

ui.R

library(shinydashboard)

dashboardPage(
   dashboardHeader(title = "xxx"),
   dashboardSidebar(),
   dashboardBody(
     column(
       box(
         title = "BoxTest",
         uiOutput('mymarkdown'),
         width = NULL
       ),
       width = 8)
  )
)

server.R

library(shiny)
library(knitr)
shinyServer(function(input, output) {

  output$mymarkdown <- renderUI(HTML(markdown::markdownToHTML(knit("mymarkdown.Rmd", quiet = TRUE))))

})

mymarkdown.Rmd

## R Markdown

Test Test Test

This creates the following:

Compressed

If I switch to a non-markdown implementation, for example:

output$mymarkdown <- renderUI(h4("Test Test Test"))

I get:

Normal

the view I would expect.

Is there any way to avoid this compression of the page, or is there another way to deploy markdown text in a shinydashboard box?

Community
  • 1
  • 1
Chris
  • 6,302
  • 1
  • 27
  • 54

2 Answers2

2

I also used markdown in shiny, but with rmarkdown and includeHTML

library(shinydashboard)
library(shiny)
library(knitr)
library(rmarkdown) 

ui <- dashboardPage(
  dashboardHeader(title = "xxx"),
  dashboardSidebar(),
  dashboardBody(
    column(
      box(
        title = "BoxTest",
         uiOutput('mymarkdown'),
        width = NULL
      ),
      width = 8)
  )
)   
server <- shinyServer(function(input, output) { 
    output$mymarkdown <- renderUI({  
         rmarkdown::render(input = "mymarkdown.Rmd",
                           output_format = html_document(self_contained = TRUE),
                           output_file = 'mymarkdown.html')  
         shiny::includeHTML('mymarkdown.html') 
                            }) 
} ) 
shinyApp(ui, server)
Eduardo Bergel
  • 2,685
  • 1
  • 16
  • 21
  • 1
    Thanks for this - it works well! I actually was just about to answer my own question - I found out that `includeMarkdown('mymarkdown.Rmd')` works and is quite clean (although it does not appear that this is documented well anywhere). If you'd like to edit in your own answer (or don't mind if I do) - I'd happily mark as accepted. – Chris Sep 06 '16 at 23:54
  • Yes, includeMarkdown does work, but it does not knit the rmd file. See http://stackoverflow.com/questions/39171890. So it will work for simple text, but R code will not be executed. – Eduardo Bergel Sep 07 '16 at 00:11
1

markdown::markdownToHTML has an option to create only a HTML fragment. It omits the HTML header and styling. Like this, the Shiny site styling is not corrupted.

output$mymarkdown <- renderUI({  
    k <- knitr::knit(input = "mymarkdown.Rmd", quiet = T)
    HTML(markdown::markdownToHTML(k, fragment.only = T))
})
Matt
  • 11
  • 1