2016-09-10 4 views
0

1つのデータフレームを操作する4つのアクションボタンでShiny Appを構築しようとしています。私は、例えば、最初の行を削除することをクリックして、データフレーム上で自分のアクションボタンを連続して動作させようとしました。 49回、私はcarsの最後の行を得ることができます。私はそのためui.Rを書いた:R Shinyの複数のアクションボタンを使ってデータフレームを連続して操作する方法は?

#ui.R 
shinyUI(pageWithSidebar(
    headerPanel("actionButton test"), 
    sidebarPanel(
    actionButton("delete_top_1", "Delete The First Row"), 
    p("Click the button to delete the first row"), 
    #cars[-1, ] 
    br(), 
    actionButton("delete_last_1", "Delete the Last Row"), 
    p("Click the button to delete the last row"), 
    #cars[-nrow(cars), ] 
    br(), 
    actionButton("delete_top_2", "Delete the Top 2 Rows"), 
    p("Click the button to delete the top 2 rows"), 
    #cars[-1:-2, ] 
    br(), 
    actionButton("delete_last_2", "Delete the Last 2 Rows"), 
    p("Click the button to delete the last 2 rows") 
    #cars[-nrow(cars):-(nrow(cars)-1), ] 
), 
    mainPanel(
    tableOutput('view') 
) 
)) 

は、しかし、私はserver.R部分にこだわっています、私はcarsを交換するreactiveValue()を使用して考えています。誰かが私のサーバー上のサンプルコードを与えることができます.Rの部分?

答えて

1

reactiveValues()を使用すると、実際に状態を管理するための正しい選択です。

私はプレゼンテーションの目的でmtcarsデータセットを使用しました。

library(shiny) 
library(datasets) 

ui <- shinyUI(pageWithSidebar(
    headerPanel("actionButton test"), 
    sidebarPanel(
    actionButton("delete_top_1", "Delete The First Row"), 
    p("Click the button to delete the first row"), 
    #cars[-1, ] 
    br(), 
    actionButton("delete_last_1", "Delete the Last Row"), 
    p("Click the button to delete the last row"), 
    #cars[-nrow(cars), ] 
    br(), 
    actionButton("delete_top_2", "Delete the Top 2 Rows"), 
    p("Click the button to delete the top 2 rows"), 
    #cars[-1:-2, ] 
    br(), 
    actionButton("delete_last_2", "Delete the Last 2 Rows"), 
    p("Click the button to delete the last 2 rows") 
    #cars[-nrow(cars):-(nrow(cars)-1), ] 
), 
    mainPanel(
    tableOutput('view') 
) 
)) 

server <- function(input, output, session){ 
    vals <- reactiveValues(data = mtcars) # Initialize vals$data 

    observeEvent(input$delete_top_1, vals$data <- vals$data[-1, ]) 
    observeEvent(input$delete_top_2, vals$data <- vals$data[-c(1,2), ]) 
    observeEvent(input$delete_last_1, vals$data <- vals$data[-nrow(vals$data), ]) 
    observeEvent(input$delete_last_2, vals$data <- vals$data[-c(nrow(vals$data) - 1, nrow(vals$data)), ]) 

    output$view <- renderTable(vals$data) 

} 

shinyApp(ui, server) 
+0

ワウ!期待どおりに機能しました!どうもありがとうございます、私は 'reactiveValues()'と 'observeEvent()'について読んで時間を割きます! – tonykuoyj

関連する問題