Home > Software design >  R shiny: choose a row to start displaying datatable
R shiny: choose a row to start displaying datatable

Time:06-16

below there is a shiny app that renders a datatable using DT. Rather than have the table start the display at row 1 I'd like to have the table render with a specific top row selected by the user (using input$startRow in this example).

E.g., if the user chose 50 in the slider the first row shown in the table would be row 50 rather than row 1.

Any tips for getting a dynamic starting row appreciated.

Edit for clarity: I do not want to subset the table, I want to display to begin at input$startRow but the user could scroll up and down and still see the entire dataset (e.g., faithful in this example).

Edit 2: It looks like the issue is that the displayStart option is what I want but that there is a known bug as of May 21 with Scroller starting the display mid table.

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 100,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderDataTable({
    # use input$startRow to begin the table display?
    datatable(faithful,
              extensions = "Scroller", 
              options = list(scrollY = 300,
                             scroller = TRUE))
  })

}

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

CodePudding user response:

Success. Following the link in the comment. I was able to use initComplete to start the table on the row from input$startRow. This appears to work.

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 10,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderDataTable({
    datatable(faithful,
              extensions = "Scroller",
              options = list(scrollY = 300,
                             scroller = TRUE,
                             initComplete  = JS('function() {this.api().table().scroller.toPosition(',
                                                input$startRow-1,');}')))})

}

shinyApp(ui = ui, server = server)

CodePudding user response:

Yes, use input$startRow to begin the table display to generate the selected table.

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 100,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  
  topDF <- reactive({
    # use input$startRow to begin the table display
    topRow <- input$startRow
    
    selectedDf <- faithful[-(1:(topRow-1)), ]
    
    return(selectedDf)
  })
  
  output$table1 <- renderDataTable({

    datatable(topDF(),
              extensions = "Scroller", 
              options = list(scrollY = 300,
                             scroller = TRUE))
  })
  
}

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