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

2 Answers 2

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")

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

Use openxlsx package and set the row height equal to 0. For example, you want to hide rows 10:15, you can write something like this:

setRowHeights(wb, sheet = "Sheet1",rows = 10:15, heights = 0)
saveWorkbook(wb,"testing.xlsx",overwrite = T)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.