0

After days trying to find a solution, I give up and ask for help.

I decided to use R Markdown very recently. While I can render plots as I want, I cannot succeed in rendering my tables in a pdf doc properly.

Here the corresponding [EDITED]code:

---
title: "My_title"
output:
  pdf_document: default
  html_document:
    df_print: paged
params:
  date: "!r Sys.Date()"
---

```{r library, echo=F, message=F, warning=F, paged.print=FALSE}
suppressMessages(library("knitr"))
suppressMessages(library(reshape2))
suppressMessages(library(splines))
suppressMessages(library(kableExtra))
suppressMessages(library(gplots))
```

```{r, setup, echo = F}
opts_knit$set(root.dir = "my_path")
knitr::opts_chunk$set(echo = F)
```


```{r}
dt <- expand.grid(Region=c("a","b","c"), Country=c("d","e","f"), Cancer= c("All", "CRC", "Breast"), 
             age.1.1=1:2,
             age.1.2=1:2,
             age.1.3=1:2)

```

   ```{r Table_1, INCLUDE = TRUE}

  cancer.lab <- c("All", "CRC", "Breast")

  for (i in 1:3){

   b <- dt[dt$Cancer==cancer.lab[i],]

   b <- b[,-3]

   t <- kable(b, format = ,caption = "Fig", row.names = F) %>%
   kable_paper() %>%
   kable_styling(font_size = 9) %>%
   add_header_above(c(" " = 2, "1998" = 3))

   print(t)
  }
  ```

Again I am new and I surely miss something.

I use Mac if it may explain something.

Thank you for your help.

Sophie.

Sophie P
  • 1
  • 2
  • 1
    Don't assign your table out put to a variable. Delete the final `print` statement. In other words, `b.wide %>% kable(...) %>% kableStyling(...)` instead of `t.eapc <- b.wide %>% kable(...) ...`. – Limey Dec 11 '20 at 14:31
  • 1
    FYI, you should almost always use `library`, not `require`. The latter never stops following code when the package is not available, which is almost never what is intended. Refs: https://stackoverflow.com/a/51263513 – r2evans Dec 11 '20 at 14:54
  • Thank you, Limey. I printed the table output because it did not appear at all in the pdf created otherwise. – Sophie P Dec 11 '20 at 14:59
  • Thank you, r2evans for this information. I have to admit I have never understood the difference between the two. I will now use library. – Sophie P Dec 11 '20 at 15:00
  • You need to show us a complete document. The options you put in the "YAML" at the top and in the chunk options matter a lot. You can cut out everything else except the chunk that is supposed to generate the tables. – user2554330 Dec 11 '20 at 16:56
  • @Limey, that won't work in a loop. You need explicit print statements to get anything out. – user2554330 Dec 11 '20 at 16:57
  • @user2554330: Thank you for your reply. I edited my message with all the relevant code. Hope this will be enough. – Sophie P Dec 11 '20 at 20:33
  • The code still won't execute, because we don't have your data. Please simplify it enough that it is self-contained: you don't need to include real data, just create enough to show the problem with your tables. – user2554330 Dec 12 '20 at 10:11
  • @user2554330: Thank you for your interest in my issue. I edited the code so that it reproduces the problem with my table. By preparing the code, I noticed that the problem occurs only when there is the loop. Thank you for your help. – Sophie P Dec 14 '20 at 14:23

1 Answers1

0

I think this is the same issue as dealt with here: https://stackoverflow.com/a/53632154/2554330. The problem is that you need to use knit_print to print the tables, but you can't do that in a loop.

So if you change the last code chunk to this, it should work:

   ```{r Table_1, INCLUDE = TRUE}
  results <- c()

  cancer.lab <- c("All", "CRC", "Breast")

  for (i in 1:3){

   b <- dt[dt$Cancer==cancer.lab[i],]

   b <- b[,-3]

   t <- kable(b, format = ,caption = "Fig", row.names = F) %>%
   kable_paper() %>%
   kable_styling(font_size = 9) %>%
   add_header_above(c(" " = 2, "1998" = 3))

   results <- c(results, knit_print(t))
  }
  asis_output(results)
  ```
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Thank you for this attempt but unfortunately, this is not working. The last plot of the loop only is displayed. I tried to included asis_output within the loop but nothing is displayed. I tried several things but it seems that in any case, only the last plot is saved. – Sophie P Dec 15 '20 at 09:38
  • It works for me. It sounds as though you didn't set the `results` variable properly. Did you use `results <- c(knit_print(t))`? That's wrong. – user2554330 Dec 15 '20 at 10:49