writexl icon indicating copy to clipboard operation
writexl copied to clipboard

Is it possible to add a row_names parameter?

Open ENIAC-6 opened this issue 5 years ago • 5 comments

I'd like to be able to read in a data frame's row names. I see col_names, but it would also be helpful to have row_names.

ENIAC-6 avatar Jan 29 '20 21:01 ENIAC-6

Can't you just add add the row names to a regular column in your data frame?

mtcars$name = row.names(mtcars)
write_xlsx(mtcars, "mtcars.xlsx")

jeroen avatar Jan 29 '20 21:01 jeroen

Can't you just add add the row names to a regular column in your data frame?

mtcars$name = row.names(mtcars)
write_xlsx(mtcars, "mtcars.xlsx")

This wouldn't work for creating an xlsx with multiple named sheets ...

Jimaras08 avatar Oct 26 '20 17:10 Jimaras08

Hi, I had the same issue, It is very important for me to have the row names. the code you mentioned is working for me but in general, it is useful and easier to have the row_names = TRUE in function as there is col_names = TRUE....

Thanks :-)

Elham-adabi avatar Jun 21 '22 09:06 Elham-adabi

I do not quite understand the importance :), but you could, of course write your own function.

Something like this when x is a data frame and not a list (for lists of data frames, alter this function):

write_xlsx_rownames <- function(
  x,
  path = tempfile(fileext= ".xlsx"),
  col_names = TRUE,
  row_names = TRUE,
  format_headers = TRUE,
  use_zip64 = FALSE
){
if (row_names == TRUE){
 x <- data.frame(rowname = row.names(x), x)
}
write_xlsx(
  x,
  path = path,
  col_names = col_names,
  format_headers = format_headers,
  use_zip64 = use_zip64
)
}

I have not tested it (I am writing on my mobile phone), but I guess you get the idea.

rdatasculptor avatar Jun 22 '22 22:06 rdatasculptor

it is important cause I am working with gene sequencing data and rownames are gene names! :-) and this function is working thank you 👍

Elham-adabi avatar Jun 24 '22 14:06 Elham-adabi