Hello,
I need to export multiple tables on an excel sheet called "Sheet_data".
I can create the tables, the workbook, and the sheet using openxlsx.
However, when I combine the loop, using for, and the openxlsx package, I failed.
I know that maybe I should use assign inside the loop, but I can't figure where.
When I open the file I noticed that I export the paste order, I mean, tab_1, tab_2, and so on.
Down I provide an example of my need.
Thanks for your time and interest.
Thanks for the get(paste("tab_",j)) tip!
I rewrite the code. I can run the loop using dplyr over a categorical variable and exporting to excel.
I couldn't understand the purrr option. I can't manage that package.
Also, I obtained a list, and exporting the list on specific rows over excel was complex for me.
I know that loop is not recommended using R, but I'm not confident using purrr.
In fact, I can't use it.
Thanks again.
rm(list=ls())
library(openxlsx)
library(tidyverse)
library(janitor)
data=mpg
qist=data %>% select(manufacturer) %>% unique() %>% pull()
qist
for (i in qist) {
x=data %>% filter(manufacturer==i) %>% group_by(manufacturer,model) %>% count()
assign(paste("tab_",i),x)
}
wb=createWorkbook()
addWorksheet(wb,"Sheet_data")
i=1
for (j in qist) {
writeData(wb,"Sheet_data", get(paste("tab_",j)),startRow = 4+10*(i-1),startCol = 5)
i=i+1
}
saveWorkbook(wb,file ="/home/juan/Desktop/aa.xlsx",overwrite = TRUE)