I'm asking if I could export from R, a huxtable dataset in one sheet and a plot from ggplot2 in another sheet, and in the same excel file?
wb <- createWorkbook()
addWorksheet(wb, sheetName = "Frequencies")
addWorksheet(wb, sheetName = "Plot")
writeDataTable(wb, sheet = "Frequencies", x = huxtable, row.names=F)
plot(p)
insertPlot(wb,"Plot")
saveWorkbook(wb=wb, file="path_file/name_file.xlsx", overwrite=TRUE)
I tried with the above code, the huxtable
is the formatted dataset (rows of the dataset are colored), and p
is the plot that I generated using the function ggplot()
, but I didn't get the required output because I lost the formatting from the huxtable
.
I tried with this code, but it only export the huxtable with formatting and not the plot:
file<- as_Workbook(huxtable,sheet="Frequencies")
showGridLines(file, sheet="Frequencies", showGridLines = FALSE)
openxlsx::saveWorkbook(file,"file_path/file_name.xlsx", overwrite = TRUE)
here is an example of the plot and the huxtable:
p <-
ggplot(mtcars)+
geom_histogram(aes(x = mpg))
p
huxtable<-as_hxtable(mtcars[1:10,])
for (i in 1:length(huxtable) ) {
if (i == 1){
huxtable<-set_background_color(huxtable,row=i , everywhere, "yellow")
}
else{
huxtable<-set_background_color(huxtable,row=i , everywhere, "red")
}
}
huxtable
I want to export the coloured dataset + plot into same excel file without losing the format of dataset