knitr icon indicating copy to clipboard operation
knitr copied to clipboard

FR: add the option of naming the column of row.names

Open iago-pssjd opened this issue 3 years ago • 1 comments

I would like to explicit a name for the column of row.names so I try

d2 = head(mtcars)
kable(d2[, 1:5], col.names = c("car", names(d2[, 1:5])))
Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent

Maybe, another option would be allow a string for row.names as follows -kable(d2[, 1:5], row.names = "cars")-, so kable would understand it as a TRUE and further naming the column.

Thank you!

By filing an issue to this repo, I promise that

  • [x] I have fully read the issue guide at https://yihui.org/issue/.
  • [x] I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

iago-pssjd avatar Dec 14 '20 12:12 iago-pssjd

I have experienced the same situation and worked around with tibble::rownames_to_column.

d2 = head(mtcars)
d2[1:5] |>
  tibble::rownames_to_column() |>
  kable(col.names = c("car", names(d2[1:5]))

allow a string for row.names

This may be confusing because users would analogize the similar behavior among col.names and row.names.

As documented, kable is a very simple table generator. I think complex steps should come prior to kable when possible.

atusy avatar Jul 26 '22 00:07 atusy

Should be fixed now. Thanks for the report!

yihui avatar Aug 31 '23 04:08 yihui

Thank you! @yihui

iago-pssjd avatar Aug 31 '23 07:08 iago-pssjd

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[bot] avatar Feb 28 '24 05:02 github-actions[bot]