[英]R Shiny DT - edit values in table with reactive
是否可以通過編輯DT :: DataTable來更新被動數據源? 下面的代碼是基於這個代碼的變化,x被激活了。 嘗試在observeEvent中更改x時,問題就開始了。
有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
})
}
)
我不確定我是否理解正確,但也許這個解決方案可能對你有所幫助。 我將您的被動變為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.