Skip to content

Preserving scroll client side #1161

@andrewjmc

Description

@andrewjmc

I am having trouble preserving scroll position in unpaged client-side DT in Shiny. In my real application, the data is a reactiveVal created in the global context and shared across users of a multi-user application. Updates can come from a user's own edits or other users.

When coding my app, I was unaware of the dataTableProxy approach.

A minimal example is below. With my approach updating the reactive global data frame, the scroll position is not preserved (clearly I am forcing a refresh of the entire table). The dataTableProxy approach also doesn't work with the client side table, and I get an "invalid JSON response" error.

If, for whatever reason, the out-of-the-box functionality doesn't work for my case, I'd be keen to understand the client-side events I might utilise to cache the current scroll position, and to catch a table redraw and scroll to the stored position.

Grateful for any advice.

library(DT)
library(shiny)
library(magrittr)

ui <- fillPage(
  DTOutput("table", fill=TRUE, height="100%")
)

data <- data.frame(A=paste("Hello", LETTERS), B=paste("Hello", LETTERS), C=paste("Hello", LETTERS))
data %<>% reactiveVal()

server <- function(input, output){
  
  output$table = renderDT(
    data(), server=FALSE, editable=TRUE, options=list(paging=FALSE), height="100%", fillContainer=TRUE
  )

  #proxy <- dataTableProxy("table")
  
  observeEvent(input$table_cell_clicked, ignoreInit=TRUE, ignoreNULL=TRUE, {
    print("clicked")
    local_data <- isolate(data())
    local_data[1,1] <- paste(local_data[1,1], "Hello")
    data(local_data)
    #alternative approach
    #proxy <- replaceData(proxy, local_data)
  })
}

shinyApp(ui, server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions