将我所在的数据页面 table 保存到 R shiny 中的变量中

Save what page of a data table I am on into a variable in R shiny

假设我有一个闪亮的应用程序,其中包含一个数据table。

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

data <- as.data.frame(USArrests)
#data<- cbind(state = rownames(data), data)
ui <- fluidPage(
    dataTableOutput("preview")
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$preview<- renderDataTable(
        datatable(data, options = list(searching = T, pageLength = 10, lengthMenu = c(5,10,15, 20), scrollY = "600px", scrollX = T ))
    )
    
}

# Run the application 
shinyApp(ui = ui, server = server)

我希望能够将用户所在的 table 页面保存在名为 x 的变量中。 有什么办法可以做到这一点吗?非常感谢任何帮助!

library(shiny)
library(DT)

data <- USArrests

callback <- c(
  "Shiny.setInputValue('pageNumber', 1);",
  "table.on('init.dt', function(){",
  "  var pageInfo = table.page.info();",
  "  Shiny.setInputValue('numberOfPages', pageInfo.pages);",
  "});",
  "table.on('page.dt', function(){",
  "  var pageInfo = table.page.info();",
  "  Shiny.setInputValue('pageNumber', pageInfo.page + 1);",
  "  Shiny.setInputValue('numberOfPages', pageInfo.pages);",
  "});",
  "table.on('length.dt', function(){",
  "  var pageInfo = table.page.info();",
  "  Shiny.setInputValue('pageNumber', pageInfo.page + 1);",
  "  Shiny.setInputValue('numberOfPages', pageInfo.pages);",
  "});"
  
)


ui <- fluidPage(
  br(),
  wellPanel(
    textOutput("displayPageNumber")
  ),
  br(),
  DTOutput("preview")
)

server <- function(input, output, session){
  
  output[["preview"]] <- renderDT(
    datatable(
      data, 
      callback = JS(callback),
      options = list(
        searching = TRUE, 
        pageLength = 5, 
        lengthMenu = c(5, 10, 15, 20), 
        #scrollY = "600px", 
        scrollX = TRUE)
    )
  )
  
  output[["displayPageNumber"]] <- renderText({
    paste(
      sprintf("You are currenly viewing page %s.", input[["pageNumber"]]),
      sprintf("There are %s pages in the table.", input[["numberOfPages"]])
    )
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)