簡體   English   中英

使用replaceData函數替換R formattable datatable中的數據

[英]Replace data in R formattable datatable using replaceData function

我需要平穩地替換(格式表)數據表中的數據,而重新加載時頁面不會閃爍。

遵循來自@yihui的示例: https : //github.com/rstudio/DT/issues/168我已經成功地平滑替換了標准數據表中的數據,而無需使用dataTableProxy函數刷新頁面。

通過formattable包包含格式時,我的代碼引發錯誤:警告:as.data.frame.default中的錯誤:無法將類“ c(“ datatables”,“ htmlwidget”)“強制轉換為data.frame

最小的可重現示例:

library(shiny)
library(DT)
library(formattable)

dt <- data.frame(type = letters[1:5], count = sample(1:10, 5))

shinyApp(
    ui = fluidPage(sidebarLayout(
        sidebarPanel(
            sliderInput(
                "number",
                "Select:",
                min = 0,
                max = 10,
                value = 8
            )
        ),

        mainPanel(DT::dataTableOutput('DTtable'))
    )),

    server = function(input, output, session) {
        # Reactive expression of the data frame, subset by the slider number
        sliderValues <- reactive({
            # Compose data frame
            dt['count' > input$number,]
        })


        output$DTtable = DT::renderDataTable(as.datatable(formattable(
            isolate(sliderValues()),

            list(count = color_tile('#ffffff', '#6be560'))
        )))


        observeEvent(sliderValues(), ignoreInit = T, {
            replaceData(dataTableProxy('DTtable'),

                as.datatable(formattable(
                    isolate(sliderValues()),

                    list(count = color_tile('#ffffff', '#6be560'))
                )))
        })
    }
)

當我移動滑塊時,我希望表格重新加載,同時還保留格式表樣式。

sliderValues小錯誤。 用。。。來代替

sliderValues <- reactive({
  # Compose data frame
  dt[dt$count > input$number,]
})

現在, replaceData在第二個參數中需要一個數據replaceData ,而不是數據表。 這就是為什么您會收到此錯誤。 當您有數據表dtable ,數據幀位於dtable$x$data 但是行名還有一個附加列,必須將其刪除。 這樣:

observeEvent(sliderValues(), ignoreInit = TRUE, {
  replaceData(dataTableProxy('DTtable'),
              as.datatable(formattable(
                isolate(sliderValues()),
                list(count = color_tile('#ffffff', '#6be560'))
              ))$x$data[,-1]
  )
})

暫無
暫無

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

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