1

我很难将我的页脚列呈现为可反应的。我想在这里使用 JS 来访问客户端过滤状态,由于我对 JS 不太熟悉,所以我被卡住了。我想要的是:使用两列中汇总的页脚值来为第三列创建汇总的页脚值。请参见下面的示例。

library(shiny)
library(tidyverse)

data = tibble(article =c("a", "b", "c"), sales = c(12,34,16), margin = c(6,20,9)) %>%
  mutate(profit = round(margin / sales * 100, digits = 2))

ui = fluidPage(
  
  div(style = "margin-top: 50px; display: flex; justify-content: center;",
  reactableOutput(outputId = "table"))
  
)

server = function(input, output, session){
 
  output$table = renderReactable({
    reactable(
    data = data,
    columns = list(
      article = colDef(
        footer = "..."),
      sales = colDef(
        format = colFormat(suffix = "€"),
        footer = JS("function(colInfo) {
        var total = 0
        colInfo.data.forEach(function(row) {
        total += row[colInfo.column.id]
        })
        return total.toFixed(2) + '€'}")),
      margin = colDef(
        format = colFormat(suffix = "€"),
        footer = JS("function(colInfo) {
        var total = 0
        colInfo.data.forEach(function(row) {
        total += row[colInfo.column.id]
        })
        return total.toFixed(2) + '€'}")),
      profit = colDef(
        format = colFormat(suffix = "%"),
        footer = "35/62*100"
      )
    ))
  })
   
}
shinyApp(ui, server)
4

0 回答 0