5

Let's say that I have a shiny app containing a datatable.

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)

I would like to be able to save what page of the table a user is on in a variable called x. Would there be any way to go about doing this? Any help is greatly appreciated!

zx8754
  • 52,746
  • 12
  • 114
  • 209
tfr950
  • 402
  • 2
  • 8
  • 1
    This question might be able to help with what you want: https://stackoverflow.com/q/53370892/8366499 – divibisan Jul 19 '21 at 19:50
  • 1
    This might also be useful: https://laustep.github.io/stlahblog/posts/DTcallbacks.html#select-page-with-a-numeric-input – divibisan Jul 19 '21 at 19:52
  • 2
    This might be relevant: https://datatables.net/reference/api/page.info() – zx8754 Jul 19 '21 at 19:53
  • I have used [`stateSave`](https://datatables.net/reference/option/stateSave) in a standard DataTable - it works well (using the browser's local storage). Not used it in a Shiny app before, hence this comment instead of an answer. Not only does this save the state, it restores it when you next visit the page (and I realize that may be more than you actually want/need, here). – andrewJames Jul 19 '21 at 23:45

1 Answers1

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

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225