-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide an example of customizing the filter argument of DT::dataTableAjax #194
Comments
I've just started a dtdatasources package to provide different |
The search and filter box is not working in dtdatasources. |
@yihui @grahamrp Can the I'm attempting to use the dtdatasources framework with some minor adjustments that allow me to pass in a lazy query (generated with reactive selectInputs) and then paginate through that result. I've been able to get it to work on a I can add the fully modified Here's my non-reactive example: test_ajax_app <- function() {
# Setup example database and table. Add mtcars rownames as a primary key.
con <- pool::dbPool(
RSQLite::SQLite()
)
onStop(function() {
pool::poolClose(con)
})
dbWriteTable(con, "mtcars", cbind(car = rownames(mtcars), mtcars))
ui <- fluidPage(
DTOutput("tbl"),
verbatimTextOutput("debug")
)
server <- function(input, output, session) {
# Create an initial dataframe. This only needs to contain column names, and
# need not have any rows.
initial_df <- dbGetQuery(con, "SELECT * FROM mtcars LIMIT 0;")
mtcars_query <- dplyr::tbl(con, "mtcars") |>
dplyr::filter(cyl %in% c(6,4))
# Create a funcFilter function describing how to get data for a datatable.
# The filter factory
mtcars_filter <- sql_filter_factory$sql_filter_factory(
# Use the sqlite connection created above
con = con,
query_fun = sqlite$query_sqlite,
query = mtcars_query,
id_field = "car" # Field used to identify a row when using input$tbl_rows_selected
)
output$tbl <- renderDT(
initial_df,
server = TRUE, # Must be TRUE to perform processing in R, not in the browser
rownames = FALSE, # Must be FALSE
funcFilter = mtcars_filter # Provide the sqlite function filter
)
output$debug <- renderPrint({
input$tbl_rows_selected
})
}
shinyApp(ui, server)
} And here's the reactive version that's not working: test_ajax_app <- function() {
# Setup example database and table. Add mtcars rownames as a primary key.
con <- pool::dbPool(
RSQLite::SQLite()
)
onStop(function() {
pool::poolClose(con)
})
dbWriteTable(con, "mtcars", cbind(car = rownames(mtcars), mtcars))
ui <- fluidPage(
selectInput("cyl", "Cylinder", choices = c(4, 6, 8)),
DTOutput("tbl"),
verbatimTextOutput("debug")
)
server <- function(input, output, session) {
# Create an initial dataframe. This only needs to contain column names, and
# need not have any rows.
initial_df <- dbGetQuery(con, "SELECT * FROM mtcars LIMIT 0;")
mtcars_filter <- reactive({
mtcars_query <- dplyr::tbl(con, "mtcars") |>
dplyr::filter(cyl == as.integer(input$cyl))
sql_filter_factory$sql_filter_factory(
# Use the sqlite connection created above
con = con,
query_fun = sqlite$query_sqlite,
query = mtcars_query,
id_field = "car" # Field used to identify a row when using input$tbl_rows_selected
)
})
output$tbl <- renderDT(
initial_df,
server = TRUE, # Must be TRUE to perform processing in R, not in the browser
rownames = FALSE, # Must be FALSE
funcFilter = mtcars_filter() # Provide the reactive filter function
)
output$debug <- renderPrint({
input$tbl_rows_selected
})
}
shinyApp(ui, server)
} |
e.g. using SQL to process the data instead of R
The text was updated successfully, but these errors were encountered: