R Shiny:当行顺序发生变化时,如何根据选定的行选择DataTable页面?

时间:2022-09-17 11:02:59

I would like to highlight a row of a datatable in a Shiny app based on a marker clicked on a leaflet map. To do so I have to change the page and go to page 5 if row 46 is selected for example.

我想基于点击传单地图上的标记突出显示Shiny应用程序中的一行数据表。为此,我必须更改页面,如果选择了第46行,则转到第5页。

It works fine if I do not change the rows order.

如果我不更改行顺序,它工作正常。

R Shiny:当行顺序发生变化时,如何根据选定的行选择DataTable页面?

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

如果我重新排序行(例如在升序值上),我找不到转到与所选行对应的页面的方法。

I have ckecked the dataTableProxy manual but it has not helped.

我已经破解了dataTableProxy手册,但它没有帮助。

Any help would be much appreciated.

任何帮助将非常感激。

A code example you can run:

您可以运行的代码示例:

ui.R

ui.R

library(shiny)
library(leaflet)
library(DT)

shinyUI(fluidPage(
  mainPanel(
    leafletOutput("Map"),
    dataTableOutput("Table")
  )
))

server.R

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

1 个解决方案

#1


5  

It seems that for the rows you should filter for the ref number and the page number you should calculate with the help of the actual row 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.

Upvote,因为这个问题非常有趣。

Edit: Credits to NicE for the hint for the dynamic table length variable, see the comments.

编辑:为NicE提供动态表长度变量的提示,请参阅注释。

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

#1


5  

It seems that for the rows you should filter for the ref number and the page number you should calculate with the help of the actual row 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.

Upvote,因为这个问题非常有趣。

Edit: Credits to NicE for the hint for the dynamic table length variable, see the comments.

编辑:为NicE提供动态表长度变量的提示,请参阅注释。

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