I have an issue while trying to make datatable reactive within a shiny app (from DT package). Here is my reproducible example:
ui.r
dashboardPage(
dashboardHeader(title = "TEST reactive DT"),
dashboardSidebar(
sidebarMenu(
menuItem("See data", tabName = "db"),
menuItem("Test", tabName = "test")),
radioButtons("rb1", label = "Select data",
choices = list("IRIS" = "iris", "CARS" = "cars"),
selected = "iris")
),
dashboardBody(
tabItems(
tabItem(tabName = "db",
h4("Show selected dataset"),
fluidRow(DT::dataTableOutput('tbl')) #THIS DOES NOT WORK (NOT REACTIVE)
),
tabItem(tabName = "test",
h4("Test tab"),
fluidRow(column(3, verbatimTextOutput("value"))) #THIS WORKS
)
)
)
)
server.r
library(shiny)
library(shinydashboard)
server <- function(input, output, session) {
output$value <- renderPrint({ input$rb1 })
data <- reactive({
switch(input$rb1,
"iris" = iris,
cars)
})
action <- dataTableAjax(session, cars) # HOW SHOULD I SPECIFY? data() INSTEAD OF cars DOES NOT WORK
widget <- datatable(cars, # HOW SHOULD I SPECIFY? data() INSTEAD OF cars DOES NOT WORK
class = 'display cell-border compact',
filter = 'top',
server = TRUE,
options = list(ajax = list(url = action))
)
output$tbl <- DT::renderDataTable(widget)
}
As you can see in the 'Test tab', radiobutton selection is updated at change. However I can't understand how this should be integrated within the dataTableAjax and dataTable functions, can you explain/ help me solve this issue?
Many thanks in advance for your help!
Best regards
Solution found:
ui.R
## ui.R ##
dashboardPage(
dashboardHeader(title = "TEST reactive DT"),
dashboardSidebar(
sidebarMenu(
menuItem("See data", tabName = "db")
),
radioButtons("rb1", label = "Select data",
choices = list("IRIS" = "iris", "CARS" = "cars"),
selected = "iris")
),
dashboardBody(
tabItems(
tabItem(tabName = "db",
h4("Show selected dataset"),
fluidRow(DT::dataTableOutput('tbl2'))
)
)
)
)
server.R
## server.R ##
library(shiny)
library(shinydashboard)
server <- function(input, output, session) {
output$value <- renderPrint({ input$rb1 })
data <- reactive({
switch(input$rb1,
"iris" = iris,
cars)
})
action <- dataTableAjax(session, cars)
widget <- datatable(cars,
class = 'display cell-border compact',
filter = 'top',
server = TRUE,
options = list(ajax = list(url = action))
)
output$tbl2 <- DT::renderDataTable({
DT::datatable(data())
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With