SNT
SNT

Reputation: 1393

Rendering html outputs from r markdown in shiny app

I am trying to use r markdown to generate a HTML document for presentation. Now when I do it using standalone that seems to be working fine. But when I use it in a shiny app that doesnt seem to be working. So far I have used this in UI

includeHTML("mkslides.html")

And in the server used this to render the markdown.

out <- render('mkslides.Rmd')

The markdown seems to be rendered when I see the console while the shiny app loads. But all I see is the HTML file without the css and js required. How can I fix this?

Upvotes: 9

Views: 11638

Answers (2)

B. Tyner
B. Tyner

Reputation: 93

My understanding is the markdown package is based on the (deprecated) Sundown rendering library. In particular, it does not appear to support the grid_tables extension. Here is one solution based on rmarkdown::render instead, to align more closely with what would be rendered by clicking "Knit to HTML" in RStudio Desktop. I only wish that it did not create and read a file on disk.

library(shiny)
library(knitr)

ui <- shinyUI(
  fluidPage(
    uiOutput('markdown')
  )
)

server <- function(input, output) {
  output$markdown <- renderUI({
      withMathJax(HTML(readLines(rmarkdown::render(input = "test_presentation.rmd",
                                                   output_format = rmarkdown::html_fragment(),
                                                   quiet = TRUE
                                                   ))))
   })
}

shinyApp(ui, server)

Upvotes: 0

RK1
RK1

Reputation: 2532

I'm not 100% sure of your objective, so will try address two points above.

  1. Rendering HTML documents in a ShinyApp

This is pretty straightforward, all you need to do is use includeHTML in your UI.R portion of your ShinyApp, no server side component is required.

http://shiny.rstudio.com/gallery/including-html-text-and-markdown-files.html

Note: includeHTML does not render your *.Rmd file.

  1. Rendering a .Rmd file in a ShinyApp

This requires knit and markdownToHTML, see the below thread.

RMarkdown in Shiny Application


Example Pieces of Code

Example .Rmd file

---
title: "An example Knitr/R Markdown document"
output: html_document
---


{r chunk_name, include=FALSE}
x <- rnorm(100)
y <- 2*x + rnorm(100)
cor(x, y)
{r scatterplot, fig.width=8, fig.height=6}
plot(x,y)

Above saved as: test_presentation.Rmd and knit as a test_presentation.html

1. Include the HMTL file in Shiny

library(shiny)

ui <- shinyUI(
  fluidPage(
    includeHTML('test_presentation.html')
  )
)
server <- function(input, output) {
}

shinyApp(ui, server)

2. Render the above *.Rmd file in Shiny

Code taken form: https://stackoverflow.com/a/33500524/5996972

library(shiny)
library(knitr)

ui <- shinyUI(
  fluidPage(
    uiOutput('markdown')
  )
)
server <- function(input, output) {
  output$markdown <- renderUI({
    HTML(markdown::markdownToHTML(knit('test_presentation.rmd', quiet = TRUE)))
  })
}

shinyApp(ui, server)

Upvotes: 16

Related Questions