在 R 中,如何突出显示特定列的值并导出到现有的 xlsx 文件
In R, How to highlight values of a specific column and export to existing xlsx file
在 iris
tibble 中,如何突出显示 Petal.Width < 1.0
所在的行并将输出导出为现有 xlsx
文件
的 worksheet/tab
# tibble
library(dplyr)
library(tidyr)
library(broom)
set.seed(1234)
df <- iris[sample(nrow(iris), 20), ]
df
输出
# tibble
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
28 5.2 3.5 1.5 0.2 setosa
80 5.7 2.6 3.5 1.0 versicolor
101 6.3 3.3 6.0 2.5 virginica
111 6.5 3.2 5.1 2.0 virginica
137 6.3 3.4 5.6 2.4 virginica
133 6.4 2.8 5.6 2.2 virginica
144 6.8 3.2 5.9 2.3 virginica
132 7.9 3.8 6.4 2.0 virginica
98 6.2 2.9 4.3 1.3 versicolor
103 7.1 3.0 5.9 2.1 virginica
90 5.5 2.5 4.0 1.3 versicolor
70 5.6 2.5 3.9 1.1 versicolor
79 6.0 2.9 4.5 1.5 versicolor
116 6.4 3.2 5.3 2.3 virginica
14 4.3 3.0 1.1 0.1 setosa
126 7.2 3.2 6.0 1.8 virginica
62 5.9 3.0 4.2 1.5 versicolor
4 4.6 3.1 1.5 0.2 setosa
143 5.8 2.7 5.1 1.9 virginica
40 5.1 3.4 1.5 0.2 setosa
我的尝试
请注意:我的尝试未在 xlsx
文件中显示突出显示的行,因此我对其他方法持开放态度。
library(knitr)
library(kableExtra)
library(xlsx)
# with the below code, I am unable to export as an xlsx file because "kable" is not a dataframe
df_highlighted <- kable(df)%>%row_spec(which(df$`Petal.Width` < 1.0), color="red")
df_highlighted
# export the dataframe to an existing xlsx file
write.xlsx(df_highlighted, file="existing_file.xlsx", sheetName="df_highlighted", append=TRUE, row.names=FALSE)
请发表评论并分享您的代码。提前致谢
您可以使用 openxlsx
包执行此操作。这是一个例子:
library(openxlsx)
set.seed(1234)
df <- iris[sample(nrow(iris), 20), ]
wb <- createWorkbook()
addWorksheet(wb, "iris")
writeData(wb, "iris", df)
highlight <- createStyle(fontColour = "#000000",
bgFill = "#FFFF00")
conditionalFormatting(wb,
"iris",
cols = 1:length(df),
rows = 1:nrow(df),
rule = "$D1<1",
style = highlight)
saveWorkbook(wb, "example.xlsx", overwrite = TRUE)
在 iris
tibble 中,如何突出显示 Petal.Width < 1.0
所在的行并将输出导出为现有 xlsx
文件
worksheet/tab
# tibble
library(dplyr)
library(tidyr)
library(broom)
set.seed(1234)
df <- iris[sample(nrow(iris), 20), ]
df
输出
# tibble
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
28 5.2 3.5 1.5 0.2 setosa
80 5.7 2.6 3.5 1.0 versicolor
101 6.3 3.3 6.0 2.5 virginica
111 6.5 3.2 5.1 2.0 virginica
137 6.3 3.4 5.6 2.4 virginica
133 6.4 2.8 5.6 2.2 virginica
144 6.8 3.2 5.9 2.3 virginica
132 7.9 3.8 6.4 2.0 virginica
98 6.2 2.9 4.3 1.3 versicolor
103 7.1 3.0 5.9 2.1 virginica
90 5.5 2.5 4.0 1.3 versicolor
70 5.6 2.5 3.9 1.1 versicolor
79 6.0 2.9 4.5 1.5 versicolor
116 6.4 3.2 5.3 2.3 virginica
14 4.3 3.0 1.1 0.1 setosa
126 7.2 3.2 6.0 1.8 virginica
62 5.9 3.0 4.2 1.5 versicolor
4 4.6 3.1 1.5 0.2 setosa
143 5.8 2.7 5.1 1.9 virginica
40 5.1 3.4 1.5 0.2 setosa
我的尝试
请注意:我的尝试未在 xlsx
文件中显示突出显示的行,因此我对其他方法持开放态度。
library(knitr)
library(kableExtra)
library(xlsx)
# with the below code, I am unable to export as an xlsx file because "kable" is not a dataframe
df_highlighted <- kable(df)%>%row_spec(which(df$`Petal.Width` < 1.0), color="red")
df_highlighted
# export the dataframe to an existing xlsx file
write.xlsx(df_highlighted, file="existing_file.xlsx", sheetName="df_highlighted", append=TRUE, row.names=FALSE)
请发表评论并分享您的代码。提前致谢
您可以使用 openxlsx
包执行此操作。这是一个例子:
library(openxlsx)
set.seed(1234)
df <- iris[sample(nrow(iris), 20), ]
wb <- createWorkbook()
addWorksheet(wb, "iris")
writeData(wb, "iris", df)
highlight <- createStyle(fontColour = "#000000",
bgFill = "#FFFF00")
conditionalFormatting(wb,
"iris",
cols = 1:length(df),
rows = 1:nrow(df),
rule = "$D1<1",
style = highlight)
saveWorkbook(wb, "example.xlsx", overwrite = TRUE)