简体   繁体   中英

Shiny r: clear selected rows from data table by click on plot

Using a ShinyR and data table we can create an interactive plot. When a user select a row in the data table it displayed with specific color and shape in the graph. When we un-select the row, point gain normal conditions. Example modified from ( Shiny apps ).

Additionally, we can identify particular points (we are interested in) on the graph (Using nearPoints )

I would like a user to be able un-select the row by clicking on particular point on the graph. Once user clicked the point on the graph it will gain normal appearance.

However, I can't find function to make it work. There is a proxy and selectRows function in new DT library (the DT -package)(but for Mac it is unavailable). Example

Another option would be to write and option javascript code in callback , however my knowledge is limited in that area.

Will be thankful for any comments and suggestions.

UI

library(shiny)
library(DT)

fluidPage(
  title = 'Select Table Rows',
  fluidRow(
    column(6, DT::dataTableOutput('x1')),
    column(6, plotOutput('x2', height = 500,click = "plot_click"),
           verbatimTextOutput("info"))
  )
)

Server

shinyServer(function(input, output, session) {  
  output$x1 = DT::renderDataTable(cars, server = FALSE)
  # highlight selected rows in the scatterplot
  output$x2 = renderPlot({
    s = input$x1_rows_selected
    par(mar = c(4, 4, 1, .1))
    plot(cars)
    if (length(s)) points(cars[s, , drop = FALSE], pch = 19, cex = 2)
  }) 
  output$info <- renderPrint({
    paste("Selected point row.name - ", row.names(nearPoints(cars, input$plot_click, xvar = "speed", yvar = "dist")), sep="")
  }) 
})

With a new version of DT it works perfect with proxy

proxy = dataTableProxy('x1')
observeEvent(input$plot_click, {
  removeRow <- as.numeric(row.names(nearPoints(cars, input$plot_click, xvar = "speed", yvar = "dist")))
  selectRows(proxy, input$x1_rows_selected[!input$x1_rows_selected %in% removeRow])
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM