【问题标题】:How to format Excel file output in R如何在R中格式化Excel文件输出
【发布时间】:2018-11-07 01:00:26
【问题描述】:

我正在编写一个读取 CSV 文件并生成两个 XLSX 文件的代码,具体取决于站点是否需要更新以及位置在哪里。例如,我会有示例数据,例如:

我现在遇到的问题是,我想应用 Excel 表格样式“橄榄绿,表格样式中号 18”。我有一个必须传递工作簿的选项,但由于我不是直接创建工作簿而是读取 CSV 文件,我将如何继续执行此操作?

现在,我只是使用write.xlsx() 将我的数据导出到 Excel,但我想对表格应用格式。

示例代码:

#Determine the input and output parameters 
input_file <-  choose.files()
output_eu <- "eu.xlsx"
output_noteu <- "noteu.xlsx"

#list of EU countries 
eu <- c("Andorra","Austria","Belarus","Belgium","Bosnia and Herzegovina","Bulgaria","Croatia","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Iceland","Ireland","Italy","Latvia","Liechtenstein","Lithuania","Luxembourg","Malta","Moldova","Monaco","Montenegro","Netherlands","Norway","Poland","Portugal","Romania","Russia","San Marino","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland","Ukraine","United Kingdom")

#reading the csv table
d <- read.table(input_file, sep = ";", header = TRUE, check.names = FALSE) 

# get all cases where there is some text in the Update field
updates <- d[d$Update != "", ]

#within updates are there countries in EU
i <- updates$Country %in% eu

eu_up <- updates[i,]
noteu_up <- updates[!i,]

#Creating the excel files 
library(openxlsx)
write.xlsx(eu_up, output_eu) 
write.xlsx(noteu_up, output_noteu)

Update1:​​添加了示例代码以显示我使用 1 个 CSV 文件进入两个 excel 文件。

更新代码:

#Determine the input and output parameters 
input_file <-  choose.files()
output_eu <- "eu.xlsx"
output_noteu <- "noteu.xlsx"

#list of EU countries 
eu <- c("Andorra","Austria","Belarus","Belgium","Bosnia and Herzegovina","Bulgaria","Croatia","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Iceland","Ireland","Italy","Latvia","Liechtenstein","Lithuania","Luxembourg","Malta","Moldova","Monaco","Montenegro","Netherlands","Norway","Poland","Portugal","Romania","Russia","San Marino","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland","Ukraine","United Kingdom")

#reading the csv table
d <- read.table(input_file, sep = ";", header = TRUE, check.names = FALSE) 

# get all cases where there is some text in the Update field
updates <- d[d$Update != "", ]

#within updates are there countries in EU
i <- updates$Country %in% eu

eu_up <- updates[i,]
noteu_up <- updates[!i,]


#importing openxlsx library 
library(openxlsx)

#create the workbook for each one 
wb_eu <- createWorkbook()
wb_xeu <-createWorkbook()

#adding the data to each corresponding workbook 
addWorksheet(wb_eu, "European Sites") 
addWorksheet(wb_xeu, "Non-European Sites")

#write our tables into each 
writeDataTable(wb_eu, 1, eu_up, startRow=1, startCol=1, tableStyle="TableStyleLight11")
writeDataTable(wb_xeu, 1,  noteu_up, startRow=1, startCol=1, tableStyle="TableStyleLight11")

#setting our column widths 
setColWidths(wb_eu, 1, cols=1:26, widths = "auto") 
setColWidths(wb_xeu, 1, cols=1:26, widths = "auto") 

#saving our workbooks 
saveWorkbook(wb_eu, "European Sites updated.xlsx", overwrite = TRUE) 
saveWorkbook(wb_xeu, "Non-European Sites updated.xlsx", overwrite = TRUE) 

【问题讨论】:

  • 您好,请避免上传您的数据图片,而是上传一小部分数据样本。此外,我们需要查看您的代码才能对其进行测试。尝试使用包含少量数据样本的可重现示例来编辑此问题。
  • 阅读社区指南并请发布可重现的示例...你的问题很好,但你问的方式很重要。所以请避免在问题中附加或发布图片
  • 有几个选项。几年前,我决定使用xlsx 包来创建格式良好的Excel 文件输出。一个替代方案可能是 openxlsx 包,它可能在此期间已经成熟。

标签: r excel


【解决方案1】:

library(openxlsx) 很适合这个。

library(openxlsx)

首先你需要创建一个工作簿:

wb <- createWorkbook()

然后向其中添加两个工作表:

addWorksheet(wb, "EU")
addWorksheet(wb, "NOTEU")

那么我们来写两张表:

writeDataTable(wb, 1, eu_up, startRow = 1, startCol = 1, tableStyle = "TableStyleMedium18")
writeDataTable(wb, 2, noteu_up, startRow = 1, startCol = 1, tableStyle = "TableStyleMedium18")

saveWorkbook(wb, "Tables_with_Formatting.xlsx", overwrite = TRUE)

【讨论】:

  • 谢谢!!现在我添加了 createWorkbook(),它正在工作,我添加了一个更新的代码来反映我所做的事情!
  • 太好了,您可以单击表示此答案解决了您的问题的复选标记,假设它已经解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2012-10-15
  • 1970-01-01
相关资源
最近更新 更多