Kurt Amend
Kurt Amend

Reputation: 27

How to write out a dataframe or vector horizontally to excel using openxlsx2 in R

It feels like I am missing something easy here, but I want to write out data in Excel horizontally as opposed to vertically.

I am using openxlsx2 to automate the creation of some spreadsheets.

Assuming I have a vector of data that is the following:

['Yes', 'Not Found', 'Yes']

I would like to write this to cells A1, B1, and C1.

Right now if I use the following code, it will write to A1, A2, A3 instead.

library(openxlsx2)
wb = wb_workbook()
wb$add_worksheet('test')

vec = c('Yes', 'Not Found', 'Yes')
wb$add_data(sheet = 'test', x= vec)

I am not seeing an argument that I can specify to write this out horizontally. Any thoughts?

Upvotes: 0

Views: 133

Answers (1)

Friede
Friede

Reputation: 8088

For this particular toy example using t() to transpose vec is enough

library(openxlsx2)
wb = wb_workbook()
wb$add_worksheet('test')

vec = c('Yes', 'Not Found', 'Yes')
wb$add_data(sheet = 'test', x = t(vec), col_names = FALSE) 

# wb_save(wb, file = temp_xlsx(), overwrite = TRUE)

this writes the vector elements to A1, B1, and C1:

enter image description here

Upvotes: 1

Related Questions