7

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?

SNT
  • 1,283
  • 3
  • 32
  • 78
  • It seems to be more common to include shiny code within an .Rmd file. I think [this](https://jjallaire.shinyapps.io/shiny-ggplot2-brushing/) is a good example, and the process is detailed a bit more [here](https://shiny.rstudio.com/articles/rmarkdown.html) and [here](https://bookdown.org/yihui/rmarkdown/shiny-embedded.html). – Felix T. May 15 '19 at 22:22

2 Answers2

14

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)
RK1
  • 2,384
  • 1
  • 19
  • 36
0

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)
B. Tyner
  • 83
  • 5