2017-06-13 5 views
1

いくつかのフィルタを共通に持つ複数のタブを持つR Shinyアプリケーションを作成しました。現在、すべてのフィルタはスタンドアロンであり、複数のタブにまたがって同期しません。したがって、selectInput1を値 "a"から "b"に変更すると、selectInput2が同じオプション/意味を持つ次のタブでこの処理を繰り返す必要があります。R Shinyは複数のタブでフィルタを同期させます

フィルタを動的にすることを考えました。そのため、R Shinyのサーバー側でレンダリングしました。もちろん、私はいつもselectInput2をselectInput1に等しくすることができます。しかし、ユーザーがselectInput1ではなくselectInput2を変更するとどうなりますか?これは、ロジックにループの種類を作成します。

私はこの問題の解決策を見つけるのにかなりの時間を費やしましたが、何とか私はこの問題に遭遇した最初の人ではないと確信しています。提案や役に立つリンクが本当に役立つでしょう!

例:

## 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つだけ追加すると何が問題になりますか?ユーザーがすべてのタブで1つの選択肢を作ってもらいたいので、これをやる方法です。 –

+1

お返事ありがとうございます。両方のタブに同じselectInput要素を追加することは機能しません。なぜなら、Shinyはロードされるすべての要素に対して異なるIDを必要とするからです。私がrenderUI(Server)とuiOutput(UI)を使って同じ要素を2つロードしようとすると、ロードに失敗します。 – Dendrobates

+1

sry、それは 'renderUI()'で間違っていました。それでも、あなたがサイドバーを使用すると、@ JorisMeysのように2回インクルードする必要はありません。そうでない場合は、例をよく見せてください。 – 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

関連する問題