2017-06-13 100 views
1

我使用多个选项卡构建了R Shiny应用程序,其中有一些共同的过滤器。现在,所有过滤器都是独立的,不会跨多个选项卡进行同步。因此,当我将selectInput1从值“a”更改为值“b”时,我必须在包含selectInput2的下一个选项卡上以相同的选项/含义重复此处理。R在多个选项卡上闪亮的同步过滤器

我考虑过滤器是动态的,因此使用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") 
    ) 
) 
) 

和:

## 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") 
    }) 
}) 
+0

在侧边栏中添加一个selectInput有什么问题?这就是我要做的事情,因为您希望用户在所有选项卡上进行单一选择。 –

+1

谢谢你的快速回复。在两个选项卡上添加相同的selectInput元素不起作用,因为Shiny需要为每个加载的元素添加不同的ID。当我尝试使用renderUI(Server)和uiOutput(UI)加载两个相同的元素时,它无法加载任何元素。 – Dendrobates

+1

sry,那是'renderUI()'错误。但是,如果你使用侧边栏,你不必包含它两次,就像@JorisMeys说的那样。如果没有,最好给我们举个例子,... – BigDataScientist

回答

0

我个人使用单一的输入控制来控制不同的标签面板。一种方法是包含在你的标签,单输入:

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) 
    }) 
    } 
) 

或者,当你使用一个shinydashboard,你实际上可以在自己的行添加在侧边栏的控制,可能再次一组选项卡下,如果你必须。

我想不出有多个输入自动选择相同的东西的理由。除了减慢你的应用程序,我看不到任何收益。但是,如果您坚持,则使用使选定的选项成为反应值,并使用例如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) 
+0

非常感谢您发布解决方案,同时也提供广泛的解释。 – Dendrobates