R Shiny: How to select a DataTable page based on the selected row when changing the order of the rows?

I would like to highlight a data line in a Shiny application based on a marker by clicking on the map flyer. To do this, I must change the page and go to page 5 if line 46 is selected.

It works fine if I don't reorder the lines.

enter image description here

If I reorder the lines (for example, ascending val ), I cannot find a way to go to the page corresponding to the selected line.

I have ckecked the dataTableProxy guide , but that did not help.

Any help would be greatly appreciated.

Example code that you can run:

ui.R

 library(shiny) library(leaflet) library(DT) shinyUI(fluidPage( mainPanel( leafletOutput("Map"), dataTableOutput("Table") ) )) 

server.R

 library(shiny) library(leaflet) library(DT) shinyServer(function(input, output) { nbpts <- 50 center <- c(47,3) ref <- 1:nbpts lat <- rnorm(nbpts, center[1], 2) lng <- rnorm(nbpts, center[2], 2) val <- sin(lat+lng) data <- data.frame(ref, lat, lng,val) output$Table <- renderDataTable({ DT::datatable(data, selection = "single") }) TableProxy <- dataTableProxy("Table") output$Map <- renderLeaflet({ data_map <- leaflet(data) %>% addTiles() %>% addCircleMarkers( lng=~lng, lat=~lat, layerId = ~ref, radius = 4, color = "purple", stroke = FALSE, fillOpacity = 0.5 ) data_map }) observeEvent(input$Map_marker_click, { clickId <- input$Map_marker_click$id dataId <- which(data$ref == clickId) TableProxy %>% selectRows(dataId) %>% selectPage(dataId %/% 10 + 1) }) }) 
+5
source share
1 answer

It seems that for strings you should filter the ref number and page number, which you should calculate using the actual line number.

This should help you:

  observeEvent(input$Map_marker_click, { clickId <- input$Map_marker_click$id dataTableProxy("Table") %>% selectRows(which(data$ref == clickId)) %>% selectPage(which(input$Table_rows_all == clickId) %/% 10 + 1) }) 

Upvote, because the question was quite interesting.

Edit: Credits for NicE for a hint for a dynamic table variable, see Comments.

  output$Table <- renderDataTable({ DT::datatable(data, selection = "single",options=list(stateSave = TRUE)) }) observeEvent(input$Map_marker_click, { clickId <- input$Map_marker_click$id dataTableProxy("Table") %>% selectRows(which(data$ref == clickId)) %>% selectPage(which(input$Table_rows_all == clickId) %/% input$Table_state$length + 1) }) 
+5
source

Source: https://habr.com/ru/post/1267279/


All Articles