R Shiny在多个选项卡上同步过滤器

Den*_*tes 6 r shiny

我构建了一个带有多个选项卡的R Shiny应用程序,它们有一些共同的过滤器.目前,所有过滤器都是独立的,不会跨多个标签同步.因此,当我将selectInput1从值"a"更改为值"b"时,我必须在包含具有相同选项/含义的selectInput2的下一个选项卡上重复此处理.

我想过使过滤器动态化,因此使用R Shiny的服务器端渲染它们.当然,我总是可以使selectInput2等于selectInput1.但是如果用户更改selectInput2而不是selectInput1会怎样?它在逻辑中创建了一种循环.

我花了很长时间找到解决这个问题的方法,不知怎的,我确信我不是第一个遇到这个问题的人.建议或有用的链接将非常有用!

例:

## UI.R
shinyUI(
  dashboardPage("Dashboard",

    # Create tabs
    tabPanel("Start",
             p("This is the frontpage")
    ),
    tabPanel("tab1",
             uiOutput("selectInput1")
    ),
    tabPanel("tab2",
             uiOutput("selectInput2")
    )
  )
)
Run Code Online (Sandbox Code Playgroud)

和:

## Server.R
library(shiny)

shinyServer(function(input, output,session){
  output$selectInput1 <- renderUI({
    selectInput(inputId = "id1", 
                label = "select", 
                choices = c("a","b","c"), 
                selected = "a")
  })

  output$selectInput2 <- renderUI({
    selectInput(inputId = "id2", 
                label = "select", 
                choices = c("a","b","c"), 
                selected = "a")
  })
})
Run Code Online (Sandbox Code Playgroud)

Jor*_*eys 5

我个人会使用单个输入控件来控制不同的选项卡面板。一种方法是在选项卡下包含该单个输入:

shinyApp(
  fluidPage(
    fluidRow(
      tabsetPanel(
        tabPanel("Tab1",
                 verbatimTextOutput("choice1")),
        tabPanel("Tab2",
                 verbatimTextOutput("choice2"))
      )
    ),
    fluidRow(
      selectInput("id1", "Pick something",
                  choices = c("a","b","c"),
                  selected = "a")
    )
  ),
  function(input, output, session){
    output$choice1 <- renderPrint(input$id1)
    output$choice2 <- renderPrint({
      paste("The choice is:", input$id1)
    })
  }
)
Run Code Online (Sandbox Code Playgroud)

或者,当您使用闪亮的仪表板时,您实际上可以在侧边栏中添加该控件,如果需要,也可以在一组选项卡下的自己的行中添加该控件。

我想不出有多个输入自动选择相同内容的理由。除了减慢你的应用程序,我看不到任何好处。但是,如果您坚持,您可以使所选选项成为一个反应值,reactiveVal并使用例如observeEvent()更新该反应值。一个小例子使用shinydashboard

library(shinydashboard)
library(shiny)
ui <- shinyUI(
  dashboardPage(title = "Dashboard",
                dashboardHeader(),
                dashboardSidebar(
                  tabsetPanel(
                  tabPanel("tab1",
                           uiOutput("selectInput1")
                  ),
                  tabPanel("tab2",
                           uiOutput("selectInput2")
                  )
                )),
                dashboardBody(
                  verbatimTextOutput("selected")
                )
  )
)

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

  thechoice <- reactiveVal("a")  
  output$selectInput1 <- renderUI({
    selectInput(inputId = "id1", 
                label = "select", 
                choices = c("a","b","c"), 
                selected = thechoice())
  })
  output$selectInput2 <- renderUI({
    selectInput(inputId = "id2", 
                label = "select", 
                choices = c("a","b","c"), 
                selected = thechoice())
  })

  observeEvent(input$id2,{
    thechoice(input$id2)
  })

  observeEvent(input$id1,{
    thechoice(input$id1)
  })

  output$selected <- renderPrint({
    c(input$id1, input$id2)
  })
})

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)