Column alignment with knitr kable function

I perform a simple task: create a table and display it with R Markdownto pdfas follows:

library(knitr)
kable(datatable,align='ccccccc',
col.names = c("Copy","Sigma Est","Sigma Lower","Sigma Upper", 
"Lambda Est","Lambda Lower","Lambda Upper"),digits=3)

Problem

when I output the table, the columns are not centered. In fact, for some tables, they are right-aligned for others - left-justification, which seems pretty random to me.

Question

How can I control column alignment using a function R kablefrom a package knitr, i.e. what am I doing wrong?

EDIT

Duration:

library(knitr)
kable(datatable,align=c(rep('c',times=7)),
col.names = c("Copy","Sigma Est","Sigma Lower","Sigma Upper", 
"Lambda Est","Lambda Lower","Lambda Upper"),digits=3)

Productivity:

| Copy | Sigma Est | Sigma Lower | Sigma Upper | Lambda Est | Lambda Lower | Lambda Upper |
|:----:|:---------:|:-----------:|:-----------:|:----------:|:------------:|:------------:|
|  0   |  14.631   |   12.275    |   16.987    |   0.129    |    8.778     |    9.296     |
|  1   |  16.988   |   14.275    |   19.700    |   0.136    |    8.190     |    8.736     |
|  2   |  20.850   |   17.517    |   24.183    |   0.129    |    8.595     |    9.113     |
|  3   |  20.551   |   17.229    |   23.874    |   0.127    |    9.015     |    9.523     |
|  4   |  22.651   |   18.993    |   26.310    |   0.127    |    8.969     |    9.478     |
|  5   |  23.369   |   19.652    |   27.086    |   0.127    |    8.599     |    9.108     |

This is exactly what I want, as it :---:denotes the centering of the columns, however, when I click Knit PDFand the document is created pdf, all the columns are still left-aligned. How do I get around this?

+13
2

kable , . ,

: , "l" (), "c" ( ) / "r" (); , ; align = NULL, .

.

- , , .

library(knitr)

kable(head(mtcars[1:5]))

|                  |  mpg| cyl| disp|  hp| drat|
|:-----------------|----:|---:|----:|---:|----:|
|Mazda RX4         | 21.0|   6|  160| 110| 3.90|
|Mazda RX4 Wag     | 21.0|   6|  160| 110| 3.90|
|Datsun 710        | 22.8|   4|  108|  93| 3.85|
|Hornet 4 Drive    | 21.4|   6|  258| 110| 3.08|
|Hornet Sportabout | 18.7|   8|  360| 175| 3.15|
|Valiant           | 18.1|   6|  225| 105| 2.76|

, , .

kable(head(mtcars[1:5]), align=rep('c', 5))


|                  | mpg  | cyl | disp | hp  | drat |
|:-----------------|:----:|:---:|:----:|:---:|:----:|
|Mazda RX4         | 21.0 |  6  | 160  | 110 | 3.90 |
|Mazda RX4 Wag     | 21.0 |  6  | 160  | 110 | 3.90 |
|Datsun 710        | 22.8 |  4  | 108  | 93  | 3.85 |
|Hornet 4 Drive    | 21.4 |  6  | 258  | 110 | 3.08 |
|Hornet Sportabout | 18.7 |  8  | 360  | 175 | 3.15 |
|Valiant           | 18.1 |  6  | 225  | 105 | 2.76 |

, .Rmd, , pdf.

---
title: "Untitled"
output: pdf_document
---

this thing

```{r table1, as.is=TRUE}
library(knitr)

kable(head(mtcars[1:5]))

```


is not a centered table, while this thing

```{r table2, as.is=TRUE}

kable(head(mtcars[1:5]), align=rep('c', 5))

```

is a centered table.
+19

, / . mtcars .

 kable(mtcars, align=rep('l', length(mtcars[,1])))
0

Source: https://habr.com/ru/post/1665138/


All Articles