Jdv
Jdv

Reputation: 329

Remove selected rows in datatable and upload plot_click event accordingly in shiny app

I have a small shiny app where the user clicks on a plot to draw a point. The x/y coordinates of the point are detected by plot_click and this results in a row added to a table with the coordinates.

I'd like to add a button that let's users remove rows they select. If rows are removed then the plot must also be updated, meaning that the point that corresponds to the deleted row must also be removed from the plot.

I have a minimal example of the app here

library(shiny)
library(tidyverse)
library(DT)


#UI
ui <- basicPage(
  

    column(width = 3, plotOutput("plot", click = "plot_click", width = "350px", height="700px")),
    
    column(width = 9, DTOutput("table"))
  
)
  

#server
server <- function(input, output) {
  
  #click inputs
  val <- reactiveValues(clickx = numeric(), clicky = numeric(), shape= 2)
  table <- reactive(
    
    data.frame(`Location X` = round(val$clickx,2), 
               `Location Y` = round(val$clicky,2))
  )
  
  
  
  #bind clicks
  observeEvent(input$plot_click, {
  
    
    val$clickx = c(val$clickx, input$plot_click$x)
    val$clicky = c(val$clicky, input$plot_click$y)
    
  }) 
  
  
  #interactive plot
  output$plot <- renderPlot({
    
    par(bg = 'red')
    plot(c(-25, 25), c(-50, 50), type = "n", axes = T , ylab = "", xlab = "")
    points(val$clickx, val$clicky, cex = 2)
    
    
  })
  
  #table
  output$table <- renderDT({
    
    
    datatable(table() %>%
                mutate(ID = row_number()) %>%
                arrange(desc(ID)) %>%
                select(ID, everything()),
                rownames= F)
    
  })
  
}

shinyApp(ui, server)

I found ways to add a button to remove selected rows but I am having a hard time getting the plot to update as well.

Upvotes: 1

Views: 230

Answers (1)

lz100
lz100

Reputation: 7340

add a button on UI and add this to server

# remove btn
observeEvent(input$remove, {
    req(input$mytable_rows_selected)
    val$clickx <-  val$clickx[-input$mytable_rows_selected]
    val$clicky <-  val$clicky[-input$mytable_rows_selected]
})

like this

library(shiny)
library(tidyverse)
library(DT)


#UI
ui <- basicPage(
    column(width = 3, plotOutput("plot", click = "plot_click", width = "350px", height="700px")),
    column(width = 9, DTOutput("mytable")),
    actionButton("remove", "remove")
    
)


#server
server <- function(input, output) {
    
    #click inputs
    val <- reactiveValues(clickx = numeric(), clicky = numeric(), shape= 2)
    mytable <- reactive(
        data.frame(`Location X` = round(val$clickx,2), 
                   `Location Y` = round(val$clicky,2))
    )
    #bind clicks
    observeEvent(input$plot_click, {
        val$clickx = c(val$clickx, input$plot_click$x)
        val$clicky = c(val$clicky, input$plot_click$y)
    }) 
    #interactive plot
    output$plot <- renderPlot({
        par(bg = 'red')
        plot(c(-25, 25), c(-50, 50), type = "n", axes = T , ylab = "", xlab = "")
        points(val$clickx, val$clicky, cex = 2)
    })
    
    #mytable
    output$mytable <- renderDT({
        datatable(mytable() %>%
                      mutate(ID = row_number()) %>%
                      arrange(desc(ID)) %>%
                      select(ID, everything()),
                  rownames= F)
    })
    # remove btn
    observeEvent(input$remove, {
        req(input$mytable_rows_selected)
        val$clickx <-  val$clickx[-input$mytable_rows_selected]
        val$clicky <-  val$clicky[-input$mytable_rows_selected]
    })
    
}

shinyApp(ui, server)

enter image description here

Upvotes: 2

Related Questions