2017-06-14 110 views
0

我想使用ReporteRs软件包制作FlexTable。我希望能够使用cols.vertical和rows.vertical,针对所有ncol()+ 1列边框和所有nrow() - 1行边框,灵活指定每个列和行是否存在边框。此外,我希望能够在桌面边框的顶部和底部包含较暗的边框。以下使用mtcars数据集显示列说明的示例,并使用虹膜数据集的子集显示较暗边界的示例。ReporteRs单元格边框

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)) 
+0

CHPROP是被用来修改一个属性,因为在你的例子中没有指定属性,这没有效果。 –

回答

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 

enter image description here

+0

“chprop(a,value = bdr_2,...)”中的“格式化属性对象”是bdr_2,不是正确的? – Ben

+0

是的,chprop也适用于FlexTable –

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