Search code examples
htmlrshinydata-analysis

How to include a rendered Rmarkdown HTML as an iframe in shiny


In Shiny, I want to include a rendered Rmarkdown HTML file that changes with a date parameter. The Rmarkdown includes an interactive table created with the DT package. As there is incompatibility between the includeHTML and DT functions, I am including the rendered file using tag$iframe(). Nonetheless, the src argument of the tag$iframe function, needs a URL.

How can I automate the code so that every time the HTML is rendered, it is stored in a public URL that I can include in the src argument?

I have tried the following:

library(shiny)
library(DT)
library(shinyjs)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      titlePanel("Repotería seguimiento PEIPRDCI"),
      selectInput("date", "Seleccione la fecha:", 
                  choices = c("2023-01-01", "2023-02-01", "2023-03-01", 
                              "2023-04-01", "2023-05-01", "2023-06-01",
                              "2023-07-01", "2023-08-01", "2023-09-01",
                              "2023-10-01", "2023-11-01", "2023-12-01")),
      actionButton("generate", "Generar reporte")
    ), 
    mainPanel(
      htmlOutput("report")
    )  
  )
)

server <- function(input, output) {
  
  
  a <-  eventReactive(input$generate, {
    
      rmarkdown::render("Reporte_seguimiento110523.Rmd",
                        params = list(fecha = input$date))
      
      tags$iframe(src='https://ff0b3a3de6574fd08c5eee452a048bf9.app.posit.cloud/file_show?path=%2Fcloud%2Fproject%2FReporte_seguimiento110523.html', height=1600, width=800)

  })
  
  
  
  output$report <- renderUI({
    a()
  })

  
}

shinyApp(ui = ui, server = server)


Here, the link used in the src argument:

tags$iframe(src='https://ff0b3a3de6574fd08c5eee452a048bf9.app.posit.cloud/file_show?path=%2Fcloud%2Fproject%2FReporte_seguimiento110523.html', height=1600, width=800)

is the URL of the Posit Cloud file. However, in order to see the file, the user needs to log in Posit Cloud, which is something I don't want. I want it to be public whether the user has an account or not.

Any ideas?


Solution

  • Here is how I do. Assuming your HTML file is named myfile.html and is in the subfolder myfolder of your app, do:

    tags$iframe(
      src = base64enc::dataURI(file="myfolder/myfile.html", mime="text/html; charset=UTF-8"),
      style="border:0; position:relative; top:0; left:0; right:0; bottom:0; width:100%; height:1800px"
    )