I am building a shinydashboard
app and I want to be able to use the same object in multiple tabs. For example, in the code below I've created the radioButtons
data1
and data2
, both of which correspond to the Model1
and Model2
tabs, respectively. However, is there a way to create one radioButton
, named data
, and have it be used in both tabs? Since I want the value for this to remain the same across all tabs, it would save time and coding space to have only one instance.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title='Title'),
dashboardSidebar(
sidebarMenu(
menuItem('Models', tabName='Models',
menuSubItem('Model1', tabName='Model1'),
menuSubItem('Model2', tabName='Model2')
),
tags$head(tags$script(HTML('$(document).ready(function() {$(".treeview-menu").css("display", "block");})')))
)
),
dashboardBody(
tabItems(
tabItem(tabName='Model1',
h1("Model 1"),
radioButtons('data1', 'Select Data',
choices=list("Use Old Data"='old', "Use New Data"='new')
),
verbatimTextOutput('out1')
),
tabItem(tabName='Model2',
h1("Model 2"),
radioButtons('data2', 'Select Data',
choices=list("Use Old Data"='old', "Use New Data"='new')
),
verbatimTextOutput('out2')
)
)
)
)
server <- function(input, output, session) {
output$out1 <- renderPrint(input$data1)
output$out2 <- renderPrint(input$data2)
}
shinyApp(ui, server)