[英]Shiny + DT: how to make datatable reactive?
嘗試在閃亮的應用程序(來自DT包)中使數據表無效時,我遇到了問題。 這是我可重復的例子:
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)
}
正如您在“測試選項卡”中看到的那樣,radiobutton選擇會在更改時更新。 但是我無法理解這應該如何集成在dataTableAjax和dataTable函數中,你能解釋/幫助我解決這個問題嗎?
非常感謝您的幫助!
最好的祝福
解決方案:
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())
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.