我正在尝试将“可见度”和“欺诈”列与单元格的右侧对齐,但是我似乎找不到一种简单的方法来做到这一点。我尝试了几种不同的选项,但是一切都会返回错误。请在下面查看我的代码:
library(tidyverse)
library(xlsx)
library(openxlsx)
library(data.table)
library(taskscheduleR)
#Create Workbook
workbook <- createWorkbook()
addWorksheet(workbook, "Viewability")
addWorksheet(workbook, "Fraud")
#Read Files and Create Data
files <- file.info(Sys.glob("C:/Users/briantos/Documents/Blacklist Raw/*.csv"))
newestfile <- row.names(files)[which.max(files[["ctime"]])]
blacklist <- fread(newestfile)
#Create Viewability Data
blacklist_viewability <- blacklist %>%
select(`Delivery Site`, `Video Viewable Impressions`, `Measured Impressions`) %>%
group_by(`Delivery Site`) %>%
summarize(Sum_of_Measured_Impressions = sum(`Measured Impressions`, na.rm = TRUE),
Sum_of_Video_Viewable_Impressions = sum(`Video Viewable Impressions`, na.rm = TRUE)) %>%
mutate(Viewability = `Sum_of_Video_Viewable_Impressions`/`Sum_of_Measured_Impressions`) %>%
arrange(Viewability, desc(Sum_of_Measured_Impressions)) %>%
select(-Sum_of_Video_Viewable_Impressions) %>%
mutate(Viewability = paste(round(100*Viewability, 2), "%", sep=""))
#Create Fraud Data
blacklist_fraud <- blacklist %>%
select(`Delivery Site`, `Fraud/SIVT Incidents`, `Monitored Impressions`) %>%
group_by(`Delivery Site`) %>%
summarize(Sum_of_Monitored_Impressions = sum(`Monitored Impressions`, na.rm = TRUE),
Sum_of_Incidents = sum(`Fraud/SIVT Incidents`, na.rm = TRUE)) %>%
mutate(Fraud = `Sum_of_Incidents`/`Sum_of_Monitored_Impressions`) %>%
arrange(desc(Fraud), desc(Sum_of_Monitored_Impressions)) %>%
select(-Sum_of_Incidents) %>%
mutate(Fraud = paste(round(100*Fraud, 2), "%", sep=""))
#Turn Into Data Frame
blacklist_viewability <- as.data.frame(blacklist_viewability)
blacklist_fraud <- as.data.frame(blacklist_fraud)
#Write Data into Workbook
writeData(workbook, sheet = "Viewability", blacklist_viewability)
writeData(workbook, sheet = "Fraud", blacklist_fraud)
#Set Column Widths
setColWidths(workbook, sheet = "Viewability", cols = 1:3, widths = "auto")
setColWidths(workbook, sheet = "Fraud", cols = 1:3, widths = "auto")
#Write File
saveWorkbook(workbook, paste("C:/Users/briantos/Documents/Blacklist Complete/",Sys.Date(),"_Blacklist.xlsx"))## Heading ##
我尝试了alignment()
函数,CellStyle()
功能等。它们都返回错误。请帮忙!
答案 0 :(得分:0)
以下是使用openxlsx
的示例。您正在寻找的是方法halign;valign
中的参数createStyle
。
library(openxlsx)
wb <- createWorkbook()
wb$addWorksheet('test')
wb$writeData(sheet = 'test',
df = cbind.data.frame(matrix(1:9,3,3), let=letters[1:3]),
colClasses = c(rep('integer',3),'character'), keepNA = TRUE,
colNames = TRUE, startRow = 1L, startCol = 1L)
wb$addStyle(sheet = 1, rows = 1:4, cols = 4L,
style = createStyle(halign = 'right')) # set halign='right' - leave every other element to default value
saveWorkbook(wb,'d:/Test/test_wb.xlsx') # last column is right-aligned
您也可以简单地进行
addStyle(wb = wb, sheet = 'test', cols = 4L, rows = 1:4,
style = createStyle(halign = 'right'))
修改
行/列选择:我通常要做的工作是df
(有问题的工作表的数据框架),并清楚地确定我要在哪里应用格式
# Ex. 1: all rows, column 4
addStyle(wb = wb, sheet = 'test', cols = 4L, rows = 1:nrow(df),
style = createStyle(halign = 'right'))
# Ex. 2: all rows, all columns
addStyle(wb = wb, sheet = 'test', cols = 1:ncol(df), rows = 1:nrow(df),
gridExpand = TRUE, # make sure to set this to TRUE in order to really select everything
style = createStyle(halign = 'right'))
然后,您可以轻松选择行或列组,例如rows = c(1,3)
(您也可以只设置最大列数和行数,但是我想对每个单元格应用格式都不是很有效)。
此外,如果您的数据不是从行/列号1而是n开始,那么您就需要移动选择,例如rows = n:(nrow(df)+n-1)
(可能需要删除-1
-不确定。我总是在这里犯错)。