You can do this by keeping track of the column you are in and incrementing the value of startCol on each iteration through the list of tables you have. Something like this would work - with some slight modifications to your data to make it easier to loop over:
library(openxlsx)
# Add Dataframes to this list as needed
l = list(
'mtcars' = mtcars,
'iris' = iris,
't2' = mtcars[, 1:5],
't4' = iris[1:8, ]
)
wb <- createWorkbook()
addWorksheet(wb, 'foo') # You can replace 'foo' with whatever you want
start_col <- 1
cols_between_data <- 3 # If you want to vary the space between dataframes,
# you would do that by changing this variable
for(df in l) {
writeData(
wb,
x = df,
sheet = 1,
startCol = start_col
)
start_col <- start_col + ncol(df) + cols_between_data
}
saveWorkbook(wb, 'test.xlsx', overwrite = T, returnValue = T)
Note the use of writeData instead of write.xlsx - this is because write.xlsx makes some assumptions about how you want your spreadsheet to look, and it sounds like you don't want those assumptions here.