How to adjust included html document inside an R markdown document?

  Kiến thức lập trình

I have a separate plot html document which I want to include in my R markdown document. The plot html is getting rendered as a background of the R markdown output. Is there any way to adjust the styling of the inserted html document so that it renders below other plots?

app.R

library(shiny)
library(ggplot2)
library(dygraphs)

report_path <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", report_path, overwrite = TRUE)

render_report <- function(input, output, params) {
  rmarkdown::render(input,
                    output_file = output,
                    params = params,
                    envir = new.env(parent = globalenv())
  )
}

ui <- fluidPage(
  downloadButton("report", "Generate report"),
  plotOutput("plot1"),
  dygraphOutput("plotDy")
)

server <- function(input, output) {
  
  plot_gg1 <- reactive({
    ggplot(data = iris, aes(x=Sepal.Length, y=Petal.Length))+geom_point()
  })
  
  output$plot1 <- renderPlot({
    plot_gg1()
  })
  
  plot_dy <- reactive({
    dygraph(nhtemp, main = "New Haven Temperatures", ylab = "Temp (F)") 
  })
  
  output$plotDy <- renderDygraph({
    plot_dy()
  })
  
  output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      html_path <- file.path(tempdir(), "report_5.html")
      htmlwidgets::saveWidget(plot_dy(), html_path)
      params <- list(
        plot1 = plot_gg1(),
        plot2 = html_path
      )
      callr::r(
        render_report,
        list(input = report_path, output = file, params = params)
      )
    }
  )
}

shinyApp(ui, server)

report.Rmd

---
title: 'Dynamic report'
output: html_document
params:
  plot1: NA
  plot2: NA
---

{r plotlyout, echo=FALSE, message=FALSE, out.width='100%'}
params$plot1

{r plot, echo=FALSE, out.width='100%'}
# shiny::tags$iframe(src = params$plot2, width = "100%", height = "600", 
#                                   style = "margin-top: 70px;")
shiny::includeHTML(params$plot2)

As in the above code you can also see, I have tried with iframe tag. In this example it is working perfectly but in my actual code, I am getting bellow message and nothing is appearing –

enter image description here

I an not sure why this message is appearing for my actual code, so I was trying with includeHTML().

LEAVE A COMMENT