1

I want to delete the last row of a table using an action button. I have tried to follow this post Shiny: dynamically add/ remove textInput rows based on index but I don't know how to apply the idea to my particular case.

A minimal reproducible example

library(shiny)

ui <- fluidPage(
  sidebarPanel(numericInput("c1","Example", NA),
           actionButton("update", "Update Table"),
           br(),  br(),
           actionButton("reset", "Clear")
  ),

  mainPanel( tableOutput("example")
  )
)

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

  # stores the current data frame, called by values() and set by 
  values(new_data_table)
  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,{
                 #.... 
 })

  #Print the content of values$df
  output$example <- renderTable({  return(values())  })

}

shinyApp(ui = ui, server = server)

Actually I don't know how to call the last row of my interactive data frame. I have tried something like values() <- values[nrow(values())-1] but it doesn't work. Any suggestion?

EDITED

Following the suggestion below I have modified the deleteEntry function and now it works.

 ##delete last row
   deleteEntry <- observeEvent(input$reset,{
                 values( values()[-nrow(values()),])
 })
msmarchena
  • 103
  • 2
  • 13

1 Answers1

1

To remove the last row of a data.frame as a reactiveVal , use this syntax:

values(values()[-nrow(values()),])
HubertL
  • 19,246
  • 3
  • 32
  • 51
  • What about reactiveValues? Can I use the same syntax? – msmarchena Jun 21 '17 at 20:37
  • with `my <- reactiveValues(values=data.frame(A=1, B=2, C=3))` you would do `my$values <- my$values[-nrow(my$values),]` which may be a bit more readable. – HubertL Jun 21 '17 at 20:40