问题描述
我正在尝试使用openxlsx格式化某些列,以将r数据帧写入excel文件。 这是R数据帧的代码段。 r data frame
“种子”列中的方括号部分用于对excel输出进行上标。
openxlsx::addWorksheet(wb,sheetName = 'data') # add sheet
openxlsx::writeData(wb,sheet ='data',x=df,xy=c(1,1),withFilter = T) # write data on workbook
# make quality codes superscript
for(i in grep("\\_\\[([A-z0-9\\s]*)\\]",wb$sharedStrings)){
# if empty string in superscript notation,then just remove the superscript notation
if(grepl("\\_\\[\\]",wb$sharedStrings[[i]])){
wb$sharedStrings[[i]] <- gsub("\\_\\[\\]","",wb$sharedStrings[[i]])
next # skip to next iteration
}
# insert additional formatting in shared string
wb$sharedStrings[[i]] <- gsub("<si>","<si><r>",gsub("</si>","</r></si>",wb$sharedStrings[[i]]))
# find the "_[...]" pattern,remove brackets and udnerline and enclose the text with superscript format
wb$sharedStrings[[i]] <- gsub("\\_\\[([A-z0-9\\s]*)\\]","</t></r><r><rPr><vertAlign val=\"superscript\"/></rPr><t xml:space=\"preserve\">\\1</t></r><r><t xml:space=\"preserve\">",wb$sharedStrings[[i]])
}
openxlsx::modifyBaseFont(wb,fontSize = 10,fontName = 'Arial')
# right-justify data
openxlsx::addStyle(wb,sheet = 'data',style = openxlsx::createStyle(halign = "right"),rows = 1:nrow(df)+1,cols = 3:12,gridExpand = TRUE)
#apply to rows with "All" in column B
openxlsx::conditionalFormatting(wb,cols = 1:ncol(df),rule = 'LEFT($B2,3)="ALL"',style = openxlsx::createStyle(textdecoration = 'bold',bgFill = '#dad9d9'))
# format numbers
openxlsx::addStyle(wb = wb,style = openxlsx::createStyle(numFmt = "#,###.0"),cols = c(5:7,10:12),gridExpand = T)
# write excel file
openxlsx::saveWorkbook(wb,file="file.xlsx",overwrite = TRUE)
输出看起来像这样:
我试图有条件地将seed / harv / yield / prod列中的数字格式化为数字,但是“ F”值充其量只能创建一个混合类向量。 (我需要这些Fs!)
有什么想法吗?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)