简体   繁体   English

根据shinydashboard中选定的选项卡更改元素的值

[英]Changing value of element based on selected tab in shinydashboard

I'm trying to create an app using shinydashboard in which there is a reactive element that changes value based on which tab is selected. 我正在尝试使用shinydashboard创建一个应用程序,其中有一个反应元素,根据选择的选项卡更改值。 Below is the code I've got in an app.R file. 下面是我在app.R文件中获得的代码。 There is an if/else statement currently commented out that I want to be able to use. 有一个if / else语句目前已注释掉我希望能够使用。 The if/else statement would change the value of answer based on which tab is selected. if / else语句将根据选择的选项卡更改answer的值。

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"),
        verbatimTextOutput('out1')
      ),
      tabItem(tabName='Model2',
        h1("Model 2"),
        verbatimTextOutput('out2')
      )
    )
  )
)

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

  answer <- reactive({
    #if(selected tabName=='Model1'){
      answer <- 1
    #} else if(selected tabName=='Model2'){
      answer <- 2
    #}
    return(answer)
  })

  output$out1 <- renderPrint(answer())
  output$out2 <- renderPrint(answer())
}

shinyApp(ui, server)

The solution to this is actually very easy and quite elegant. 对此的解决方案实际上非常简单而且非常优雅。 You have to give sidebarMenu an ID , say, tab and input$tab is going to report which tab is selected. 你必须给sidebarMenu一个ID ,比方说, tabinput$tab将报告选择了哪个选项卡。

So, your if-else statement is going to look like this: 所以,你的if-else语句看起来像这样:

if (input$tab == 'Model1'){
      answer <- 1
    } else if (input$tab == 'Model2'){
      answer <- 2
    }

Full example: 完整示例:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title='Title'),
  dashboardSidebar(
    sidebarMenu(id = "tab", # added ID 
      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"),
              verbatimTextOutput('out1')
      ),
      tabItem(tabName='Model2',
              h1("Model 2"),
              verbatimTextOutput('out2')
      )
    )
  )
)

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

  observe({
    print(input$tab)
  })

  answer <- reactive({
    if (input$tab == 'Model1'){
      answer <- 1
    } else if (input$tab == 'Model2'){
      answer <- 2
    }
    return(answer)
  })

  output$out1 <- renderPrint(answer())
  output$out2 <- renderPrint(answer())
}

shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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