How can I call a plot from R markdown to shiny interface?

I have a "example.Rmd" file where I am creating a plotly plot. I want to call that plot into my shiny and show the plot in the shiny interface.

My "example.Rms" is like below :

title: "example"
output: html_document
  var1: default
  var2: default

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```{r pressure, echo=FALSE}
p <- plotly::plot_ly(data = cars, x = ~params$var1, y = ~params$var2)

And my app code is as follows :

ui <- fluidPage(
           selectInput("v1", "X variable", names(cars))),
           selectInput("v2", "Y variable", names(cars))),
           actionButton("g_plot", "Generate Plot"))

server <- function(input, output){

  observeEvent(input$g_plot, {
      input = "~/example.Rmd",
      params = list(
        var1 = input$v1,
        var2 = input$v2


shinyApp(ui, server)

By this code I am able to generate the report. But How can I show plot "p" in the shiny app?

I think the best way to do would be by inserting an HTML iframe, which you could do by calling the following inside your observeEvent() (but after the report is generated).

  selector = "g_plot", where = "afterEnd",
  tags$iframe(src = "example.html", width = "100%", height = 400)

(If you don't want the whole report, you could have the report itself call htmlwidgets::saveWidget() to save p to an HTML file, then iframe in that HTML file instead)

You'll also want to place these HTML files in a safe and isolated directory that the app can serve via shiny::addResourcePath() (please don't place these files directory under ~...if you did, the only way to get this working would be to serve up all the files in that directory).

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.