0

我正在尝试使用 ReporteRs 包制作 FlexTable。我希望能够使用 cols.vertical 和 rows.vertical 灵活地指定每列和每行是否存在边界,对于所有 ncol()+ 1 列边界和所有 nrow() - 1 行边界。此外,我希望能够在表格边框的顶部和底部包含较暗的边框。下面显示了使用 mtcars 数据集的列规范示例,使用 iris 数据集的子集显示了较暗边框的示例。

library(ReporteRs)

a <- FlexTable(mtcars, body.cell.props = cellProperties(border.style = "none"))

cols.vertical <- c(2, 4, 5)
rows.horizontal <- c(3, 4, 7)

a[, cols.vertical] <- chprop(cellProperties(border.right.width = 1, 
                                            border.left.width=0, 
                                            border.top.width=0, 
                                            border.bottom.width=0))
a[rows.horizontal, ] <- chprop(cellProperties(border.right.width = 0, 
                                              border.left.width=0, 
                                              border.top.width=0, 
                                              border.bottom.width=1))
a[rows.horizontal, cols.vertical] <- chprop(cellProperties(border.right.width=1, 
                                                           border.left.width=0, 
                                                           border.top.width=0, 
                                                           border.bottom.width=1))

b <- FlexTable(iris[1:10,], body.cell.props = cellProperties(border.style="none"))
b[1, ]<- chprop(cellProperties(border.right.width = 0, 
                               border.left.width=0, 
                               border.top.width=2, 
                               border.bottom.width=0))
b[nrow(iris[1:10, ]), ] <- chprop(cellProperties(border.right.width = 0, 
                                                 border.left.width=0, 
                                                 border.top.width=0, 
                                                 border.bottom.width=2))
4

2 回答 2

0

不确定它是否能准确回答您的问题,但至少您会得到一些代码。

library(ReporteRs)

bdr_1 <- borderSolid(width=2, color="gray")
bdr_2 <- chprop(bdr_1, color="orange")
a <- FlexTable(mtcars)
a <- setFlexTableBorders(a, 
                         inner.vertical = borderNone(), inner.horizontal = borderNone(), 
                         outer.vertical = bdr_1,
                         outer.horizontal = bdr_1,
                         body = TRUE, header = TRUE)

cols.vertical <- c(2, 4, 5)
rows.horizontal <- c(3, 4, 7)

a <- chprop(a, value = bdr_2, i = rows.horizontal, j = cols.vertical, side = "left")
a <- chprop(a, value = bdr_2, i = rows.horizontal, j = cols.vertical, side = "right")
a <- chprop(a, value = bdr_2, i = rows.horizontal, j = cols.vertical, side = "top")
a <- chprop(a, value = bdr_2, i = rows.horizontal, j = cols.vertical, side = "bottom")
a

在此处输入图像描述

于 2017-06-14T19:14:24.653 回答
0

大卫的回答很有帮助,这是我将使用的代码,最后有一些混乱,以允许零在 cols.vertical

library(ReporteRs)

bdr_1 <- borderSolid(width=1)
bdr_2 <- borderSolid(width=2)
a <- FlexTable(mtcars)
a <- setFlexTableBorders(a, 
                         inner.vertical = borderNone(), 
                         inner.horizontal = borderNone(), 
                         outer.vertical = borderNone(),
                         outer.horizontal = bdr_2,
                         body = TRUE, header = FALSE)
a <- setFlexTableBorders(a,  
                         inner.vertical = bdr_1, 
                         inner.horizontal = bdr_1, 
                         outer.vertical = borderNone(),
                         outer.horizontal = bdr_1,
                         body = FALSE, header = TRUE)

cols.vertical <- c(1, 2, 4, 5)
rows.horizontal <- c(1, 3, 4, 7)

a <- chprop(a, value = bdr_1, j = cols.vertical[cols.vertical>0], side = "right")
a <- chprop(a, value = bdr_1, i = rows.horizontal, side = "bottom")

if(0 %in% cols.vertical) {
  if(1 %in% cols.vertical) {
    a <- chprop(a, value = bdr_1, j = 1, side = "left")
    a <- chprop(a, value = bdr_1, j = 1, side = "right")
  } else {
    a <- chprop(a, value = bdr_1, j = 1, side = "left")
  }
}
a
于 2017-06-14T20:10:53.470 回答