简体   繁体   English

使用replaceData函数替换R formattable datatable中的数据

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

I need to replace data in a (formattable) datatable smoothly without the page flashing while re-loading. 我需要平稳地替换(格式表)数据表中的数据,而重新加载时页面不会闪烁。

Following the example from @yihui here: https://github.com/rstudio/DT/issues/168 I have managed to replace data in a standard datatable smoothly without the page flashing using the dataTableProxy function. 遵循来自@yihui的示例: https : //github.com/rstudio/DT/issues/168我已经成功地平滑替换了标准数据表中的数据,而无需使用dataTableProxy函数刷新页面。

When including the formatting via the formattable package my code throws an error: Warning: Error in as.data.frame.default: cannot coerce class "c("datatables", "htmlwidget")" to a data.frame 通过formattable包包含格式时,我的代码引发错误:警告:as.data.frame.default中的错误:无法将类“ c(“ datatables”,“ htmlwidget”)“强制转换为data.frame

Minimal reproducible example: 最小的可重现示例:

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'))
                )))
        })
    }
)

When I move the slider I would like the table to reload whilst also retaining the formattable styling. 当我移动滑块时,我希望表格重新加载,同时还保留格式表样式。

Small error in sliderValues . sliderValues小错误。 Replace with 用。。。来代替

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

Now, replaceData requires a dataframe in the second argument, not a datatable. 现在, replaceData在第二个参数中需要一个数据replaceData ,而不是数据表。 That's why you get this error. 这就是为什么您会收到此错误。 When you have a datatable dtable , the dataframe is in dtable$x$data . 当您有数据表dtable ,数据帧位于dtable$x$data But there is an additional column for the rownames, which must be removed. 但是行名还有一个附加列,必须将其删除。 So do: 这样:

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