Home > Back-end >  Printing flextables from a list of lists in R-markdown
Printing flextables from a list of lists in R-markdown

Time:12-16

I have example data as follows:

library(data.table)
library(flextable)
datA <- fread("anyvar somevar
               2  4
               2  3
               2  5")

dat_list <- list("one" = list("datA" = datA, "datB" = 2*datA),"two" = list("datC" = 3*datA, "datD" = 4*datA), "three" = list("datE" = 5*datA, "datF" = 6*datA))

for (i in seq_along(dat_list) ) {
  for (j in seq_along( dat_list[[i]] ) ) {
    dat_list[[i]][[j]] <- flextable( dat_list[[i]][[j]] )
  }
}

I want to print out part of this list in R-markdown. In R I can just use the print function in a for loop:

 for (j in seq_along(dat_list[[2]])) {
    print(dat_list[[2]][[j]])
 }

However in R-markdown I cannot. I tried using print and return, but it is not giving me the right output (print does not print a flextable, and return, returns only the first table). How should I do this? :

---
output: word_document
---

My example data:

```{r}

library(data.table)
library(flextable)
datA <- fread("anyvar somevar
               2  4
               2  3
               2  5")

dat_list <- list("one" = list("datA" = datA, "datB" = 2*datA),"two" = list("datC" = 3*datA, "datD" = 4*datA), "three" = list("datE" = 5*datA, "datF" = 6*datA))


for (i in seq_along(dat_list) ) {
  for (j in seq_along( dat_list[[i]] ) ) {
    dat_list[[i]][[j]] <- flextable( dat_list[[i]][[j]] )
  }
}

```

Print function

```{r, echo=FALSE}

 for (j in seq_along(dat_list[[2]])) {
    print(dat_list[[2]][[j]])
    cat("\n\newpage\n")
 }
```

Return function

```{r, echo=FALSE}

 for (j in seq_along(dat_list[[2]])) {
    return(dat_list[[2]][[j]])
    cat("\n\newpage\n")
 }
```

CodePudding user response:

Based on enter image description here

  • Related