我正在crosstalk
与reactable
. 在表中,我使用分组行。现在我需要使用 javascript 添加一列总计。
---
title: "Focal Chart (Top)"
output:
flexdashboard::flex_dashboard:
orientation: rows
---
Inputs {.sidebar}
-------------------------------------
```{r}
library(crosstalk)
library(reactable)
cars <- MASS::Cars93[1:20, c("Manufacturer", "Model", "Type", "Price")]
data <- SharedData$new(cars)
filter_checkbox("type", "Type", data, ~Type)
filter_slider("price", "Price", data, ~Price, width = "100%")
filter_select("mfr", "Manufacturer", data, ~Manufacturer)
```
Row {data-height=500}
-------------------------------------
### Chart 1
```{r}
reactable(
data,
groupBy = "Manufacturer",
searchable = TRUE,
columns = list(
Price = colDef(aggregated = JS("
function(cellInfo) {
// Calculate sum of all values in visible rows
var values = cellInfo.subRows.map(function(row) { return row.Price})
var total = values.reduce(function(a, b) { return a + b }, 0)
return total.toFixed(1)
}
"), footer = JS("function(colInfo) {
var values = colInfo.data.map(function(row) { return row[colInfo.column.id] })
var total = values.reduce(function(a, b) { return a + b }, 0)
return '$' + total.toFixed(2)
}")
),
Manufacturer = colDef(footer = "Total")
),
defaultColDef = colDef(footerStyle = list(fontWeight = "bold"))
)
```
Row {data-height=500}
-------------------------------------
### empty
```{r}
```
### empty
```{r}
```
如您所见,列总数为$NaN。当然,这是一个不希望的结果。如果我删除该groupBy
参数,则该列总计为$471.80,应该是这样。有谁知道如何解决这种groupBy
行为?提前谢谢了!