簡體   English   中英

如何放棄用戶對 DT::datatable 的編輯 Shiny R

[英]How to discard user edits on DT::datatable in Shiny R

我使用 DT::renderDataTable 為 Shiny 應用程序呈現 data.table,並在datatable()的定義中使用editable = TRUE datatabledata參數是一個反應式表達式,它根據從下拉菜單中選擇的值從數據庫中獲取數據。 我希望能夠有一個按鈕來刷新數據表並放棄任何用戶編輯,就像從下拉菜單中選擇另一個值一樣。 我無法為您提供可重現的示例,因為我使用的是專有代碼和來自數據庫的數據。 我知道我可以在刷新按鈕單擊事件上使用observeEvent ,然后調用表的渲染 function,但我正在尋找一種更優雅的方法。 謝謝

發現您可以使用proxy <- datatableProxy() ,然后在刷新按鈕的watchEvent內部使用reloadData(proxy) 這將取消對數據表所做的任何編輯。

謝謝@Stelios!

這是改編自https://github.com/rstudio/DT/issues/357#issuecomment-247423310的代表

library(shiny)
shinyApp(
  ui = fluidPage(
    fluidRow(
      column(2, actionButton('refresh', 'Refresh Data', icon = icon('refresh'))),
      column(10, DT::dataTableOutput('foo'))
    )
  ),
  server = function(input, output, session) {

    output$foo = DT::renderDataTable(
      iris, 
      selection = 'none', 
      editable = 'all',
      caption = 'Double-click to edit cells. Press Ctrl+Enter to save changes.'
    )
    
    proxy = dataTableProxy('foo')
    
    observeEvent(input$refresh, {
      DT::reloadData(proxy)
    })
  }
)

而且,本着上述帖子的精神,它適用於模塊。

library(shiny)

edit_dt_ui <- function(id) {
  tagList(
    fluidRow(
      column(2, actionButton(NS(id, 'refresh'), 'Refresh Data', icon = icon('refresh'))),
      column(10, DT::dataTableOutput(NS(id, 'foo')))
    )
  )
}

edit_dt_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    output$foo <- DT::renderDataTable(
      iris, 
      selection = 'none', 
      editable = 'all',
      caption = 'Double-click to edit cells. Press Ctrl+Enter to save changes.'
    )
    
    proxy <- dataTableProxy('foo')
    
    observeEvent(input$refresh, {
      DT::reloadData(proxy)
    })
    
  })
}

ui <- fluidPage(
  edit_dt_ui('dt')
)

server <- function(input, output, session) {
  edit_dt_server('dt')
}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM