代码之家  ›  专栏  ›  技术社区  ›  Johnny

从HTML文本(嵌套在shinyServer中)链接到特定的闪亮选项卡面板(在shinyUI中)

  •  0
  • Johnny  · 技术社区  · 8 年前

    我正在寻找一种从HTML文本(嵌套在服务器部件中)链接到特定的闪亮选项卡面板(嵌套在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都在不断变化。

    提前谢谢。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Stéphane Laurent    8 年前

    我不知道这是否可能与链接。但是你可以用按钮 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)
    
        2
  •  0
  •   Johnny    8 年前

    renderUI actionLink . 现在的解决方案如下:

    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
      })
    })
    
    推荐文章