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
params:
  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)
p

And my app code is as follows :

library(shiny)
ui <- fluidPage(
  fluidRow(
    column(3,
           selectInput("v1", "X variable", names(cars))),
    column(3,
           selectInput("v2", "Y variable", names(cars))),
    column(2, 
           actionButton("g_plot", "Generate Plot"))
  )
)

server <- function(input, output){

  observeEvent(input$g_plot, {
    rmarkdown::render(
      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).

insertUI(
  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).