benjaminrich / table1

79 stars 26 forks source link

table not rendering in for loop #85

Closed vforget closed 2 years ago

vforget commented 2 years ago

Hi,

Wonderful R package! I want to render multiple tables in the same document using a for loop. I noticed that the table doesn't render inside a for loop (see below). It renders if you enclose the table call in a print statement.

---
title: Gapminder data (test table1)
output:
  html_document
---

\```{r echo=F}
suppressPackageStartupMessages({
    library(table1)
    library(gapminder)
})
label(gapminder$continent) <- "Continent"
label(gapminder$lifeExp)   <- "Life expectency at birth (years)"
label(gapminder$gdpPercap) <- "GDP per capita (US$, inflation-adjusted)"
\```

Table 1:
\```{r echo=F, results='asis'}
for (i in c(1)){
table1(~ continent + lifeExp + gdpPercap, data=gapminder)
}
\```

Table 2:
\```{r echo=F, results='asis'}
for (i in c(1)){
print(table1(~ continent + lifeExp + gdpPercap, data=gapminder))
}
\```

Table3:
\```{r echo=F, results='asis'}

table1(~ continent + lifeExp + gdpPercap, data=gapminder)
\```
benjaminrich commented 2 years ago

Yes, this is expected behavior. A lot of R packages work like that, i.e., the object won't be displayed until it is printed, which happens automatically at the top level, but must be done explicitly inside loops.