Before starting, I've tried consulting this question, this question and the documentation for dataTableProxy()
.
I'm currently trying to have a basic RShiny application that, upon loading, automatically navigates to a given page and selects a given row(later on I plan to have the application do so based on a GET query string, but am currently using this basic version as a starting point). The table in question has 32 rows and 4 pages. The contrast to the questions I consulted is that they do so through an event trigger, while I want to do so on document loading.
Selecting the row and page is straightforward enough with selectRows()
and selectPage()
. selectRows()
provides the expected result; however, selectPage()
does not seem to work for me, throwing a 'page index out of range' in the browser console. In my example code, I select row 25 and navigate to page index 2. Even trying to select page index 0 throws an 'out of range' error.
I've provided my application code below. I'm wondering if I'm trying to navigate to a page before the table even paginates?
Any input is greatly appreciated.
EDIT: I have a way to both select the row and go to a page, shown below. However, even though I go to page index 2, the contents of page index 0 are still presented. So I have to click to a different page and then back to page index 2 to see my selected row.
~ Callen
library(DT)
library(shiny)
library(shinyjs)
ui <- fluidPage(
tags$head(tags$script(src="datatables.min.js")),
fluidRow(
DT::dataTableOutput("mtcar_table")
)
)
server <- function(input, output, session){
which_page <<- 0
which_row <<- 0
observe({
which_row <<- 25
which_page <<- floor(which_row/10)
output$mtcar_table = DT::renderDataTable({
# I know here I can normalize
# by the table state's rows per page attribute
# but want to just get this base exmaple
# to work first
return(
mtcars
)
},
options = list(
initComplete = JS(
"function(settings, json){",
"$(this.api().table().page(2).draw('page'));",
"console.log(this); return this;}"
)
)
)
# making sure I'm still recording the row
# and page number
print(which_row)
print(which_page)
# manipulating the table here to navigate
# to the right page and row.
# I constantly get "page index out of range"
# even if I put down page {0, 1}. I'm wondering
# if I'm trying to navigate to the page before
# the pagination actually takes place?
dataTableProxy("mtcar_table") %>%
selectRows(which_row)
})
}
shinyApp(ui, server)