如何在R shiny中使用datatable函数替换DT渲染的数据?

5
我有一个使用R语言中的shiny应用程序,其中有一个使用datatable函数渲染的DT数据表。我想使用dataTableProxyreplaceData来更新表格中的数据,但是我找到的所有示例都假定DT直接从数据对象呈现,而不是使用datatable函数。下面的示例演示了我想要实现的内容,但是这种模式下replaceData不起作用。我该如何操作?谢谢。

# based on 
# https://community.rstudio.com/t/reorder-data-table-with-seleceted-rows-first/4254

library(shiny)
library(DT)

ui = fluidPage(
    actionButton("button1", "Randomize"),
    fluidRow(
        column(6,
               h4("Works"),
               DT::dataTableOutput('table1', width="90%")),
        column(6,
               h4("Doesn't Work"),
               DT::dataTableOutput('table2', width="90%"))
    )
)

server = function(input, output, session) {

        my <- reactiveValues(data = iris)

        output$table1 <- DT::renderDataTable(isolate(my$data))

        output$table2 <- DT::renderDataTable({
            DT::datatable(isolate(my$data),
                          options = list(lengthChange=FALSE, ordering=FALSE, searching=FALSE,
                                       columnDefs=list(list(className='dt-center', targets="_all")),
                                       stateSave=TRUE, info=FALSE),
                          class = "nowrap cell-border hover stripe",
                          rownames = FALSE,
                          editable = FALSE
            ) %>%
                DT::formatStyle('Sepal.Width', `text-align`="center")
        })

        observeEvent(input$button1, {

            # calculate new row order
            row_order <- sample(1:nrow(my$data))
            my$data <- my$data[row_order, ]

            proxy1 <- DT::dataTableProxy('table1')
            DT::replaceData(proxy1, my$data)
            proxy2 <- DT::dataTableProxy('table2')
            DT::replaceData(proxy2, my$data)

        })

}

shinyApp(ui, server)

你尝试选择多个吗?在那种情况下它是有效的。 - amrrs
谢谢,问题出在数据刷新方面。我已经更新了reprex。 - Simon Woodward
4
我会尝试使用replaceData函数并设置选项rownames = FALSE,即DT::replaceData(proxy2, my$data, rownames = FALSE)。该函数将替换数据表格中的内容,并且不包含原本的行名信息。 - Stéphane Laurent
1个回答

10
更新:非常奇怪的是,删除 rownames = FALSE 后就一切都变得可能了。我不确定为什么,但很可能行名对替换数据是必要的。
# based on 
# https://community.rstudio.com/t/reorder-data-table-with-seleceted-rows-first/4254

library(shiny)
library(DT)

ui = fluidPage(
  actionButton("button1", "Randomize"),
  fluidRow(
    column(6,
           h4("Works"),
           DT::dataTableOutput('table1', width="90%")),
    column(6,
           h4("Doesn't Work"),
           DT::dataTableOutput('table2', width="90%"))
  )
)

server = function(input, output, session) {

  my <- reactiveValues(data = iris)

  output$table1 <- DT::renderDataTable(isolate(my$data))

  output$table2 <- DT::renderDataTable({
    DT::datatable(isolate(my$data),
                  options = list(lengthChange=FALSE, ordering=FALSE, searching=FALSE,
                                 columnDefs=list(list(className='dt-center', targets="_all")),
                                 stateSave=TRUE, info=FALSE),
                  class = "nowrap cell-border hover stripe",
                 # rownames = FALSE,
                  editable = FALSE
    ) %>%
      DT::formatStyle('Sepal.Width', `text-align`="center")
  })

  observeEvent(input$button1, {

    # calculate new row order
    row_order <- sample(1:nrow(my$data))
    my$data <- my$data[row_order, ]

    proxy1 <- DT::dataTableProxy('table1')
    DT::replaceData(proxy1, my$data)
    proxy2 <- DT::dataTableProxy('table2')
    DT::replaceData(proxy2, my$data)

  })

}

shinyApp(ui, server)

谢谢,我已经更新了reprex以更清楚地展示我的问题。 - Simon Woodward
@SimonWoodward,那么你希望“doesn't work”的部分工作吗? - amrrs
4
请注意,您也可以在调用replaceData时传入参数rownames=FALSE - bobbel
为了进一步阐述@bobbel所说的观点,如果您不想要行名称,请在replaceDataDT::datatable的调用中都添加rownames=FALSE - reisner
我成功地让这个工作起来了,而不需要在原始表格中使用isolate()。使用它时并没有起作用。 - william3031
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接