【问题标题】:Link from HTML text (nested in shinyServer) to specific Shiny tabPanel (in shinyUI)从 HTML 文本(嵌套在 shinyServer 中)链接到特定的 Shiny tabPanel(在 shinyUI 中)
【发布时间】:2019-01-24 07:18:27
【问题描述】:

我正在寻找一种从 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 都会不断变化。

提前致谢。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我不知道这是否可以通过链接实现。但是你可以使用一个按钮和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)
    

    【讨论】:

    • 谢谢斯特凡!!你肯定给我指出了正确的方向。幸运的是,有一个actionLink(每天都是上学日)。我将它与renderUI 结合使用,以将所有内容保留在服务器功能中。我会尽快发布我的解决方案。
    【解决方案2】:

    感谢 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
      })
    })
    

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2016-08-23
      • 2016-07-21
      • 2018-03-02
      • 2021-03-04
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多