R shiny: How to get an reactive data frame updated each time pressing an actionButton without creating a new reactive data frame?

后端 未结 1 1154
星月不相逢
星月不相逢 2020-12-04 12:38

My shiny app allows user to upload a csv by using fileInput and stored as an reactive object df_data. I then created a numericInput for user to enter a row

相关标签:
1条回答
  • 2020-12-04 13:09

    Below is a working solution. I created a reactiveValues to store the dataframe. When a file is chosen, the dataframe gets populated. When the delete button is pressed, that same dataframe gets a row deleted. The table always outputs whatever that dataframe object is holding. I hope this code can be a good learning material

    runApp(shinyApp(
    ui=(fluidPage(
      titlePanel("amend data frame"),
    
      mainPanel(
        fileInput("file", "Upload file"),
    
        numericInput("Delete", "Delete row:", 1, step = 1),
        actionButton("Go", "Delete!"),
    
        tableOutput("df_data_out")
      )
    )),
    server = (function(input, output) {
      values <- reactiveValues(df_data = NULL)
    
      observeEvent(input$file, {
        values$df_data <- read.csv(input$file$datapath)
      })
    
      observeEvent(input$Go, {
        temp <- values$df_data[-input$Delete, ]
        values$df_data <- temp
    
      })
    
      output$df_data_out <- renderTable(values$df_data)
    })))
    
    0 讨论(0)
提交回复
热议问题