1

I try to display an existing HTML file as content in my R Shiny Dashboard App - similar as in this question. My HTML file also contains links to other local HTML files I would like to be able to click and follow as well.

I setup the minimal example as below. If I click the link in main.html, I want target.html to be displayed. Currently, when I click the link in main.html, I get a Not Found error.

Any help is highly appreciated.

Thanks, Jonathan

main.html

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>Head</title></head>
<body><a href="target.html">target</a></body>
</html>

target.html

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>Head</title></head>
<body><a href="main.html">back to main</a></body>
</html>

ui.R

library(shinydashboard)

dashboardPage(
    dashboardHeader(title = "HTML Main"),
    dashboardSidebar(
        sidebarMenu(
            menuItem("Main", tabName = "main")
        )
    ),
    dashboardBody(
        tabItems(
            tabItem(tabName = "main",
                fluidRow(box(width=NULL, htmlOutput("html_main")))
            )
        )
    )
)

server.R

library(shiny)

shinyServer(function(input, output) {
  getPageMain<-function() {
    return(includeHTML("C:/sub_link/main.html"))
  }
  output$html_main<-renderUI({getPageMain()})
})
ThreeStars
  • 11
  • 1

1 Answers1

0

You can use an iframe. This requires to set target = "_self" in the a tags. The html files must be in the www subfolder.

ui <- dashboardPage(
  dashboardHeader(title = "HTML Main"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Main", tabName = "main")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "main",
              tags$iframe(src = "main.html")
      )
    )
  )
)

server <- function(input, output) {}

shinyApp(ui, server)

main.html

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>Head</title>
</head>
<body>
  <a href="target.html" target="_self">target</a>
</body>
</html>

target.html

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>Head</title>
</head>
<body>
  <a href="main.html" target="_self">back to main</a>
</body>
</html>
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225