在模块内和更改选择标准时,如何在Shiny中更新DT数据表 [英] How to update DT datatable in Shiny when within a module and the selection criteria are changed
问题描述
我尝试制作一个闪亮的模块,以使用DT包显示数据帧中的数据。我想使用一个模块来设置标准的DT表选项,例如语言和其他选项。
I try to make a shiny module to present data from dataframes using the DT package. I would like to use a module to have a standard set up of DT-table options like language and others.
我希望用户能够选择DT表的不同子集交互地处理数据,此后便可以将数据视为DT表。子集的选择将在模块外部生成,因为我希望该子集可用于其他用途,例如导出到csv文件。
I want the user to be able to select different subsets of the data interactively and thereafter be able to see the data as a DT-table. The selection of the subset will be generated outside the module because I would like the subset to be available for other uses, for example to be exported to a csv-file.
当我不使用用于制作DT表的模块时,此方法可以按预期工作。当我将代码放入模块中时,在应用启动时会生成一个表。但是,当选择标准被改变,该表不更新。
This works as intended when I don't use a module for making the DT table. When I put the code inside a module, a table is produced when the app starts. But when the selection criteria are changed, the table don't update.
我已经包括示出该问题的应用程式。表1是在不使用闪亮模块的情况下生成的,并且在更改选择时按预期进行更新。表2是使用该模块输出的,选择更改后不会更新。
I have included an app illustrating the problem. Table 1 is generated without using shiny module and updates as expected when the selection changes. Table 2 is output using the module and don't update when the selection is changed.
我正在运行R-studio 1.1.463,R版本3.5.2和DT版本0.5。
I'm running R-studio 1.1.463, R version 3.5.2 and DT version 0.5.
require("DT")
require("shiny")
# module for presenting data using DT
showDTdataUI <- function(id) {
ns <- NS(id)
tagList(
DT::dataTableOutput(ns("table"))
)
}
showDTdata <- function(input, output, session, DTdata) {
output$table <- renderDataTable({
DT::datatable(DTdata)
})
}
# User interface
ui <-
fluidPage(
sidebarLayout(
sidebarPanel(id="DT",
width = 4,
helpText(h4("Select")),
selectInput("selectedSpecies", label = "Species",
choices = c("setosa","versicolor","virginica"),
selected = "versicolor")
),
mainPanel(
h3("Table 1. Presenting selected data from Iris" ),
DT::dataTableOutput("table"),
h5(br("")),
h3("Table 2. Presenting selected data from Iris using shiny module"),
showDTdataUI(id="testDTModule")
)
)
)
# Define server logic ----
server <- function(session, input, output) {
selectedIris <- reactive ( {
selected <- iris[which(iris$Species==input$selectedSpecies),]
selected
})
output$table <- renderDataTable({
DT::datatable(selectedIris())
})
callModule(showDTdata, id="testDTModule", DTdata=selectedIris())
}
# Run the app ----
shinyApp(ui = ui, server = server)
推荐答案
您必须在 showDTdata $中传递无功导体c $ c>:
showDTdata <- function(input, output, session, DTdata) {
output$table <- renderDataTable({
DT::datatable(DTdata()) # not datatable(DTdata)
})
}
callModule(showDTdata, id="testDTModule", DTdata=selectedIris) # not DTdata=selectedIris()
这篇关于在模块内和更改选择标准时,如何在Shiny中更新DT数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!