4

I have a saved html widget (from leaflet) that I want to display in a shiny app.

Using includeHTML() works when it is given in ui.R, but not when dynamically called using renderUI(). I have many .html files that I would like a user to choose from with a select input so I need a dynamic solution.

The accepted answer from this popular post Display HTML file in Shiny App has a dynamic solution, but it does not work for me. No error is given, and nothing is rendered.

In summary:

Works

ui <- fluidPage(
  includeHTML("foo.html")
)

Does Not Work

ui <- fluidPage(
  htmlOutput("map")
)

server <- function(input,output){
  output$map <- renderUI({
     includeHTML(path = "foo.html")
   })
}

This post is also related, but no solution was found.

Adam Birenbaum
  • 940
  • 9
  • 23
  • 1
    This is because `foo.html` is a complete HTML file. The function `includeHTML` works for a HTML fragment: the content between `` and ``. But you certainly need the header. I would try an `iframe`. **EDIT**: Ah finally I'm not sure. Because you said the first option works... Strange. – Stéphane Laurent May 09 '19 at 21:38
  • For me also your 2nd option works (using RStudio's rmarkdown example as foo.html). – ismirsehregal May 10 '19 at 06:36
  • Could you link this example? – Adam Birenbaum May 10 '19 at 18:34
  • When using RStudio click the File tab in the upper left corner -> New file -> R Markdown. But I don't think the HTML file will be the problem. Did you check your paths? Is the file located in the www folder or is it available in the current working directory? see `?addResourcePath` – ismirsehregal May 10 '19 at 21:20
  • For anyone facing the same issue - you can solve it by putting dynamic html content in the iframe. This thread mentioned the solution https://github.com/rstudio/shiny/issues/2535 – Yaroslaw Homenko Dec 26 '20 at 07:29

2 Answers2

4

The problem is that knitting an .Rmd file creates an HTML document with <html><head><title><body> etc. while fluidPage() does exactly the same. So including a complete HTML document into fluidPage() creates problems due to redundancy. Fortunately, there's a very easy solution: use output: html_fragment in the YAML header of your .Rmd file before knitting and saving it as .html document.

alstoc
  • 106
  • 2
  • This is brilliant! It solved my problem by just using "output: html_fragment". I spent half a day googling and trying everything with no luck! Thank you! @alstoc. – Steven Ge Jan 10 '23 at 03:26
3

One possible solution: within server.R you need the following:

output$inc <- renderUI(includeHTML("./foo.html"))

please use the relative path. And in ui.R

fluidRow(box(htmlOutput("inc"), width =12))
maniA
  • 1,437
  • 2
  • 21
  • 42