Hi, I have an issue with this markdown code.
I have tried different options but the word document does not show the tables I expect.
Here is the reprex of the Markdown document .
The codes in chunks iris.3 to iris.5 works when I run the chuck but don't work when I knit it to Word.
They are different options I have seen:
- in chunk iris.3: walk()
- in chunk iris.4: flextable_to_rmd()
- in chunk iris.5: knit_print()
I will add the reprex of the individual chunks afterwards.
Many thanks in advance for any help.
---
title: "example"
author: "Beatriz"
date: "`r Sys.Date()`"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(rstatix)
library(tidyverse)
library(tidyxl)
library(lubridate)
library(skimr)
library(flextable)
```
1. This example works
```{r iris.1}
iris %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
```
2. This example works
```{r iris.2}
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
tabulate_by_spp("setosa") # works
```
3. This example works when I run the chuck in R but the Word document does not show the tables.
It uses **walk()** which i have used successfully to print plots (ggplots) using very similar functions.
```{r iris.3, results='asis'}
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
plots <-
levels(iris$Species) %>%
map(tabulate_by_spp)
plots %>%
walk(print)
```
4. This example works when I run the chuck in R but the Word document does not show the tables.
It uses **flextable_to_rmd()** instead of **walk()**
```{r iris.4, results='asis'}
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
plots <-
levels(iris$Species) %>%
map(tabulate_by_spp)
flextable_to_rmd(plots)
```
5. This example works when I run the chuck in R but the Word document does not show the tables.
It uses **knit_print()** instead of **walk()**
```{r iris.5, results='asis'}
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
plots <-
levels(iris$Species) %>%
map(tabulate_by_spp)
knit_print(plots)
```
#> Error in parse(text = input): attempt to use zero-length variable name
Created on 2025-12-02 with reprex v2.1.1
Individual chunks:
iris.1. This example works:
library(knitr)
library(rstatix)
#>
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#>
#> filter
library(tidyverse)
library(tidyxl)
library(lubridate)
library(skimr)
library(flextable)
#> Warning: package 'flextable' was built under R version 4.4.3
#>
#> Attaching package: 'flextable'
#> The following object is masked from 'package:purrr':
#>
#> compose
iris %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)

Created on 2025-12-02 with reprex v2.1.1
iris.2. This example works
library(knitr)
library(rstatix)
#>
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#>
#> filter
library(tidyverse)
library(tidyxl)
library(lubridate)
library(skimr)
library(flextable)
#> Warning: package 'flextable' was built under R version 4.4.3
#>
#> Attaching package: 'flextable'
#> The following object is masked from 'package:purrr':
#>
#> compose
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
tabulate_by_spp("setosa") # works

Created on 2025-12-02 with reprex v2.1.1
iris.3. This example works when I run the chuck in R but the Word document does not show the tables.
It uses walk() which i have used successfully to print plots (ggplots) using very similar functions.
library(knitr)
library(rstatix)
#>
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#>
#> filter
library(tidyverse)
library(tidyxl)
library(lubridate)
library(skimr)
library(flextable)
#> Warning: package 'flextable' was built under R version 4.4.3
#>
#> Attaching package: 'flextable'
#> The following object is masked from 'package:purrr':
#>
#> compose
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
plots <-
levels(iris$Species) %>%
map(tabulate_by_spp)
plots %>%
walk(print)
#> a flextable object.
#> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> header has 1 row(s)
#> body has 1 row(s)
#> original dataset sample:
#> 'data.frame': 1 obs. of 8 variables:
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1
#> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> $ n : num 50
#> $ min : num 4.3
#> $ max : num 5.8
#> $ q1 : num 4.8
#> $ median : num 5
#> $ q3 : num 5.2
#> a flextable object.
#> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> header has 1 row(s)
#> body has 1 row(s)
#> original dataset sample:
#> 'data.frame': 1 obs. of 8 variables:
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2
#> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> $ n : num 50
#> $ min : num 4.9
#> $ max : num 7
#> $ q1 : num 5.6
#> $ median : num 5.9
#> $ q3 : num 6.3
#> a flextable object.
#> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> header has 1 row(s)
#> body has 1 row(s)
#> original dataset sample:
#> 'data.frame': 1 obs. of 8 variables:
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 3
#> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> $ n : num 50
#> $ min : num 4.9
#> $ max : num 7.9
#> $ q1 : num 6.22
#> $ median : num 6.5
#> $ q3 : num 6.9
Created on 2025-12-02 with reprex v2.1.1
iris.4. This example works when I run the chuck in R but the Word document does not show the tables.
It uses flextable_to_rmd() instead of walk()
library(knitr)
library(rstatix)
#>
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#>
#> filter
library(tidyverse)
library(tidyxl)
library(lubridate)
library(skimr)
library(flextable)
#> Warning: package 'flextable' was built under R version 4.4.3
#>
#> Attaching package: 'flextable'
#> The following object is masked from 'package:purrr':
#>
#> compose
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
plots <-
levels(iris$Species) %>%
map(tabulate_by_spp)
flextable_to_rmd(plots)
#>
#>
#> ```
#> #> [[1]]
#> #> a flextable object.
#> #> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> #> header has 1 row(s)
#> #> body has 1 row(s)
#> #> original dataset sample:
#> #> 'data.frame': 1 obs. of 8 variables:
#> #> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1
#> #> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> #> $ n : num 50
#> #> $ min : num 4.3
#> #> $ max : num 5.8
#> #> $ q1 : num 4.8
#> #> $ median : num 5
#> #> $ q3 : num 5.2
#> #>
#> #> [[2]]
#> #> a flextable object.
#> #> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> #> header has 1 row(s)
#> #> body has 1 row(s)
#> #> original dataset sample:
#> #> 'data.frame': 1 obs. of 8 variables:
#> #> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2
#> #> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> #> $ n : num 50
#> #> $ min : num 4.9
#> #> $ max : num 7
#> #> $ q1 : num 5.6
#> #> $ median : num 5.9
#> #> $ q3 : num 6.3
#> #>
#> #> [[3]]
#> #> a flextable object.
#> #> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> #> header has 1 row(s)
#> #> body has 1 row(s)
#> #> original dataset sample:
#> #> 'data.frame': 1 obs. of 8 variables:
#> #> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 3
#> #> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> #> $ n : num 50
#> #> $ min : num 4.9
#> #> $ max : num 7.9
#> #> $ q1 : num 6.22
#> #> $ median : num 6.5
#> #> $ q3 : num 6.9
#> #>
#> #> $caption
#> #> $caption$word_stylename
#> #> [1] "Table Caption"
#> ```
Created on 2025-12-02 with reprex v2.1.1
iris.5. This example works when I run the chuck in R but the Word document does not show the tables.
It uses knit_print() instead of walk()
library(knitr)
library(rstatix)
#>
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#>
#> filter
library(tidyverse)
library(tidyxl)
library(lubridate)
library(skimr)
library(flextable)
#> Warning: package 'flextable' was built under R version 4.4.3
#>
#> Attaching package: 'flextable'
#> The following object is masked from 'package:purrr':
#>
#> compose
tabulate_by_spp <- function(Species_name) {
table_by_spp <-
iris %>%
filter(Species == Species_name) %>%
group_by(Species) %>%
get_summary_stats(Sepal.Length, type = "five_number")%>% # this is necessary to print as image
flextable::flextable() %>% # convert to image
flextable::autofit() %>%
flextable::fit_to_width(max_width = 7) # Fits the table to max width for Word(R)
return(table_by_spp)
}
plots <-
levels(iris$Species) %>%
map(tabulate_by_spp)
knit_print(plots)
#> [[1]]
#> a flextable object.
#> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> header has 1 row(s)
#> body has 1 row(s)
#> original dataset sample:
#> 'data.frame': 1 obs. of 8 variables:
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1
#> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> $ n : num 50
#> $ min : num 4.3
#> $ max : num 5.8
#> $ q1 : num 4.8
#> $ median : num 5
#> $ q3 : num 5.2
#>
#> [[2]]
#> a flextable object.
#> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> header has 1 row(s)
#> body has 1 row(s)
#> original dataset sample:
#> 'data.frame': 1 obs. of 8 variables:
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2
#> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> $ n : num 50
#> $ min : num 4.9
#> $ max : num 7
#> $ q1 : num 5.6
#> $ median : num 5.9
#> $ q3 : num 6.3
#>
#> [[3]]
#> a flextable object.
#> col_keys: `Species`, `variable`, `n`, `min`, `max`, `q1`, `median`, `q3`
#> header has 1 row(s)
#> body has 1 row(s)
#> original dataset sample:
#> 'data.frame': 1 obs. of 8 variables:
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 3
#> $ variable: Factor w/ 1 level "Sepal.Length": 1
#> $ n : num 50
#> $ min : num 4.9
#> $ max : num 7.9
#> $ q1 : num 6.22
#> $ median : num 6.5
#> $ q3 : num 6.9
Created on 2025-12-02 with [reprex v2.1.1]