2017-06-27 12 views
0

カウンタ値に応じて異なる出力を表示したい。たとえば、カウンタが3の場合は出力Aが表示されます。カウンタが3未満の場合は表示されず、カウンタの値が3より大きい場合は出力AとCが表示されます。 表示される出力は、 observe()関数に由来します。ここでShiny:カウンタ条件に基づいて結果を表示する方法

が最小の例である

library(shiny) 

ui <- fluidPage(
    sidebarPanel(numericInput("c1","Example", NA), 
      actionButton("update", "Update"), 
      br(), br(), 
      actionButton("reset", "Clear"), 
      br(), br(), 
      uiOutput("displayCounter"), 
      br(),br(), 
      textOutput("displaysum"), 
      br(),br(), 
      textOutput("total_sum") 
), 

    mainPanel(tableOutput("example") 

      ) 
) 

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

values <- reactiveVal(data.frame(A=1, B=2, C=3)) 

    # update values table on button click 
    observeEvent(input$update,{ 

    old_values <- values() 

    A_new <- input$c1 
    B_new <- A_new + 2 
    C_new <- A_new + B_new 

    new_values <- data.frame(A=A_new, B=B_new, C=C_new) 

    # attach the new line to the old data frame here: 
    new_df <- rbind(old_values, new_values) 

    #store the result in values variable 
    values(new_df) 

    #reset the numeric input to NA 
    updateNumericInput(session, "c1", "Example", NA) 

    }) 

#Delete last row 
    deleteEntry <- observeEvent(input$reset,{ 
      values(values()[-nrow(values()),]) 
    }) 

##reactive counter 
    updateCounter <- reactive({ 
    if (is.na(input$update)) {k <- 0} 
    else {k <- (input$update - input$reset)} 
    return(k) 
    }) 

##After 3 interactions stop counter 
    Counterlimit <- reactive({ 
    if(updateCounter() > 3){ return(3) 
    }else{ updateCounter() } 
    }) 

    output$displayCounter <- renderText({ c("Iteractions:", 
Counterlimit()) })  

     # reactive 

     Total <- reactive({ colSums(values()[3]) }) 

#After 3 interactions display sum of column 3 

    observe({ 

    c3_sum <-Total() 
    if (updateCounter()==3) { 
    output$displaysum <- renderText({paste("Partial sum", c3_sum)}) 
    } 
    if (updateCounter()<3){ 
    output$displaysum <- renderText({paste("Partial sum", NULL)}) 
    } 
    if (updateCounter()>3) { 
     output$total_sum <- renderText({paste("Total sum", Total())}) 

    } 

    }) 
# Print table 

    output$example <- renderTable({ return(values()) }) 
} 

shinyApp(ui = ui, server = server) 

私は3未満のカウンタを取得するにはクリアボタンを使用する場合は、総合計は、まだ表示されます。私の問題は条件で結果を表示することなので、私が望むものを表示するために私のUIに条件付きパネルを使用できるかどうかは疑問でした。

observe()関数の出力を条件付きパネルの入力として使用して結果を表示することはできますか?またはカウンター条件に基づいてそれらの出力を表示する他の提案あなたはとても

ui <- fluidPage(
    selectInput("choose", "choose", letters[1:2]), 
    conditionalPanel(
    condition = "output.myCondition == true", 
    actionButton("button", "this button is only visible when a is selected")) 
) 

server <- function(input, output){ 
    output$myCondition <- reactive({input$choose == 'a'}) 
    outputOptions(output, "myCondition", suspendWhenHidden = FALSE) 
} 

shinyApp(ui, server) 

suspendWhenHidden一部のように条件にouputスロットを割り当てる必要があります

+0

- あなたの "出力オブザーバー" にrenderText({NULL})は' –

答えて

0

グレゴールのアイデアに続いて、私は解決策を見直しました。 2つの主な変更点:

まず、カウンタ値を分離する必要があることを認識したので、カウンタの定義方法を変更しました。

第2に、条件をレンダリングテキスト関数に置きます。これで、期待される結果が直接表示されます。

私は元の質問をより正確に修正しました。ここでは、最終的なコードは次のとおりです。ただ、 `場合(updateCounter()> 3)出力の$ total_sum <追加

library(shiny) 

ui <- fluidPage(
    sidebarPanel(numericInput("c1","Example", NA), 
      tags$p(actionButton("update", "Update")), 
      tags$p(actionButton("reset", "Clear")), 
      textOutput("count"), 
      textOutput("display") 
), 

    mainPanel(tableOutput("example") 
      ) 
) 

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

    values <- reactiveVal(data.frame(A=1, B=2, C=3)) 

    # update values table on button click 
    observeEvent(input$update,{ 

    old_values <- values() 

    A_new <- input$c1 
    B_new <- A_new + 2 
    C_new <- A_new + B_new 

    new_values <- data.frame(A=A_new, B=B_new, C=C_new) 

    # attach the new line to the old data frame here: 
    new_df <- rbind(old_values, new_values) 

    #store the result in values variable 
    values(new_df) 

    #reset the numeric input to NA 
    updateNumericInput(session, "c1", "Example", NA) 

    }) 

#Delete last row 
     deleteEntry <- observeEvent(input$reset,{ 
       values(values()[-nrow(values()),]) 
    }) 

##reactive counter modified 
    updateCounter <- reactiveValues(i = 0) 

    output$count <- renderText({ 
    paste0("Iteractions: ", updateCounter$i) 
    }) 

    observe({ 
    input$update 

    isolate({ 
     updateCounter$i <- updateCounter$i + 1 
    }) 
    }) 

    observe({ 
    input$reset 
    isolate(updateCounter$i <- updateCounter$i - 1) 
    }) 

Total <- reactive({ colSums(values()[3]) }) 

     output$display <- renderText({ 
     partialsum <-Total() 
     if(updateCounter$i==3) { 
      value3 <<- partialsum 
      return(paste("Partial result", value3)) 
     } 
     if(updateCounter$i<3){ 
      return() 
     } 
     if(updateCounter$i>3) { 
     display1 <- paste("Partial result", value3) 
     display2 <- paste("Total result", partialsum) 
     paste(display1, display2) 
     } 
     }) 

    output$example <- renderTable({ return(values()) }) 
} 

shinyApp(ui = ui, server = server) 
1

が不可欠です。それはUIに(直接)表示されていなくても、output$myConditionの評価を強制します。

condition引数にはTRUEではなく、trueを使用します。これは、javascriptの構文に従っているためです。


ただし、conditionalPanelを使用する必要はありません。入力に応じて異なるテキストを返すことができます。

shinyApp(
    fluidPage(
    textOutput("display"), 
    actionButton("rerun","rerun") 
), 
    function(input, output, session){ 
    updateCounter = reactive({ 
     input$rerun 
     sample(4:6,1) 
    }) 

    Total = function(){isolate(updateCounter())} 

    output$display <- renderText({ 
     partialsum <-Total() 
     ctr = updateCounter() 
     if(ctr==5) { 
     return(paste("Result", partialsum)) 
     } 
     if(ctr<5){ 
     return(paste("Result", NULL)) 
     } 
     if(ctr>5) { 
     return(paste("Result", partialsum)) 
     } 
    }) 
    } 
) 
+0

の作品、私はどのように把握することはできません私の場合にあなたの提案を適用する。私は自分のやりたいことをより良く説明し、例を含めるために質問を編集しました。ありがとう! – Chene

+0

私は、コードにコメントされていない行があることを認識しました。今それは動作します。あなたの提案は私の場合は実際には機能しません。 – Chene

関連する問題