Adjusting width of tables made with kable() in RMarkdown documents

前端 未结 4 1120
刺人心
刺人心 2020-12-08 14:30

Is it possible to adjust the width of columns when making tables with the kable() function in knitr?

A chunk like this for a table with two columns produces a table

4条回答
  •  时光取名叫无心
    2020-12-08 15:06

    My new favorite thing is the flextable package because 1) the tables are beautiful, 2) you can more easily control things like width and font size, and 3) they render well to HTML and Word. Using your data:

    ```{r}
    library(flextable)
    library(magrittr)
    df <- data.frame(x = 1:10, y = 11:20)
    df %>% regulartable() %>% autofit() %>% 
    width(j=~x,width=1) %>% width(j=~y,width=1)
    ```
    

    With both values of width set to 1, the columns are close together:

    If you adjust the last line to width(j=~x,width=2) %>% width(j=~y,width=2) the table looks like this:

提交回复
热议问题