Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to a tab or panel of a shiny app

How do I manage to link from a given shiny part to parts that are located on other tabs/panels?

Update

The solution I drafted below works for the explicit case of linking to tabs/panels (and that's what I asked for).

However, I'd be interested to also know about more generic ways of linking parts of a shiny app.

Example

I'd like to link from panel A to panel B, but I'm not quite sure what I need to specify as an action when the action link in panel A is clicked.

The value #tab-4527-2 came from investigating the HTML output of ui, but I just saw that those values change each time I restart the app.

library(shiny)

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

ui <- fluidPage(
  tabsetPanel(
    tabPanel(
      "A",
      p(),
      actionLink("link_to_tabpanel_b", "Link to panel B")
    ),
    tabPanel(
      "B",
      h3("Some information"),
      tags$li("Item 1"),
      tags$li("Item 2")
    )
  )
)

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

server <- function(input, output, session) {
  observeEvent(input$link_to_tabpanel_b, {
    tags$a(href = "#tab-4527-2")
  })
}

shinyApp(ui, server)
like image 495
Rappster Avatar asked Dec 16 '15 15:12

Rappster


People also ask

What are the main 2 parts of an R shiny app?

Shiny applications have two components, a user interface object and a server function, that are passed as arguments to the shinyApp function that creates a Shiny app object from this UI/server pair.

Which function is used to create the shiny app?

Finally the shinyApp function creates Shiny app objects from an explicit UI/server pair. Note: Prior to version 0.10. 2, Shiny did not support single-file apps and the ui object and server function needed to be contained in separate scripts called ui. R and server.

How do you enter shiny data?

To add an input in a Shiny app, we need to place an input function *Input() in the ui object. Each input function requires several arguments. The first two are inputId , an id necessary to access the input value, and label which is the text that appears next to the input in the app.


2 Answers

The following solution is based on the inputs I got from the comments.

Note that updateTabsetPanel() belongs to shiny while updateTabItems() is a function of the shinydashboard package. They seem to work interchangeably.

library(shiny)
library(shinydashboard)

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

ui <- fluidPage(
  tabsetPanel(
    id = "panels",
    tabPanel(
      "A",
      p(),
      actionLink("link_to_tabpanel_b", "Link to panel B")
    ),
    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")
#   })
  observeEvent(input$link_to_tabpanel_b, {
    newvalue <- "B"
    updateTabItems(session, "panels", newvalue)
  })
  observeEvent(input$link_to_tabpanel_a, {
    newvalue <- "A"
    updateTabsetPanel(session, "panels", newvalue)
  })
}

shinyApp(ui, server)
like image 135
Rappster Avatar answered Oct 10 '22 06:10

Rappster


You can give your tabsetPanel an id and use updateTabsetPanel with your observeEvent

library(shiny)

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

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

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

server <- function(input, output, session) {
  observeEvent(input$link_to_tabpanel_b, {
    updateTabsetPanel(session, "demo", "B")
  })
}

shinyApp(ui, server)
like image 31
thesadie Avatar answered Oct 10 '22 07:10

thesadie