2016-09-27 51 views
1

即時通訊工作在一個閃亮的應用程序。 我在做什麼:我想要將兩個不同動作按鈕的激活順序按順序記錄到一個向量中。例如,如果button1(紅色)被按下兩次,然後button2(黑色)被按下一次,然後button1(紅色)被再次按下=>該向量應該看起來像:col = c(「red」,「紅」, 「黑」, 「紅」)。R Shiny:用兩個動作按鈕定義矢量

我做了到目前爲止,這是(在許多不同的變化):

col <- NULL observeEvent(input$rot, {col<-c(col,"red")}) observeEvent(input$schwarz, {col<-c(col,"black")}

輸入$腐UND輸入$施瓦茨的動作按鈕的名稱。所以我認爲每次按下其中一個按鈕時,應該將相應的顏色添加到矢量中。但是:什麼也沒有發生...... 當然我在應用程序中粘貼矢量列,這絕對不是問題。

如果有人有想法,這將是偉大的。 感謝

回答

2

您可以使用reactiveValues存儲在服務器端的載體col

geschichte <- reactiveValues(col = NULL) 

可以反應環境中通過geschichte$col或通過geschichte[["col"]]然後訪問它,並簡單地覆蓋彷彿geschichte是一個普通清單:

observeEvent(input$rot, { 
    geschichte$col <- c(geschichte$col, "red") 
    }) 

完整的示例:

library(shiny) 
rm(ui) ; rm(server) 

ui <- fluidPage(
    actionButton("rot", "Red"), 
    actionButton("schwarz", "Black"), 
    br(), 
    textOutput("col") 
) 

server <- function(input, output) { 

    geschichte <- reactiveValues(col = NULL) 

    observeEvent(input$rot, { 
    geschichte$col <- c(geschichte$col, "red") 
    }) 

    observeEvent(input$schwarz, { 
    geschichte$col <- c(geschichte$col, "black") 
    }) 

    output$col <- renderText({ 
    geschichte$col 
    }) 
} 

shinyApp(ui, server) 

enter image description here

+1

完美!非常感謝。我也試圖用反應式做到這一點,但我顯然沒有線索,花了數小時。謝謝 –