2016-05-31 16 views
0

ここでは、Rの光沢のある条件付きパネルについていくつかの記事を読んでいますが、簡単な例を再現することはできますが、アプリで動作させることはできません。Shinyのタブ内の条件付きメインパネル

メインパネルが異なる2つのタブ(下の例では「可用性」と「廃棄」という名前)を使いたいと考えています。特に、2番目のタブでは、メインパネルを変数入力に条件付にしたいと思います。 input.variable2 == 'Raw_tot_manuf'が2のplot2とplot2bを表示する場合、他の値はplot2のみと何らかのテキストを表示します。最初のタブのメインパネルはもう一度異なるでしょう。

previous posts/solutionsに基づいて、私は次のコードを考え出しましたが、(両方のタブの)メインパネルには何も表示されませんでした(条件文の挿入前に表示されました)。

ui <- 
    navbarPage(
    "My Application", 
    tabsetPanel(id = "tabs", 
       tabPanel(
        "Availability of raw milk", 
        sidebarLayout(sidebarPanel(
        selectInput(
         "variable",label = "Availability of raw milk", 
         list("UK production" = "UK_prod", 
          "Imports" = "Imports", 
          "Total available" = "Total"))), 

        mainPanel(plotOutput("plot_1"))) 
       ), 

       tabPanel("Disposal of raw milk", 
         sidebarLayout(
          sidebarPanel(selectInput(
          "variable2",label = "Disposal of raw milk", 
          list("Liquid milk" = "Raw_liq_milk", 
            "For manufacture" = "Raw_tot_manuf", 
            "Total available" = "Raw_exports", 
            "Stock change & wastage" = "Raw_wastage"))), 

          mainPanel(
          conditionalPanel(condition = "input.tabs =='Disposal of raw milk' & input.variable2=='Raw_tot_manuf'", 
               plotOutput("plot2"),plotOutput("plot2b")), 
          conditionalPanel(condition = "input.tabs =='Disposal of raw milk' & input.variable2!='Raw_tot_manuf'", 
               plotOutput("plot2"),textOutput("Nothing here"))))))) 

server<- 
    function(input, output) { 
    formulaText1 <- reactive({ 
     paste(input$variable,"~Year")}) 

    formulaText2 <- reactive({ 
     paste(input$variable2,"~Year")}) 


    manuf_data<- reactive ({ 
     if (input$variable2=="Raw_tot_manuf") 
     melt(milk_data[,c(1,7:13)],id.vars=c("Year"),variable.name="Product", value.name="million_L")}) 

    output$plot1<- renderPlot({ 
     plot(as.formula(formulaText1()))}) 

    output$plot2 <- renderPlot({ 
     plot(as.formula(formulaText2()))}) 

    output$plot2b <- renderPlot({ 
     p<- ggplot(manuf_data(), aes(x=Year, y=million_L, fill=Product)) + geom_area() 
     print(p)}) 
    } 

答えて

0

あなたは出力と同じこと二回(plotOutput(「plot2」))することはできません...それは以下のコードから、おそらく自明であろうが、これは光沢のあるアプリケーションを構築での私の最初の試みです。その後、それは常に壊れます。余分な条件をプロット関数に入れる必要があります。 fx。

output$plot2b <- renderPlot(if (input$variable2=="Raw_tot_manuf") {plot(as.formula(formulaText2()))} else {}) 
+0

ありがとう、ジェイコブ。 2番目の "plot2"の名前を変更すると、実際に動作しました。 – Flavie

関連する問題