0

I would like to hide a row in a dataframe that I am writing to Excel using the xlsx package, but can't find a way to do this.

xlsx has the option of

SheetName$setColumnHidden(index, TRUE)

is there no equivalent to hide a single row?

I tried SheetName$setRowGroupCollapsed(rownumber, TRUE), but this hid all rows in the dataframe when saving to Excel

neilfws
  • 32,751
  • 5
  • 50
  • 63
hdiza
  • 23
  • 2

1 Answers1

0

example:

library(tidyverse)

col1 <- c(1,2,3,4,5)
col2 <- c('a', 'b', 'c', 'd', 'e')

data <- data.frame(col1, col2)
> data

  col1 col2
1    1    a
2    2    b
3    3    c
4    4    d
5    5    e

notice how there are row names to the left of col1?

you can do:

> data %>% 
    filter(row.names(.) != 1)

  col1 col2
1    2    b
2    3    c
3    4    d
4    5    e

or:

> data %>% 
    filter(col1 != 1)

  col1 col2
1    2    b
2    3    c
3    4    d
4    5    e

then you do:

> data <- data %>% 
    filter(col1 != 1)

> data

  col1 col2
1    2    b
2    3    c
3    4    d
4    5    e

then write.xlsx(data, "abc.xlsx", sheetname = "poop")

Nicholas
  • 21
  • 2
  • Thanks for the response. This is removing the row from the dataframe - I'm hoping there's a way to write to Excel with the row hidden but still present, similar to setColumnHidden in the xlsx package. I need the row still present, but hidden for display purposes. – hdiza Mar 21 '23 at 03:02