首页 > 解决方案 > 在 R Shiny App 中显示带有工作子链接的 HTML 文件

问题描述

我尝试在我的 R Shiny Dashboard App 中将现有的 HTML 文件显示为内容 - 与此问题类似。我的 HTML 文件还包含指向其他本地 HTML 文件的链接,我也希望能够点击和关注。

我设置了如下的最小示例。如果我单击main.html中的链接,我希望显示target.html。目前,当我单击main.html中的链接时,出现Not Found错误。

非常感谢任何帮助。

谢谢,乔纳森

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>

目标.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>

用户界面

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")))
            )
        )
    )
)

服务器.R

library(shiny)

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

标签: htmlrshinyshinydashboard

解决方案


您可以使用iframe。这需要target = "_self"a标签中设置。html 文件必须位于www子文件夹中。

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>

目标.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>

推荐阅读