简体   繁体   中英

How to add the same inputs into two tabItems in shinydashboard?

I am using shinydashboard to create the interface of my shiny App. However I want one input which appear in the two tabMenu. In the example below, I want to textInput i_test appears in menu menu1 and menu2 .

How should I implement it? Thanks for any suggestions.

library(shiny)
library(shinydashboard)

# Side bar boardy
sidebar <- dashboardSidebar(
    sidebarMenu(
        id = 'menu_tabs'
        , menuItem('menu1', tabName = 'menu1')
        , menuItem('menu2', tabName = 'menu2')
        , menuItem('menu3', tabName = 'menu3')
    )
)

# Body board
body <- dashboardBody(
    tabItems(
        tabItem(
            tabName = 'menu1',
            textInput('i_test', 'Test')
        ),
        tabItem(
            tabName = 'menu2'
        )
    )
)

# Shiny UI
ui <- dashboardPage(
    title = 'test',
    dashboardHeader(),
    sidebar,
    body
)

server <- function(input, output, session) {
}

shinyApp(ui, server)

It seems that shiny always renders two distinct elements, even if you try to build the same element a second time.

Thats why i could only come up with a solution that only makes it look like the two text iputs are the same.

Check the Code:

library(shiny)
library(shinydashboard)

# Side bar boardy
sidebar <- dashboardSidebar(
  sidebarMenu(
    id = 'menu_tabs'
    , menuItem('menu1', tabName = 'menu1')
    , menuItem('menu2', tabName = 'menu2')
    , menuItem('menu3', tabName = 'menu3')
  )
)

# Body board
body <- dashboardBody(
  tabItems(
    tabItem(
      tabName = 'menu1',
      textInput('i_test_1', 'Test')
    ),
    tabItem(
      tabName = 'menu2',
      textInput('i_test_2', 'Test')
    ),
    tabItem(
      tabName = 'menu3'
    )
  )
)

# Shiny UI
ui <- dashboardPage(
  title = 'test',
  dashboardHeader(),
  sidebar,
  body
)

server <- function(input, output, session) {

  observe({
    text1 <- input$i_test_1        
    updateTextInput(session, 'i_test_2', value = text1)
  })
  observe({
    text2 <- input$i_test_2        
    updateTextInput(session, 'i_test_1', value = text2)
  })

}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM