繁体   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