use*_*225 2 html r datatables shiny dt
我搜索解决方案将selectedInputs放在renderDataTable单元格中.我找到了js解决方案:https://datatables.net/examples/api/form.html,但我不知道如何在shinyjs中将此解决方案实现为renderDataTable对象.我将非常感谢提示/想法/解决方案如何在闪亮中实现可编辑的renderDataTable.
与此非常相似:添加一个TRUE/FALSE列并将其显示为复选框
library(shiny)
library(DT)
runApp(list(
ui = basicPage(
h2('The mtcars data'),
DT::dataTableOutput('mytable'),
h2("Selected"),
tableOutput("checked")
),
server = function(input, output) {
# helper function for making checkbox
shinyInput = function(FUN, len, id, ...) {
inputs = character(len)
for (i in seq_len(len)) {
inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
}
inputs
}
# datatable with checkbox
output$mytable = DT::renderDataTable({
data.frame(mtcars,Rating=shinyInput(selectInput,nrow(mtcars),"selecter_",
choices=1:5, width="60px"))
}, selection='none',server = FALSE, escape = FALSE, options = list(
paging=TRUE,
preDrawCallback = JS('function() {
Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() {
Shiny.bindAll(this.api().table().node()); } ')
) )
# helper function for reading checkbox
shinyValue = function(id, len) {
unlist(lapply(seq_len(len), function(i) {
value = input[[paste0(id, i)]]
if (is.null(value)) NA else value
}))
}
# output read checkboxes
output$checked <- renderTable({
data.frame(selected=shinyValue("selecter_",nrow(mtcars)))
})
}
))
Run Code Online (Sandbox Code Playgroud)
请注意,如果您重新呈现表,除非添加一些额外的代码以解除绑定,否则输入将不起作用.
编辑:
假设表中的数据是反应性的,因此它会发生变化,表格会重新呈现.根据@yihui,您需要明确解开绑定:https://groups.google.com/forum/#!msg/shiny-discuss/ZUMBGGl1sss/zfcG9c6MBAAJ
所以你需要在UI中添加:
tags$script(HTML("Shiny.addCustomMessageHandler('unbind-DT', function(id) {
Shiny.unbindAll($('#'+id).find('table').DataTable().table().node());
})"))
Run Code Online (Sandbox Code Playgroud)
然后在服务器中,只要您使用以下内容重新渲染数据表,就会触发该函数:
session$sendCustomMessage('unbind-DT', 'mytable')
Run Code Online (Sandbox Code Playgroud)
colnames参数是列名的向量,因此当您指定一个FALSE向量的长度时,它会为您提供一个表,其中一列名为FALSE.我不确定从数据表中删除列名的简单方法.这本身就是一个很好的问题.
| 归档时间: |
|
| 查看次数: |
2565 次 |
| 最近记录: |