Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Cmidrules and Multicolumn for kable #2136

Closed
amarz45 opened this issue Jun 8, 2022 · 5 comments · Fixed by #2138
Closed

[Feature request] Cmidrules and Multicolumn for kable #2136

amarz45 opened this issue Jun 8, 2022 · 5 comments · Fixed by #2138
Labels
feature Feature requests table

Comments

@amarz45
Copy link
Contributor

amarz45 commented Jun 8, 2022

Suppose I want to make the following table:

This table has a cmidrule from 2-3 and it is multi-columned.
Here is how I can make it using LaTeX code:

\begin{tabular}{lll}
\toprule
Activity & \multicolumn{2}{l}{February 10, 2022}\\
\cmidrule(l){2-3}
& Score & Goal\\
\midrule
Push-ups & 20 & 25\\
\addlinespace
Crunches & 24 & 30\\
\addlinespace
Plank & 1:00 & 1:30\\
\addlinespace
Vertical jump & 40 & 50\\
\addlinespace
Sit, reach, and hold & 16 & 17\\
\bottomrule
\end{tabular}

Making this table using kable is impossible because

  1. kable does not support cmidrules
  2. kable does not support the \multicolumn command

I tried to recreate it in kable with this code:

activity = c("Push-ups", "Crunches", "Plank", "Vertical jump", "Sit, reach, and hold")
one = c("20", "24", "1:00", "40", "16")
two = c("25", "30", "1:30", "50", "17")

fitness_test = data.frame(activity, one, two)

knitr::kable(fitness_test, escape = FALSE, booktabs = TRUE, linesep = "\\addlinespace", col.name=c("Activity", "\\multicolumn{2}{l}{February 10, 2022}"))

It gave me this error:

Error in dimnames(x) <- dn :
  length of 'dimnames' [2] not equal to array extent
Calls: <Anonymous> ... eval_with_user_handlers -> eval -> eva
l -> <Anonymous> -> colnames<-

When I made the table in LaTeX, the header row only needed to be two columns because I used the \multicolumn{2} command anyways.
But all of the other rows were three columns.
In the code chunk above, I set \\multicolumn in the col.name, but col.name expects three arguments.
Also, I'm not sure where I should add \\cmidrule.

@cderv
Copy link
Collaborator

cderv commented Jun 9, 2022

@cderv cderv added table feature Feature requests labels Jun 9, 2022
@cderv
Copy link
Collaborator

cderv commented Jun 9, 2022

BTW the way, you get the error because your dataframe is 3 columns, and your col.names only has 2 elements.

@amarz45
Copy link
Contributor Author

amarz45 commented Jun 9, 2022

BTW the way, you get the error because your dataframe is 3 columns, and your col.names only has 2 elements.

Yeah that's the problem: I used the \multicolumn command (which works in LaTeX), but R expects 3 elements.
Perhaps this could be solved by adding a function called multicolumn.

@amarz45
Copy link
Contributor Author

amarz45 commented Jun 10, 2022

In my pull request, I added support for cmidrules by adding an option called header which allows you to specify extra things to put before the main body of the table.
Now I just need a fix for the multicolumn issue.

@cderv cderv linked a pull request Jun 10, 2022 that will close this issue
@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests table
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants