R:如何为多个数据框添加边框?
R: how to add borders to multiple data frames?
我在将样式添加到一个 Excel sheet 中的多个数据帧时遇到问题。我想在一个 sheet.
中为每个 table 单独绘制边框
我已经有一个代码可以自动从列表中添加多个数据框:
library(openxlsx)
# Data
table1 <- data.frame("Num" = c(5,6,8,10), "Call" = c(1,2,3,4), "Name" = c("a", "b", "c", "d"), stringsAsFactors = FALSE)
table2 <- data.frame("Num" = c(8,1,11,54,3,5), "Call" = c(1,2,3,4,5,6), "Name" = c("f", "g", "h", "i", "j", "k"), "Age" = c(55,21,30,74,16,41), stringsAsFactors = FALSE)
df_list <- list(table1=table1, table2=table2)
wb <- createWorkbook()
addWorksheet(wb, sheetName ="first")
s1 <- createStyle(border = "TopBottomLeftRight")
curr_row <- 1
curr_col <- 1
for(i in seq_along(df_list)) {
writeData(wb, "first", names(df_list)[i], startCol = 1, startRow = curr_row)
writeData(wb, "first", df_list[[i]], startCol = 1, startRow = curr_row+1, rowNames = TRUE)
curr_row <- curr_row + nrow(df_list[[i]]) + 3
}
saveWorkbook(wb, paste0(Sys.Date()," Test_file (openxlsx)",".xlsx"))
所以一个问题是有没有通过循环为每个数据帧添加样式的解决方案?或者我必须为每个 table?
单独编写 addStyle
谢谢!
所以,如果我没猜错的话,你想在每个 table 周围设置边框。
您可以在附件中找到一个示例,其中每个 table 周围都有一个边框(table 个名称除外)。我还添加了一个额外的 table3 来测试实现。此过程适用于任意数量的行和列。
library(openxlsx)
# Data
table1 <- data.frame("Num" = c(5,6,8,10), "Call" = c(1,2,3,4), "Name" = c("a", "b", "c", "d"), stringsAsFactors = FALSE)
table2 <- data.frame("Num" = c(8,1,11,54,3,5), "Call" = c(1,2,3,4,5,6), "Name" = c("f", "g", "h", "i", "j", "k"), "Age" = c(55,21,30,74,16,41), stringsAsFactors = FALSE)
table3 <- data.frame("Num" = c(8,1,11,54,3,5, 10, 10), "Call" = c(0, 0, 1,2,3,4,5,6), "Name" = c("a", "b", "f", "g", "h", "i", "j", "k"), "Age" = c(0, 0, 55,21,30,74,16,41),
"Test" = c(0, 0, 55,21,30,74,16,41), stringsAsFactors = FALSE)
df_list <- list(table1=table1, table2=table2, table3 = table3)
wb <- createWorkbook()
addWorksheet(wb, sheetName ="first")
s1 <- createStyle(border = "TopBottomLeftRight")
curr_row <- 1
curr_col <- 1
for(i in seq_along(df_list)) {
writeData(wb, "first", names(df_list)[i], startCol = 1, startRow = curr_row)
writeData(wb, "first", df_list[[i]], startCol = 1, startRow = curr_row+1, rowNames = TRUE)
addStyle(wb, sheet = "first", style = s1, rows = (curr_row+1):(nrow(df_list[[i]]) + (curr_row+1)), cols = 1:(1 + ncol(df_list[[i]])), gridExpand = TRUE)
curr_row <- curr_row + nrow(df_list[[i]]) + 3
}
saveWorkbook(wb, paste0(Sys.Date()," Test_file (openxlsx)",".xlsx"))
由 reprex package (v2.0.1)
于 2022-05-09 创建
附上屏幕。
我在将样式添加到一个 Excel sheet 中的多个数据帧时遇到问题。我想在一个 sheet.
中为每个 table 单独绘制边框我已经有一个代码可以自动从列表中添加多个数据框:
library(openxlsx)
# Data
table1 <- data.frame("Num" = c(5,6,8,10), "Call" = c(1,2,3,4), "Name" = c("a", "b", "c", "d"), stringsAsFactors = FALSE)
table2 <- data.frame("Num" = c(8,1,11,54,3,5), "Call" = c(1,2,3,4,5,6), "Name" = c("f", "g", "h", "i", "j", "k"), "Age" = c(55,21,30,74,16,41), stringsAsFactors = FALSE)
df_list <- list(table1=table1, table2=table2)
wb <- createWorkbook()
addWorksheet(wb, sheetName ="first")
s1 <- createStyle(border = "TopBottomLeftRight")
curr_row <- 1
curr_col <- 1
for(i in seq_along(df_list)) {
writeData(wb, "first", names(df_list)[i], startCol = 1, startRow = curr_row)
writeData(wb, "first", df_list[[i]], startCol = 1, startRow = curr_row+1, rowNames = TRUE)
curr_row <- curr_row + nrow(df_list[[i]]) + 3
}
saveWorkbook(wb, paste0(Sys.Date()," Test_file (openxlsx)",".xlsx"))
所以一个问题是有没有通过循环为每个数据帧添加样式的解决方案?或者我必须为每个 table?
单独编写 addStyle谢谢!
所以,如果我没猜错的话,你想在每个 table 周围设置边框。
您可以在附件中找到一个示例,其中每个 table 周围都有一个边框(table 个名称除外)。我还添加了一个额外的 table3 来测试实现。此过程适用于任意数量的行和列。
library(openxlsx)
# Data
table1 <- data.frame("Num" = c(5,6,8,10), "Call" = c(1,2,3,4), "Name" = c("a", "b", "c", "d"), stringsAsFactors = FALSE)
table2 <- data.frame("Num" = c(8,1,11,54,3,5), "Call" = c(1,2,3,4,5,6), "Name" = c("f", "g", "h", "i", "j", "k"), "Age" = c(55,21,30,74,16,41), stringsAsFactors = FALSE)
table3 <- data.frame("Num" = c(8,1,11,54,3,5, 10, 10), "Call" = c(0, 0, 1,2,3,4,5,6), "Name" = c("a", "b", "f", "g", "h", "i", "j", "k"), "Age" = c(0, 0, 55,21,30,74,16,41),
"Test" = c(0, 0, 55,21,30,74,16,41), stringsAsFactors = FALSE)
df_list <- list(table1=table1, table2=table2, table3 = table3)
wb <- createWorkbook()
addWorksheet(wb, sheetName ="first")
s1 <- createStyle(border = "TopBottomLeftRight")
curr_row <- 1
curr_col <- 1
for(i in seq_along(df_list)) {
writeData(wb, "first", names(df_list)[i], startCol = 1, startRow = curr_row)
writeData(wb, "first", df_list[[i]], startCol = 1, startRow = curr_row+1, rowNames = TRUE)
addStyle(wb, sheet = "first", style = s1, rows = (curr_row+1):(nrow(df_list[[i]]) + (curr_row+1)), cols = 1:(1 + ncol(df_list[[i]])), gridExpand = TRUE)
curr_row <- curr_row + nrow(df_list[[i]]) + 3
}
saveWorkbook(wb, paste0(Sys.Date()," Test_file (openxlsx)",".xlsx"))
由 reprex package (v2.0.1)
于 2022-05-09 创建附上屏幕。