r语言 - 从HTML文本(嵌套在shinyServer中)到特定的Shiny tabPanel(在shinyUI中)的链接



我正在寻找一种从HTML文本(嵌套在服务器部分中(链接到特定Shiny tabPanel(嵌套在UI中(的方法。假设我们有以下应用程序:

library(shiny)
shinyUI(fluidPage(
sidebarLayout(
mainPanel(
tabsetPanel(
type="tabs",
tabPanel("Contents", htmlOutput("contents")),
tabPanel("Plot", plotOutput("plot")) # <- A link to here
)
)
)
))
shinyServer(function(input, output) {
output$contents <- renderText({
HTML("A link to <a href='#Plot'>Plot</a>") # <- from there
})
output$plot({
some ggplot
})
})

如何在文本中创建链接,然后重定向到某个选项卡。我尝试了锚标签,但它们似乎不起作用,因为每次启动应用程序时 id 都会不断变化。

提前谢谢。

我不知道这是否可以通过链接来实现。但是您可以使用按钮并updateTabsetPanel.

library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(),
mainPanel(
tabsetPanel(
type="tabs",
id = "tabset",
tabPanel("Contents", actionButton("go", "Go to plot")),
tabPanel("Plot", plotOutput("plot")) 
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$go, {
updateTabsetPanel(session, "tabset", "Plot")
})
output$plot <- renderPlot({
ggplot(mtcars, aes(x=cyl, y=disp)) + geom_point()
})
}
shinyApp(ui, server)

感谢Stéphane Laurent为我指明了正确的方向,我设法创建了我想要的解决方案。为了将所有HTML文本保留在服务器功能中,我使用了renderUIactionLink的组合。解决方案现在如下所示:

library(shiny)
shinyUI(fluidPage(
sidebarLayout(
mainPanel(
tabsetPanel(
type="tabs",
id = "tabset", # <- Key element 1
tabPanel("Contents", htmlOutput("contents")),
tabPanel("Plot", plotOutput("plot"))
)
)
)
))
shinyServer(function(input, output, session) {
output$contents <- renderUI({ # <- Key element 2
list(
HTML(<p>Some text..</p>),
actionLink("link", "Link to Plot") # <- Key element 3
)
})
observeEvent(input$link, {updateTabsetPanel(session, "tabset", "Plot")}) # <- Key element 4
output$plot({
some ggplot
})
})

最新更新