3

I would like to have a ui element for example a radio button on one tab of a shiny app be "the same" or linked as the ui on a different tab. There potentially be other tabs that do not share that ui element.

For example, I want when I select distance in the plot tab to have it also selected when I go to the summary tab in the code below.

library(markdown)
library(shiny)
ui <- navbarPage("Navbar!",
           tabPanel("Plot", sidebarLayout(sidebarPanel(
              radioButtons("yaxis1", "y-axis", c("speed"="speed", "dist"="dist"),
                                     selected = "speed"
                        )),
                      mainPanel( plotOutput("plot") ))),

           tabPanel("Summary", sidebarLayout(sidebarPanel(
              radioButtons("yaxis2", "y-axis", c("speed"="speed", "dist"="dist")
                        )),
                      mainPanel(verbatimTextOutput("summary"))))
)
# Server ------------------------------------------
server <- function(input, output, session) {
  output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })
  output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })
}
shinyApp(ui, server)

You may run the app with:

shiny::runGist('c8d5131c4e903817316e23e98cf088f9')

The following question links the tabs, but with hyperlinks:

R shiny build links between tabs

It's also kind of the opposite of the following:

How to use the same output binding on different tab panels in shiny

Nate
  • 10,361
  • 3
  • 33
  • 40
student
  • 1,001
  • 2
  • 12
  • 24

2 Answers2

4

You can use the function observe({}) to link your radioButtons. It will create javascript that watches for changes to the input$variables you reference and then performs an action, like updating the other button choice to match.

library(shiny)
# UI ----------------------------------------------------------
ui <- navbarPage("Navbar!",
                 tabPanel("Plot", sidebarLayout(sidebarPanel(
                     radioButtons("yaxis1", "y-axis", c("speed"="speed", "dist"="dist"),
                                  selected = "speed"
                     )),
                     mainPanel( plotOutput("plot"),
                                textOutput("test2")))),  # for input checking

                 tabPanel("Summary", sidebarLayout(sidebarPanel(
                     radioButtons("yaxis2", "grouping-var", c("speed"="speed", "dist"="dist")
                     )),
                     mainPanel(
                               verbatimTextOutput("summary"),
                               textOutput("test1")
                               )))
)
# Server ------------------------------------------
server <- function(input, output, session) {

    observe({
        x <- input$yaxis1
        updateRadioButtons(session, "yaxis2", selected = x)
    })

    observe({
        y <- input$yaxis2
        updateRadioButtons(session, "yaxis1", selected = y)
    })

    output$test1 <- renderPrint({cat("yaxis1", input$yaxis1)})
    output$test2 <- renderPrint({cat("yaxis2", input$yaxis2)})
    output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })
    output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })
}
shinyApp(ui, server)
Nate
  • 10,361
  • 3
  • 33
  • 40
  • 2
    I like your solution. I wonder however is there a way where I can avoid duplicating the code for the radioButtons. – student Nov 12 '17 at 01:33
-1

Putting tabs inside the sidebar layout is one way.

ui <- fluidPage(sidebarLayout(
 sidebarPanel(radioButtons("yaxis1", "y-axis", 
   c("speed"="speed", "dist"="dist"),selected = "speed"
                             )), 
 mainPanel(tabsetPanel(
  tabPanel("Plot", plotOutput("plot")),
  tabPanel("Summary", verbatimTextOutput("summary"))
 )
 )
)
)


    # Server ------------------------------------------
    server <- function(input, output, session) {
      output$plot <- renderPlot({ plot(cars[['speed']], 
        cars[[input$yaxis1]]) })
      output$summary <- renderPrint({ summary(cars[[input$yaxis1]]) 
     })
    }
    shinyApp(ui, server)
    ```
Jai
  • 321
  • 1
  • 8
  • This is not what I'm looking for as I would like to have other tabs that do not use the same ui. – student Nov 11 '17 at 02:16