删除数据表中的选定行并在闪亮的应用程序中相应地上传 plot_click 事件

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

我有一个闪亮的小应用程序,用户可以在其中点击绘图来绘制一个点。该点的 x/y 坐标由 plot_click 检测到,这导致将一行添加到具有坐标的 table。

我想添加一个按钮,让用户可以删除他们 select 的行。如果删除行,则还必须更新绘图,这意味着对应于已删除行的点也必须从绘图中删除。

我这里有一个应用程序的最小示例

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)

我找到了添加按钮以删除 selected 行的方法,但我也很难更新绘图。

在 UI 上添加一个按钮并将其添加到服务器

# 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]
})

像这样

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)