[英]R Shiny DT - edit values in table with reactive
Is it possible to update a reactive data source by editing the DT::DataTable? 是否可以通过编辑DT :: DataTable来更新被动数据源? Below code is based on this code with change that x is made reactive.
下面的代码是基于这个代码的变化,x被激活了。 The problem starts when trying to change x in observeEvent.
尝试在observeEvent中更改x时,问题就开始了。
The purpose of having x reactive is that I intend to source it from an external database, then have edits to the DT::DataTable write back to the database so that it stays in sync with what the user sees (I'm fine with doing that - it is not part of the question). 有x反应的目的是我打算从外部数据库中获取它,然后编辑DT :: DataTable写回数据库,以便它与用户看到的内容保持同步(我很好那 - 它不是问题的一部分)。
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DTOutput('x1')
),
server = function(input, output, session) {
x = reactive({
df <- iris
df$Date = Sys.time() + seq_len(nrow(df))
df
})
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
# problem starts here
x()[i, j] <<- isolate(DT::coerceValue(v, x()[i, j]))
replaceData(proxy, x(), resetPaging = FALSE) # important
})
}
)
I am not sure if I understand you correctly, but maybe this solution might help you a bit. 我不确定我是否理解正确,但也许这个解决方案可能对你有所帮助。 I changed your reactive into a reactiveValues object and I removed the replaceData line.
我将您的被动变为reactiveValues对象,并删除了replaceData行。
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DTOutput('x1'),
verbatimTextOutput("print")
),
server = function(input, output, session) {
x = reactiveValues(df = NULL)
observe({
df <- iris
df$Date = Sys.time() + seq_len(nrow(df))
x$df <- df
})
output$x1 = renderDT(x$df, 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
# problem starts here
x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
})
output$print <- renderPrint({
x$df
})
}
)
如果你没有在DT中显示行名,那么你应该在info$col
添加1以获得正确的列,即j = info$col + 1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.