I have recently tried to use the kable package for tables, and while I'm quite satisfied of the results I'm getting in R scripts, I can't figure out how to use them in an R Markdown file.
Here is a short example of a table that does work in a R script, but won't when I try to reproduce it in a Markdown document.
data(mtcars)
mtcars
## @knitr install
check_and_install <- function( packname ) { # given package name, check installation, install if not found
if ( packname %in% rownames(installed.packages()) == FALSE ) {
install.packages( packname, repos="http://cran.rstudio.com/" )
}
}
check_and_install("kableExtra")
check_and_install("dplyr")
check_and_install("qwraps2")
check_and_install("reprex")
library(kableExtra)
library(dplyr)
library(qwraps2)
library(reprex)
## Tableau
summary_test <-
list("Cylindres" =
list("Huit" = ~ qwraps2::n_perc0(cyl == 8,show_symbol=TRUE),
"Six" = ~ qwraps2::n_perc0(cyl == 6,show_symbol=TRUE),
"Quatre" = ~ qwraps2::n_perc0(cyl == 4,show_symbol=TRUE)),
"Vitesses" =
list("Cinq" = ~ qwraps2::n_perc0(gear == 5,show_symbol=TRUE),
"Quatre" = ~ qwraps2::n_perc0(gear == 4,show_symbol=TRUE),
"Trois" = ~ qwraps2::n_perc0(gear == 3,show_symbol=TRUE))
)
tabtest2<-summary_table(dplyr::group_by(mtcars, am), summary_test)
kable_out <- kable(tabtest2, format = "html", caption = "", col.names=c("Auto","Manuelle"), booktabs = T, full_width = F) %>%
kable_styling(bootstrap_options = c("striped", "hover")) %>%
kableExtra::group_rows("Cylindres", 1, 3) %>%
kableExtra::group_rows("Vitesses", 4, 6)
kable_out
Now as for the next step, I want to include this tab in a R-markdown document, ideally the output would be a word file. And this is where I'm having problems : I can't find a way to include it properly. Markdown completely messes up the formatting in the word output. Please note that there is no issue if I switch the output to HTML... unfortunately I have to provide a word document for now so that's not an option.
---
title: "Test2"
author: "MJ"
date: "14 mars 2019"
output: word_document
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(cache = FALSE, include = FALSE)
library(knitr)
opts_chunk$set(echo = FALSE, cache=FALSE)
read_chunk('C:/Users/Mathieu/Desktop/indicateurs pps/Test SO.R')
```
```{r install, include=FALSE}
```
## Analyse comparative
```{r table1, include=T}
knitr::kable(tabtest2, format = "html", caption = "", col.names=c("Auto","Manuelle"), booktabs = T, full_width = F) %>%
kable_styling(bootstrap_options = c("striped", "hover")) %>%
kableExtra::group_rows("Cylindres", 1, 3) %>%
kableExtra::group_rows("Vitesses", 4, 6)
kable_out
```
No issues at all when creating the document, everything seems to run smoothly...
I've been looking into it for a while and I just can't figure out what's wrong. Any leads someone ?
Thanks in advance