问。我正在寻找一种在阅读和操作后保留excel文件注释的方法。
0。我的excelfile :(不能与您分享!)
1。我想这样阅读:(在excel中将col注释作为R中的col注释)
(使用任何执行此操作的库。openxlsx最好)
df<- data.frame(a=1:3,b=4:6,c=7:9)
comment(df$a) <- "Random comment" #is there a better practice to preserve the excel comment?
2。数据的随机处理:
df[2:3] <- df[2:3] * 2
df$d <- "funny"
编辑:
library(openxlsx)
wb <- loadWorkbook("commented.xlsx")
df <- readWorkbook(wb)
df[2:3] <- df[2:3] * 2
df$d <- "funny"
df <- rev(df)
df <- df[order(df$a,decreasing = T),]
writeData(wb, 1, df)
saveWorkbook(wb, "modified.xlsx")
到目前为止,这是一个很棒的解决方案!
答案 0 :(得分:2)
您可以使用 openxlsx 进行此操作:使用loadWorkbook()
读取原始文件,修改所需的数据,最后完成saveWorkbook()
。评论将被保留:
library(openxlsx)
wb <- loadWorkbook("commented.xlsx")
df <- readWorkbook(wb)
df[2:3] <- df[2:3] * 2
df$d <- "funny"
writeData(wb, 1, df)
saveWorkbook(wb, "modified.xlsx")
unlink(c("commented.xlsx", "modified.xlsx"))
样本数据:
library(openxlsx)
wb <- createWorkbook("unknown")
addWorksheet(wb, "Sheet 1")
df <- data.frame(a = 1:3, b = 4:6, c = 7:9)
writeData(wb, 1, df)
comment <- createComment("Random comment")
writeComment(wb, 1, 1, 1, comment)
saveWorkbook(wb, "commented.xlsx")