Hello,
I am discovering cool kableExtra features for tables, yet cannot get collapse_rows to work. Can you spot what is wrong with the code?
library(kable)
library(kableExtra)
mtcars <- arrange(mtcars, cyl)
kbl(mtcars) |> kable_classic(full_width=F) |> collapse_rows(columns = 2, valign = "top")
1 Like
Hope this is of some use to you
function to collapse_row
#' Collapse the values within a grouped dataframe
#'
#'
collapse_rows_df <- function(df, variable){
group_var <- enquo(variable)
df %>%
group_by(!! group_var) %>%
mutate(groupRow = 1:n()) %>%
ungroup() %>%
mutate(!!quo_name(group_var) := ifelse(groupRow == 1, as.character(!! group_var), "")) %>%
select(-c(groupRow))
}
To execute:
collapse_rows_df(mtcars,cyl)
Credit goes to
1 Like
Thanks - this is some workaround indeed.
While the original collapse_rows has nice additional settings, the core task is solved by this function, I will keep it in case nobody has a clue what's wrong with the original, thanks
1 Like
Ok, I understand. in connection with kableExtra table, you may please review my video on my channel "Happy Learning-GP". here is the link: Making Awesome HTML Table with kableExtra package in R - YouTube
1 Like
Great, thanks for the link ;D
1 Like
cderv
April 21, 2022, 9:35am
6
If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
Well, sure, but the question has not been answered yet.
@ganapap1 presented a valuable workaround - but the question how to use collapse_rows so it works has not been answered.
Do you think waiting for a solution is futile?
cderv
April 21, 2022, 10:14am
8
Oh sorry I thought it was my bad.
You could also open a Feature request or an issue with kableExtra repo is something is off.
Ok, thanks. To be honest I do not know how to open an issue with kableExtra repo.
And to do that I guess I should be quite sure that it is not my coding mistake, which I am not.
cderv
April 22, 2022, 9:44pm
10
Ok, thanks, the issue is opened
1 Like
cderv
April 27, 2022, 8:17am
12
Sharing link here for reference :
opened 03:25PM - 26 Apr 22 UTC
closed 09:07PM - 27 Apr 22 UTC
Hello,
I was directed here from RStudio Community
When I try to use collap… se_rows I get no effect - can't locate fault in the code, it is:
```{r}
library(kable)
library(kableExtra)
mtcars <- arrange(mtcars, cyl)
kbl(mtcars) |> kable_classic(full_width=F) |> collapse_rows(columns = 2, valign = "top")
```
PS Thanks for the great stuff kableExtra is :)
Thanks. This really helped me out!
1 Like