问题描述
我正在尝试构建一个闪亮的应用程序,该应用程序通过 DT
包在 data.table
中使用动态创建的输入.在下面的可复制示例中,在DT输出中创建的闪亮输入 input $ Sel_Group_1
取决于闪亮输入 input $ selectGroup
中选择的值(值a或b).然后在文本输出 output $ selectedItem
中显示所选项目(值c,d,f,g).
I am trying to build a shiny app that uses dynamically created inputs within a data.table
with the help of the DT
package. In the reproducible example below, the shiny-input input$Sel_Group_1
created within the DT-output depends on the value chosen in the shiny-input input$selectGroup
(values a or b). The selected item (values c,d,f,g) are then shown in the textoutput output$selectedItem
.
最初启动应用程序时,一切正常,但是一旦更新 input $ selectGroup
中的值,元素 input $ Sel_Group_1
的绑定就会丢失,并且textoutput output $ selectedItem
不再反应.在检查元素 input $ Sel_Group_1
时,我发现类属性"shiny-bound-input"一旦 input $ selectGroup
中的值更新,它就会丢失.
When I initially launch the app everything works fine, but as soon as I update the value in input$selectGroup
the binding of the element input$Sel_Group_1
is lost and the textoutput output$selectedItem
does not react any more. Upon inspecting the element input$Sel_Group_1
I found out that the class attribute "shiny-bound-input" is lost as soon as the value in input$selectGroup
is updated.
在此先感谢您的帮助!
library(shiny)
library(DT)
library(data.table)
data <- data.table(Group = c("a", "a", "b", "b"), Item = c("c", "d", "f", "g"))
ui <- fluidPage(
selectInput("selectGroup", "Select Group", c("a", "b")),
DT::dataTableOutput('ItemSelection'),
textOutput("selectedItem")
)
server <- function(input, output, session) {
output$ItemSelection <- DT::renderDT(
{
data_selected <- data[Group == input$selectGroup]
data_unique <- unique(data[Group == input$selectGroup, .(Group)])
data_unique$Item_Selection[1] <-
as.character(selectInput(
"Sel_Group_1",
label = NULL,
choices = unique(data_selected[Group %in% data_unique[1], Item]),
width = "100px"
))
return(data_unique)
}
, escape = FALSE, selection = 'none', server = FALSE,
options = list(dom = 't', paging = FALSE, ordering = FALSE),
rownames = FALSE,
callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-container');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
output$selectedItem <- renderText(paste0("Item selected is: ", input$Sel_Group_1))
}
shinyApp(ui, server)
推荐答案
您必须在重新呈现表之前取消绑定:
You have to unbind before the table is rerendered:
library(shiny)
library(DT)
library(data.table)
data <- data.table(Group = c("a", "a", "b", "b"), Item = c("c", "d", "f", "g"))
ui <- fluidPage(
tags$head(tags$script(
HTML(
"Shiny.addCustomMessageHandler('unbindDT', function(id) {
var $table = $('#'+id).find('table');
if($table.length > 0){
Shiny.unbindAll($table.DataTable().table().node());
}
})")
)),
selectInput("selectGroup", "Select Group", c("a", "b")),
DT::dataTableOutput('ItemSelection'),
textOutput("selectedItem")
)
server <- function(input, output, session) {
output$ItemSelection <- DT::renderDT(
{
data_selected <- data[Group == input$selectGroup]
data_unique <- unique(data[Group == input$selectGroup, .(Group)])
data_unique$Item_Selection[1] <-
as.character(selectInput(
"Sel_Group_1",
label = NULL,
choices = unique(data_selected[Group %in% data_unique[1], Item]),
width = "100px"
))
datatable(
data_unique, escape = FALSE, selection = 'none',
options = list(
dom = 't',
paging = FALSE,
ordering = FALSE,
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); }')
),
rownames = FALSE
)
}, server = FALSE)
observeEvent(input$selectGroup, {
session$sendCustomMessage("unbindDT", "ItemSelection")
})
output$selectedItem <- renderText(paste0("Item selected is: ", input$Sel_Group_1))
}
shinyApp(ui, server)
这篇关于带有DT和data.table的R Shiny中丢失了闪亮绑定输入类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!