Highlight specific cells with borders.
To set borders for the whole table, use border_outer()
,
border_inner_h()
and border_inner_v()
.
All the following functions also support the
row and column selector i
and j
:
hline()
: set bottom borders (inner horizontal)
vline()
: set right borders (inner vertical)
hline_top()
: set the top border (outer horizontal)
vline_left()
: set the left border (outer vertical)
surround(
x,
i = NULL,
j = NULL,
border = NULL,
border.top = NULL,
border.bottom = NULL,
border.left = NULL,
border.right = NULL,
part = "body"
)
a flextable object
rows selection
columns selection
border (shortcut for top, bottom, left and right)
border top
border bottom
border left
border right
partname of the table (one of 'all', 'body', 'header', 'footer')
Other borders management:
border_inner_h()
,
border_inner_v()
,
border_inner()
,
border_outer()
,
border_remove()
,
hline_bottom()
,
hline_top()
,
hline()
,
vline_left()
,
vline_right()
,
vline()
library(officer)
library(flextable)
# cell to highlight
vary_i <- 1:3
vary_j <- 1:3
std_border <- fp_border(color = "orange")
ft <- flextable(head(iris))
ft <- border_remove(x = ft)
ft <- border_outer(x = ft, border = std_border)
for (id in seq_along(vary_i)) {
ft <- bg(
x = ft,
i = vary_i[id],
j = vary_j[id], bg = "yellow"
)
ft <- surround(
x = ft,
i = vary_i[id],
j = vary_j[id],
border.left = std_border,
border.right = std_border,
part = "body"
)
}
ft <- autofit(ft)
ft
#> a flextable object.
#> col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`
#> header has 1 row(s)
#> body has 6 row(s)
#> original dataset sample:
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
# # render
# print(ft, preview = "pptx")
# print(ft, preview = "docx")
# print(ft, preview = "pdf")
# print(ft, preview = "html")