sujith kumar
sujith kumar

Reputation: 21

In shiny How to create a DT table, where i can add rows and delete the rows simultaneously

I have tried this in different ways and achieved one task, either add or delete., but i couldn't get complete solution in one, i might be missing some small concept somewhere.. I am adding the code , please help me complete my basic app.

library(shiny)
library(DT)
x<- data.frame(v1 = NA,
         v2 = NA

),
ui = shinyUI(
 fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput("v1","v1","a"),
      numericInput("V2","V2","1"),
      # Row selection
      numericInput(inputId = "row.selection", label = "Select row to be 
 deleted", min = 1, max = 100, value = "")
      # Add button
      actionButton(inputId = "add.button", label = "Add", icon = 
 icon("plus")), 
      # Delete button 
      actionButton(inputId = "delete.button", label = "Delete", icon = 
 icon("minus")),

    ),
    mainPanel(
      dataTableOutput('table')
    )
  )
 )
),

Server side code

server = function(input, output, session) {
  values <- reactiveValues()
  values$df <- x

  newEntry <- observe({
    cat("newEntry\n")
    if(input$add.button > 0) {
      newRow <- data.frame(input$v1, input$v2)
      isolate(values$df <- rbind(values$df,newRow))
   }
  })

  deleteEntry <- observe({
   cat("deleteEntry\n")
   if(input$delete.button > 0) {
     if(is.na(isolate(input$row.selection))){
       values$df <- isolate(values$df[-nrow(values$df), ])
     } else {
       values$df <- isolate(values$df[-input$row.selection, ])
     }
   } 

  })

  output$table = renderDataTable({
    values$df 

  })

}

Upvotes: 1

Views: 2041

Answers (1)

Jim Chen
Jim Chen

Reputation: 3729

Try to use observeEvent rather than obser with actionbutton and also, you have uppercase and lowercase issue with input$v2 (should be input$V2) Try this modified code:

library(shiny)
library(DT)
x<- data.frame(v1 = NA,
               v2 = NA

)
ui = shinyUI(
  fluidPage(
    sidebarLayout(
      sidebarPanel(
        textInput("v1","v1","a"),
        numericInput("V2","V2","1"),
        # Row selection
        numericInput(inputId = "row.selection", label = "Select row to be 
                     deleted", min = 1, max = 100, value = ""),
        # Add button
        actionButton(inputId = "add.button", label = "Add", icon = 
                       icon("plus")), 
        # Delete button 
        actionButton(inputId = "delete.button", label = "Delete", icon = 
                       icon("minus"))

        ),
      mainPanel(
        dataTableOutput('table')
      )
    )
  )
)
server = function(input, output, session) {
  values <- reactiveValues()
  values$df <- x

  observeEvent(input$add.button,{
    cat("addEntry\n")
    print(input$v1)
    print(input$V2)
    newRow <- data.frame(input$v1, input$V2)
    colnames(newRow)<-colnames(values$df)
    values$df <- rbind(values$df,newRow)
    print(nrow(values$df))
  })

  observeEvent(input$delete.button,{
    cat("deleteEntry\n")
    if(is.na(input$row.selection)){
      values$df <- values$df[-nrow(values$df), ]
    } else {
      values$df <- values$df[-input$row.selection, ]
    }
  })  

  output$table = renderDataTable({
    values$df 
  })

}
shinyApp(ui,server)

Just run all the code above, and it should work properly.

Upvotes: 1

Related Questions