Table format for chucks in rmarkdown

Hello Rstudio community,
Using rmarkdown , some columns of the tables mixed up or not shown in the word document . Please see below code. Wondering if there is a global table format code to fix this. Appreciate your help.
Thanks

---
title: "Table"
author: "table width"
date: '2022-05-20'
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Table format issue

The last columns of my tables not appeared in the word document.

```{r cars}
cars$type<-as.character(c("a", "b", "c", "d", "e"))
summary(cars)

library(dplyr)
cars %>%                               
  group_by(type) %>% 
  summarize(count = sum(!is.na(speed)),
            missing= sum(is.na(speed)),
            min = min(speed,na.rm = TRUE),
            q1 = quantile(speed, 0.25, na.rm = TRUE),
            median = median(speed, na.rm = TRUE),
            mean = mean(speed, na.rm = TRUE),
            mean.sd = sd(speed, na.rm = TRUE),
            q3 = quantile(speed, 0.75, na.rm = TRUE),
            max = max(speed, na.rm = TRUE) )%>% 
              mutate(mean.se = mean.sd / sqrt(count),
                     mean.lci = mean - qnorm(0.975) * mean.se,
                     mean.uci= mean + qnorm(0.975) * mean.se)   
           

cars %>%                               
  group_by(type) %>% 
  summarize(count = sum(!is.na(dist)),
            missing= sum(is.na(dist)),
            min = min(dist,na.rm = TRUE),
            q1 = quantile(dist, 0.25, na.rm = TRUE),
            median = median(dist, na.rm = TRUE),
            mean = mean(dist, na.rm = TRUE),
            mean.sd = sd(dist, na.rm = TRUE),
            q3 = quantile(dist, 0.75, na.rm = TRUE),
            max = max(dist, na.rm = TRUE) )%>% 
  mutate(mean.se = mean.sd / sqrt(count),
         mean.lci = mean - qnorm(0.975) * mean.se,
         mean.uci= mean + qnorm(0.975) * mean.se)   

```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```
1 Like

Hello,

Please provide an example so that we are able to help you.

What did you try already ?

FAQ: How to Format R Markdown Source

Hi cderv,
Thank you for the reply and help. I added the example if you please can help me with that.
Appreciate it.
Thank you again.

Thanks can you look at the second link I put for correct formatting ?
Thanks
FAQ: How to Format R Markdown Source

Hi again, I corrected the rmarkdown formatting. Please look at it now if you can help. Thanks

Can you described the issue in more details ? Your data.frame is currently printing in the word document exactly as it does in R console.

Hi again, when I run the code, the last columns of the tables such as "mean.lci" and "mean.uci " do not show up in the word document. I need help with a global auto formatting for the tables that completely show in the word/pdf outputs. Thanks

It seems you did not try at all to format your table. You should use a table package that would help format your table as you want.

Some helps in this chapter

And for word output, you can have a look at flextable package

I let you read the documentation and try out.

Hi again, This document did not help me. I have several summary tables (similar to above code) and I just want to see the output completely (see below output) in word or pdf outputs . I miss some columns in rmarkdown output. Thanks

# A tibble: 5 × 13
  type  count missing   min    q1 median  mean mean.sd    q3   max mean.se mean.lci mean.uci
  <chr> <int>   <int> <dbl> <dbl>  <dbl> <dbl>   <dbl> <dbl> <dbl>   <dbl>    <dbl>    <dbl>
1 a        10       0     2  26.5     36  36.4    20.9  51.5    70    6.61     23.5     49.3
2 b        10       0    10  21.5     38  40.4    24.9  53.5    92    7.86     25.0     55.8
3 c        10       0     4  28       48  48.5    28.3  67      93    8.96     30.9     66.1
4 d        10       0    20  26       33  47.2    31.7  61     120   10.0      27.6     66.8
5 e        10       0    16  26       34  42.4    25.4  52.5    85    8.03     26.7     58.1

So you do not want to format the table ? you just want raw tibble output like this

## # A tibble: 5 × 13
##   type  count missing   min    q1 median  mean mean.sd    q3   max mean.se
##   <chr> <int>   <int> <dbl> <dbl>  <dbl> <dbl>   <dbl> <dbl> <dbl>   <dbl>
## 1 a        10       0     4  11.5   14.5  14.6    5.80  18.5    24    1.83
## 2 b        10       0     4  12.2   15    15      5.70  18.8    24    1.80
## 3 c        10       0     7  12.2   15    15.3    5.10  18.8    24    1.61
## 4 d        10       0     7  12.2   16    15.8    5.41  19.5    24    1.71
## 5 e        10       0     8  12.5   16    16.3    5.38  19.5    25    1.7 
## # … with 2 more variables: mean.lci <dbl>, mean.uci <dbl>

but with the 2 missing columns ?

They are hidden because there is no more width left on the page and they don't fit.
The printing behavior is detailed here:

changing the width option could help, but I don't think this will fit on the page if the table you want to print has too many column. That is why in R Markdown there are different options. See

Hi, not much luck, only I put the below code at the top and it helped in different way.

knit_print.data.frame = function(x, ...) {
  res = paste(c("", "", knitr::kable(x)), collapse = "\n")
  knitr::asis_output(res)
}

registerS3method(
  "knit_print", "data.frame", knit_print.data.frame,
  envir = asNamespace("knitr")
)

By doing that, you are using kable to format your table. If the table is too big, then you won't have the spaces on page.

You can try different Table packages to get the output you need. You have to read the documentation, and configure to suits your output. Default behaviour will produce not ideal table probably, but that works no issue

Example code
---
title: "Table"
author: "table width"
date: '2022-05-20'
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Table format issue

The last columns of my tables not appeared in the word document.

```{r cars}
cars$type<-as.character(c("a", "b", "c", "d", "e"))
summary(cars)

library(dplyr)
tabs <- cars %>%                               
  group_by(type) %>% 
  summarize(count = sum(!is.na(speed)),
            missing= sum(is.na(speed)),
            min = min(speed,na.rm = TRUE),
            q1 = quantile(speed, 0.25, na.rm = TRUE),
            median = median(speed, na.rm = TRUE),
            mean = mean(speed, na.rm = TRUE),
            mean.sd = sd(speed, na.rm = TRUE),
            q3 = quantile(speed, 0.75, na.rm = TRUE),
            max = max(speed, na.rm = TRUE) )%>% 
              mutate(mean.se = mean.sd / sqrt(count),
                     mean.lci = mean - qnorm(0.975) * mean.se,
                     mean.uci= mean + qnorm(0.975) * mean.se)   
```

```{r}
tabs
```

```{r}
knitr::kable(tabs)
```

```{r}
flextable::flextable(tabs)
```

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.