manu p
manu p

Reputation: 985

navigate to next tab post clicking on link

Below is the application. I need to navigate to next tab when clicked on "I clicked" on DT table. But as per below code, that is not working. Can anyone help me........................................................................

library(shiny)
library(shinydashboard)
library(DT)

# UI ---------------------------------------------------------------------

ui <- fluidPage(
  tabsetPanel(
    id = "panels",
    tabPanel(
      "A",
      p(),
      DTOutput("tab")
      # actionLink("link_to_tabpanel_b", "Link to panel B")
      # HTML('<a id="Iclickehase" class="action-button" href="#">I clicked</a>')
    ),
    tabPanel(
      "B",
      h3("Some information"),
      tags$li("Item 1"),
      tags$li("Item 2"),
      actionLink("link_to_tabpanel_a", "Link to panel A")
    )
  )
)

# Server ------------------------------------------------------------------

server <- function(input, output, session) {
  #   observeEvent(input$link_to_tabpanel_b, {
  #     tags$a(href = "#tab-4527-2")
  #   })
  
  df <- data.frame(a = c(1))
  df$b <- HTML('<a id="Iclickehase" class="action-button" href="#">I clicked</a>')
  # 
  output$tab <- renderDataTable({
    datatable(df,escape = F,selection = 'none')
  })
  
  observeEvent(input$Iclickehase, {
    newvalue <- "B"
    updateTabItems(session, "panels", newvalue)
  })
  observeEvent(input$link_to_tabpanel_a, {
    newvalue <- "A"
    updateTabsetPanel(session, "panels", newvalue)
  })
}

shinyApp(ui, server)

Upvotes: 0

Views: 595

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33417

When adding inputs (like the actionLink) to a datatable you need to bind them manually, here is why.

Please check the following:

library(shiny)
library(shinydashboard)
library(DT)

# UI ---------------------------------------------------------------------

ui <- fluidPage(tabsetPanel(
  id = "panels",
  tabPanel("A",
           p(),
           DTOutput("tab")),
  tabPanel("B",
    h3("Some information"),
    tags$li("Item 1"),
    tags$li("Item 2"),
    actionLink("link_to_tabpanel_a", "Link to panel A")
  )
))

# Server ------------------------------------------------------------------

server <- function(input, output, session) {
  
  DF <- data.frame(a = c(1))
  DF$b <- HTML('<a id="Iclickehase" class="action-button" href="#">I clicked</a>')
  
  output$tab <- renderDataTable({
    datatable(
      DF,
      escape = FALSE,
      selection = 'none',
      options = list(
        preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
        drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
      )
    )
  })
  
  observeEvent(input$Iclickehase, {
    newvalue <- "B"
    updateTabItems(session, "panels", newvalue)
  })
  observeEvent(input$link_to_tabpanel_a, {
    newvalue <- "A"
    updateTabsetPanel(session, "panels", newvalue)
  })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions