的R - 导出Excel与闪亮的格式化表格(R - Export excel with forma

2019-09-26 04:01发布

这是我的代码的一部分。

library(shiny)
library(DT)
library(xlsx)

#r input
df <- data.frame(sd = c("A1", "A1", "A1", "A1", "A1", "A1", "A1"),
                 rsm = c("B", "B", "B", "B", "C", "C", "C"),
                 asm = c("D", "D", "E", "E", "F", "G", "H"),
                 sr = c("I", "J", "K", "L", "M", "N", "O"),
                 revenue _target = c(100, 300, 200, 300, 120, 160, 100),
                 revenue_achive = c(80, 30, 150, 450, 20, 80, 12),
                 revenue_perachive = c(80, 10, 75, 150, 25, 50, 12),
                 quantites_target = c(30, 40, 60, 20, 15, 42, 30),
                 quantities_achive = c(15, 25, 30, 42, 10, 10, 12),
                 quantities_perachive = c(50, 62.5, 50, 210, 66.67, 23.81, 40),
                 day = c(10, 10, 10, 12, 12, 12, 12),
                 month = c(2017, 2017, 2017, 2017, 2017, 2017, 2017),
                 stringsAsFactors = FALSE)
)

 #r formatting table
  sketch <-  htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 2, ''),
      th(rowspan = 2, 'sd'),
      th(rowspan = 2, 'rsm'),
      th(rowspan = 2, 'asm'),
      th(rowspan = 2, 'sr'),
      th(colspan = 2, 'Revenue'),
      th(colspan = 2, 'Quantites'),
      th(rowspan = 2, 'day'),
      th(rowspan = 2, 'month')
    ),
    tr(
      lapply(rep(c('target', 'achive', 'perachive'), 2), th)
    )
  )
))

out_table <- reactive({
out <- df
out
})

output$out_tbl <- DT::renderDataTable (
{out_table()},
rownames = FALSE,
container = sketch
)

fluiRow(
column(
width = 12,
dataTableOutput("out_tbl")
))

reactiving,在经过DT::renderDataTable ,我添加了一个选项: container = sketch 。 我有2个误区:

首先,在htmltools::withTags ,我这样做,如果列数合并单元格是一样的。 你看样品,这是3列:“目标”,“achive”和“perachive”。 列名为“月”和“年”,我不能合并。

第二,当我下载Excel中,格式表丢失。 我听不懂。

这是我的输出画面。

output_picture

我如何能做到这一点,像这样的输出画面?

文章来源: R - Export excel with formatting table in shiny