Sri
Sri

Reputation: 1160

Calling Rmakdown document in a shiny application

I have a test.Rmd file that has the following YAML content plus some other code chunks and statements:

---
title: "Rmarkdown test"
runtime: shiny
output: html_document
params:
     product: "abc"
     type: "xyz"
---

I would like to call this from a Shiny application and embed the result of rendering this test.Rmd file in the shiny output

How do I call this from server.R ? I tried output$text<-render::rmarkdown('test.Rmd), with error Error in .subset2(x, "impl")$defineOutput(name, value, label) : Unexpected character output for text But I see that it generated the test.html file in the same location where ui.R and server.R are present.

The ui.R entry I tested is htmlOutput("text") with same error as above.

What is the correct way of calling the .Rmd file and generate the output in the shiny output window and also while calling .Rmd how to over-write the parameters? Any help?

Upvotes: 0

Views: 1232

Answers (2)

mysteRious
mysteRious

Reputation: 4294

I did two steps: 1) Render the Rmd to an HTML file called background.HTML that I stored to the directory containing the Shiny app.R, and 2) includeHTML to load rendered HTML into the tabPanel. Here is Part 1:

---
title: "Background Info"
date: "November 24, 2019"
output:
  html_document:
    theme: cerulean
    toc: no
---
<style>
.main-container {
    max-width: 940px;
    margin-left: 0;
    margin-right: auto;
}
</style>

Then here I put my text that I wanted in the `tabPanel` of the Shiny app

Here is part 2 that I put in the UI section of my Shiny app:

mainPanel(
    tabsetPanel(
        tabPanel("Descriptive Stats", div(tableOutput("descstatstable"), style = "font-size:90%")), 
        tabPanel("Background Info", includeHTML("background.html"))
    )
)

Upvotes: 0

Soroosh
Soroosh

Reputation: 477

Solution 1:

First: Render your rmarkdown using:

rmarkdown::render()

Second: Call the generated html file using:

includeScript()

Solution 2:

HTML(markdownToHTML(text = "_Hello_, \*\*World\*\*!"))

Solution 3:

Here is another way which I separated Sever and ui and added a couple of fancy modifications:

Sever:

// generate your report using test.Rmd
generateReport <- function() {
  out.markdown <- ''
  withProgress(message = 'Generating Report',
               value = 0, {
                 out.markdown <- rmarkdown::render(
                   input = "test.Rmd",
                   output_format = "html_document")

                 setProgress(1)
               })

  read_file(out.markdown)
}

// load generateReport function and
// renderText helps function be called whenever it is needed
shinyServer(function(input, output, session) {

  output$report <- renderText({ HTML(generateReport()) })


// Your rest of code

}

ui.R

// load report in ui
shinyUI(fluidPage(
  titlePanel("MY Summary"),

  sidebarLayout(
     mainPanel(htmlOutput("report"))
  )
))

Upvotes: 1

Related Questions