在R Shiny App中显示具有工作子链接的HTML文件

问题描述 投票:0回答:1

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

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

非常感谢您的帮助。

谢谢,乔纳森

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()})
})
html r shiny shinydashboard
1个回答
0
投票

您可以使用iframe。这需要在a标记中设置target = "_self"。 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>

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>
© www.soinside.com 2019 - 2024. All rights reserved.