4

When I trying print table with knitr::kable function "id" word apperas in the column names. How can I change it?

Example:

> x <- structure(c(42.3076923076923, 53.8461538461538, 96.1538461538462,
                   2.56410256410256, 1.28205128205128, 3.84615384615385,
                   44.8717948717949, 55.1282051282051, 100),
                 .Dim = c(3L, 3L),
                 .Dimnames = structure(list(Condition1 = c("Yes", "No", "Sum"),
                                            Condition2 = c("Yes", "No", "Sum")),
                 .Names = c("Condition1", "Condition2")), class = c("table", "matrix"))
> print(x)
          Condition2
Condition1    Yes     No    Sum
       Yes  42,31   2,56  44,87
       No   53,85   1,28  55,13
       Sum  96,15   3,85 100,00
> library(knitr)
> kable(x)
|id   |   Yes|    No|    Sum|
|:----|-----:|-----:|------:|
|Yes  |  42,3|  2,56|   44,9|
|No   |  53,8|  1,28|   55,1|
|Sum  |  96,2|  3,85|  100,0|

Edit: I find reason of this behavior in the knitr:::kable_mark function. But now I not understand how to make it more flexible.

Artem Klevtsov
  • 9,193
  • 6
  • 52
  • 57

2 Answers2

2

An alternative to kable might be the general S3 method of pander:

> library(pander)
> pander(x, style = 'rmarkdown')


|  &nbsp;   |  Yes  |  No   |  Sum  |
|:---------:|:-----:|:-----:|:-----:|
|  **Yes**  | 42.31 | 2.564 | 44.87 |
|  **No**   | 53.85 | 1.282 | 55.13 |
|  **Sum**  | 96.15 | 3.846 | 100   |

If you need to set the decimal mark to comma, then set the relevant option before and use that in your R session:

> panderOptions('decimal.mark', ',')
> pander(x, style = 'rmarkdown')


|  &nbsp;   |  Yes  |  No   |  Sum  |
|:---------:|:-----:|:-----:|:-----:|
|  **Yes**  | 42,31 | 2,564 | 44,87 |
|  **No**   | 53,85 | 1,282 | 55,13 |
|  **Sum**  | 96,15 | 3,846 | 100   |

There are also some other possible tweaks: http://rapporter.github.io/pander/#pander-options

daroczig
  • 28,004
  • 7
  • 90
  • 124
1

I think the easiest way is to rip out and replace kable_mark completely. Note: this is quite dirty – but it seems to work, and there is no current way to customise how kable_mark works (you could submit a patch to knitr though).

km <- edit(knitr:::kable_mark)
# Now edit the code and remove lines 7 and 8.

unlockBinding('kable_mark', environment(knitr:::kable_mark))
assign('kable_mark', km, envir=environment(knitr:::kable_mark))

Explanation: First we edit the function and store the amended definition in a temporary variable. We remove the two lines

if (grepl("^\\s*$", cn[1L])) 
    cn[1L] = "id"

… of course you can also hard-code the amended function rather than editing it, or change the function around completely.

Next we use unlockBinding to make knitr:::kable_mark overridable. If we don’t do this, the next assign command wouldn’t work.

Finally, we assign the patched function back to knitr:::kable_mark. Done.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks. I thought about something like that. But I want get a flixible argument like `title` in the `Hmisc::latex` function. – Artem Klevtsov Nov 09 '13 at 16:09
  • @unikum Unfortunately `kable` and `kable_markdown` do not pass through any arguments to `kable_mark` so there’s no direct way of making this configurable. Of course you could replace *all* these functions to achieve this, in the same way as shown in my answer. Alternatively, you could replace the `'id'` string by a `getOption` call inside `kable_mark` and set the desired column name via `option(…)`. However, I wouldn’t call that a good solution. – Konrad Rudolph Nov 09 '13 at 16:13
  • 1
    As you said, this is dirty, and should be the last resort, after the package author refuses to cooperate, which is [rarely the case](https://github.com/yihui/knitr/pulls?direction=desc&page=1&sort=created&state=closed) in knitr's development :) – Yihui Xie Nov 10 '13 at 07:51