Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 464
SNT Avatar asked May 15 '19 21:05

SNT


People also ask

How do I export html from R Markdown?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

What output formats can be generate from R Markdown?

There are two types of output formats in the rmarkdown package: documents, and presentations.

How do I render in R Markdown?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

How do I embed a shiny app in rmarkdown?

There are two ways to do this: Defining the application inline using the shinyApp() function; or. Referring to an external application directory using the shinyAppDir() function.


1 Answers

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)
like image 124
RK1 Avatar answered Sep 28 '22 05:09

RK1