R 为DT中的单个列渲染下拉列表并使表格可编辑
我的目标是在datatable的列中有一个下拉菜单,并使该表对其他列也可编辑R 为DT中的单个列渲染下拉列表并使表格可编辑,r,shiny,dropdown,R,Shiny,Dropdown,我的目标是在datatable的列中有一个下拉菜单,并使该表对其他列也可编辑 我为我的第一个需求找到了解决方案,可以通过放置editable=TRUE来编辑数据表。 我可以单独做这两件事,没有任何问题,但我不能同时做这两件事 library(shiny) library(DT) ui <- fluidPage( title = 'Selectinput column in a table', h3("Source:", tags$a("Yihui Xie", href = "ht
我为我的第一个需求找到了解决方案,可以通过放置
editable=TRUE
来编辑数据表。
我可以单独做这两件事,没有任何问题,但我不能同时做这两件事
library(shiny)
library(DT)
ui <- fluidPage(
title = 'Selectinput column in a table',
h3("Source:", tags$a("Yihui Xie", href = "https://yihui.shinyapps.io/DT-radio/")),
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
)
server <- function(input, output, session) {
data <- head(iris, 5)
# adding column having dropdown items
for (i in 1:nrow(data)) {
data$species_selector[i] <- as.character(selectInput(paste0("sel", i), "", choices = unique(iris$Species), width = "100px"))
}
## output for data table
output$foo = DT::renderDataTable(
data,
escape = F,
editable = T,
selection = 'none',
server = F, # we need it to be TRUE.
options = list(dom = 't', paging = FALSE, ordering = 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());")
,rownames = F
)
# saving the proxy of table
proxy = dataTableProxy('foo')
text <- reactive({
# browser()
(sapply(1:nrow(data), function(i) input[[paste0("sel", i)]]))
})
output$sel = renderPrint({
# data$species_selector[1]
text()
})
# this chunk is for update the table for changes done in ui. "This is a must have"
observeEvent(input$foo_cell_edit, {
info = input$foo_cell_edit
str(info)
i = info$row
j = info$col + 1 # column index offset by 1
v = info$value
data[i, j] <<- DT::coerceValue(v, data[i, j])
# replacing the data in the proxy
replaceData(proxy, data, resetPaging = FALSE, rownames = FALSE)
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(DT)
用户界面