【问题标题】:how to group, summarize and render table in shiny?如何在闪亮中对表格进行分组,汇总和渲染?
【发布时间】:2021-05-01 00:01:15
【问题描述】:

我正在尝试创建一个可以在本地闪亮的网络应用程序中呈现的汇总表。

这是一个示例数据集。

df1 = structure(list(D_NoIDProv = c("900098550", "900098550", "900098550", 
"900098550", "900098550", "900098550", "900098550", "900098550", 
"900098550", "900098550", "900098550", "900098550", "900098550", 
"900098550", "900098550", "900098550", "900098550", "900098550", 
"900098550", "900098550"), D_TipoTec = c("N", "N", "N", "N", 
"N", "N", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M"), CUMS = c("150416", "150416", "150416", "170176", 
"170176", "170176", "19945455-02", "19945455-02", "20046741-04", 
"20046741-04", "20041735-05", "20041735-05", "20041735-05", "20041735-05", 
"20001982-02", "20001982-02", "20001982-02", "19933626-02", "19933626-02", 
"19933626-02"), D_CantTotAEntregar = c(2, 2, 1, 30, 30, 30, 1, 
1, 90, 90, 30, 30, 30, 30, 1, 1, 1, 1, 1, 1), D_FecDireccionamiento = structure(c(1617299520, 
1617299520, 1617299580, 1617299640, 1617299700, 1617299700, 1617235200, 
1617235260, 1617235500, 1617235560, 1617357540, 1617357600, 1617357600, 
1617357660, 1617358080, 1617358080, 1617358080, 1617358200, 1617358200, 
1617358260), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    D_FecMaxEnt = structure(c(18747, 18777, 18808, 18747, 18777, 
    18808, 18854, 18885, 18748, 18779, 18748, 18779, 18809, 18840, 
    18748, 18779, 18809, 18748, 18779, 18809), class = "Date"), 
    ValorUnitarioMax = c(75952, 75952, 75952, 18594, 18594, 18594, 
    96725, 96725, 156, 156, 897, 897, 897, 897, 32695, 32695, 
    32695, 20892, 20892, 20892), ValorTotal = c(151904, 151904, 
    75952, 557810, 557810, 557810, 96725, 96725, 14067, 14067, 
    26903, 26903, 26903, 26903, 32695, 32695, 32695, 20892, 20892, 
    20892)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", 
"data.frame"))

到目前为止我的服务器代码

      df1 = df1 %>%  
            group_by(D_NoIDProv) %>% 
            summarise(SumValorTotal=sum(ValorTotal)) %>%
            ungroup()

    output$mytable = DT::renderDataTable( df1 ,
                                          filter = 'top',
                                          rownames = FALSE,
                                          options = list(autoWidth = TRUE,
                                                         columnDefs = list(list(width = '100px',className = 'dt-center' ,targets = 0:2),
                                                                           list(width = '400px',className = 'dt-center' ,targets = 3))
                                                         )
                                          )

这在 RStudio 中运行良好,但我无法尝试在闪亮上可视化汇总表,因为它不会填充。

我进行了一些检查,表格在 :

之前正确呈现
        group_by(D_NoIDProv) %>% 
        summarise(SumValorTotal=sum(ValorTotal)) %>%
        ungroup()

我做错了吗?

感谢您的帮助

【问题讨论】:

  • 如果D_NoIDProv 是数据中的一个变量,为什么不直接使用group_by(D_NoIDProv) 而不是间接使用!!sym(.)(这对你不起作用)。
  • 仅供参考,找不到对象,因此无法测试或重现此:date_startdate_endprecios_unitarios_mipres,我假设 D_CantTotAEntregarValorUnitarioMax 是在precios_unitarios_mipres 中找到,否则请同时提供。
  • 请保留editing :-) 你还有一个!!sym(.),我们仍然不知道其他3个(或更多)对象是什么样的。
  • (2/2) 我可以轻松更改其中一个连接变量,以便我们实际上得到一个sum,这很好。之后,我用DT::datatable 替换了你的DT::render*,只是为了在控制台上尝试它,我得到DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter '2' for row 0, column 2. For more information about this error, please see http://datatables.net/tn/4。输出帧只有两列,因此更改为单个 columnDefs = list(list(width = '100px',className = 'dt-center' ,targets = 0:1) 可以修复错误。这就是你所追求的吗?
  • 也就是说,您的框架是 2 列,但您的选项使用 targets=0:2(需要 3 列)和 targets=3(假定至少 4 列)。

标签: r shiny


【解决方案1】:

问题不在于group_by 或基本渲染,而在于您对DT::datatable 的选项建议的列比您的摘要产生的列更多。值得注意的是,df1 只有两列(D_NoIDProvSumValorTotal),但在您的选项中有 targets=0:2targets=3,您建议您至少有 4 列。

如果您期望只有两列,那么您需要将选项减少为:

df1 %>%  
  group_by(D_NoIDProv) %>% 
  summarise(SumValorTotal=sum(ValorTotal)) %>%
  ungroup() %>% 
  DT::datatable(filter = 'top',
                rownames = FALSE,
                options = list(autoWidth = TRUE,
                  columnDefs = list(list(width = '100px',className = 'dt-center' ,targets = 0:1))
                )
  )

df1 %>%  
  group_by(D_NoIDProv) %>% 
  summarise(SumValorTotal=sum(ValorTotal)) %>%
  ungroup() %>% 
  DT::datatable(filter = 'top',
                rownames = FALSE,
                options = list(autoWidth = TRUE,
                  columnDefs = list(list(width = '100px',className = 'dt-center' ,targets = 0),
                                    list(width = '400px',className = 'dt-center' ,targets = 1))
                )
  )

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    相关资源
    最近更新 更多