Kevin Tracey
Kevin Tracey

Reputation: 314

How to remove/deleted selected the selected rows and columns in R shiny

I want to remove the selected rows and columns (not empty rows and columns) from the data table in R shiny.

The code "app.R" does, unmerging the selected columns while choosing the column names. I'd like to expand on this by picking the Rows and Columns and deleting the selected rows and columns.

For removing Rows, I have included the action button "deleteRows" and calling the datatable stored in a reactive value 'rv1$data' in the server function, but it gives me the below error.

Note: for removing the Rows, I have gone through this -> Delete row of DT data table in Shiny app and the same code has been included for expending in my current code

Could someone help me fix this issue?

Error

Listening on http://127.0.0.1:6649
Warning: Error in : Can't access reactive value 'data' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?
  54: <Anonymous>
Error : Can't access reactive value 'data' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?

csv data

ID  Type   Range
21  A1 B1   100
22  C1 D1   200
23  E1 F1   300

app.R

library(shiny)

source('splitColumn_stack.R')

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File", accept = ".csv"),
      checkboxInput("header", "Header", TRUE),
      actionButton("Splitcolumn", "SplitColumn"),
      selectInput(inputId='selectcolumn', label='select column', ''),
      actionButton("deleteRows", "Delete Rows")
    ),
    mainPanel(
      tableOutput("contents"),
      dataTableOutput("table1")
    )
  )
)

server <- function(session, input, output) {
  rv <- reactiveValues(data = NULL)
  rv1 <- reactiveValues(data = NULL)
  
  observeEvent(input$file1, {
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    
    validate(need(ext == "csv", "Please upload a csv file"))
    
    rv$data <- read.csv(file$datapath, header = input$header)
    
    updateSelectInput(session, 'selectcolumn', 'select column', names(rv$data))
    
  })
  
  output$contents <- renderTable({
    req(rv$data)
    rv$data
  })
  
  
  observeEvent(input$Splitcolumn, {
    rv$data <- splitColumn(rv$data, input$selectcolumn)
    
  })
  #for removing the selected Rows
  values <- reactiveValues(dfWorking = rv1$data)
  
  observeEvent(input$deleteRows,{
    
    if (!is.null(input$table1_rows_selected)) {
      
      values$dfWorking <- values$dfWorking[-as.numeric(input$table1_rows_selected),]
    }
  })
  
  output$table1 <- renderDataTable({
    values$dfWorking
  })
}
shinyApp(ui, server)

Upvotes: 1

Views: 1149

Answers (1)

YBS
YBS

Reputation: 21287

You were very close. Try this

library(shiny)
library(reshape2)
#source('splitColumn_stack.R')

splitColumn <- function(data, column_name) {
  newColNames <- c("Unmerged_type1", "Unmerged_type2")
  newCols <- colsplit(data[[column_name]], " ", newColNames)
  after_merge <- cbind(data, newCols)
  after_merge[[column_name]] <- NULL
  after_merge
}

### use a_splitme.csv for testing this program

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File", accept = ".csv"),
      checkboxInput("header", "Header", TRUE),
      actionButton("Splitcolumn", "SplitColumn"),
      selectInput(inputId='selectcolumn', label='select column', ''),
      actionButton("deleteRows", "Delete Rows")
    ),
    mainPanel(
      tableOutput("contents"),
      DTOutput("table1")
    )
  )
)

server <- function(session, input, output) {
  rv <- reactiveValues(data = NULL)
  rv1 <- reactiveValues(data = NULL)

  observeEvent(input$file1, {
    file <- input$file1
    ext <- tools::file_ext(file$datapath)

    req(file)

    validate(need(ext == "csv", "Please upload a csv file"))

    rv$data <- read.csv(file$datapath, header = input$header)

    updateSelectInput(session, 'selectcolumn', 'select column', names(rv$data))

  })

  output$contents <- renderTable({
    req(rv$data)
    rv$data
  })

  #for removing the selected Rows
  values <- reactiveValues(dfWorking = NULL)

  observeEvent(input$Splitcolumn, {
    rv1$data <- splitColumn(rv$data, input$selectcolumn)
    values$dfWorking <- rv1$data
  })

  observeEvent(input$deleteRows,{

    if (!is.null(input$table1_rows_selected)) {

      values$dfWorking <- values$dfWorking[-as.numeric(input$table1_rows_selected),]
    }
  })

  output$table1 <- renderDT({
    values$dfWorking
  })
}

shinyApp(ui, server)

output

Upvotes: 2

Related Questions