如何在保存所做的编辑时向 DT 包添加行
How to add rows to DT package while saving the edits made
我正在尝试开发一个闪亮的应用程序,允许用户在启用 editable 设置的情况下添加、删除和保存对 DT table 的编辑。但是由于某种原因我无法添加一行。有人知道为什么会这样吗?
我以鸢尾花数据集为例。 (saveRDS(虹膜,"iris.rds"))
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DTOutput('x1'),
actionButton("save", "Save Table"),
actionButton("add_btn","Add Button")
),
server = function(input, output, session) {
x <- readRDS("iris.rds")
output$x1 = renderDT(x, selection = 'none', editable = TRUE)
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE) })
observeEvent(input$add_btn,
{newrow <- setNames(data.frame(matrix(ncol = ncol(x), nrow = 1)),
colnames(x))
x<<-rbind(x,newrow)})
observeEvent(input$save,
{saveRDS(x, "iris.rds")
})
}
)
您正在使用 values$data
一个未知的 data.frame 而不是使用 x
,并且您没有使用 replaceData 函数更新 DT
输出:
observeEvent(input$add_btn,
{ newrow <- setNames(data.frame(matrix(ncol = ncol(x), nrow = 1)),
colnames(x))
x<<-rbind(x,newrow)
replaceData(proxy, x, resetPaging = F)
})
我正在尝试开发一个闪亮的应用程序,允许用户在启用 editable 设置的情况下添加、删除和保存对 DT table 的编辑。但是由于某种原因我无法添加一行。有人知道为什么会这样吗?
我以鸢尾花数据集为例。 (saveRDS(虹膜,"iris.rds"))
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DTOutput('x1'),
actionButton("save", "Save Table"),
actionButton("add_btn","Add Button")
),
server = function(input, output, session) {
x <- readRDS("iris.rds")
output$x1 = renderDT(x, selection = 'none', editable = TRUE)
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE) })
observeEvent(input$add_btn,
{newrow <- setNames(data.frame(matrix(ncol = ncol(x), nrow = 1)),
colnames(x))
x<<-rbind(x,newrow)})
observeEvent(input$save,
{saveRDS(x, "iris.rds")
})
}
)
您正在使用 values$data
一个未知的 data.frame 而不是使用 x
,并且您没有使用 replaceData 函数更新 DT
输出:
observeEvent(input$add_btn,
{ newrow <- setNames(data.frame(matrix(ncol = ncol(x), nrow = 1)),
colnames(x))
x<<-rbind(x,newrow)
replaceData(proxy, x, resetPaging = F)
})