Creating pander tables in a loop in RMarkdown in Rstudio

I have a set of tables that I generate using code similar to the snippet below:

```{r tables, echo=TRUE, eval=TRUE} require(reshape2); require(pander) data("mtcars") data("AirPassengers") dta_lst <- list(mtcars, AirPassengers) for (i in 1:length(dta_lst)) { pander(melt(head(dta_lst[[i]], n = 2), varnames = c("Something"), value.name = "Something else"), caption = paste("Some table for: ", class(dta_lst[[i]]))) } ``` 

When I run the code, it produces the desired result (naturally, the provided example does not make sense, according to my data I am melting the data in a reasonable way):

 --------------------------- variable Something else ---------- ---------------- mpg 21 mpg 21 cyl 6 cyl 6 disp 160 disp 160 hp 110 hp 110 drat 3.9 drat 3.9 wt 2.62 wt 2.875 qsec 16.46 qsec 17.02 vs 0 vs 0 am 1 am 1 gear 4 gear 4 carb 4 carb 4 --------------------------- Table: Some table for: data.frame ---------------- Something else ---------------- 112 118 ---------------- Table: Some table for: ts 

When I try to knit code in Rstudio, pander tables are not displayed:

absent tables

Naturally, without a loop, the pander command works just fine and generates tables that are neatly knitted into an HTML document.

+5
source share
1 answer

There is no "output screen" in the for loop unless you use the print(x) function.

for (i in 1:4) { i } doesn’t display anything

for (i in 1:4) {print(i)} displays the numbers 1 2 3 and 4

Decision. In a FOR loop, create a table (using knitt) and assign it to a variable. Then print this variable using the print() function. Remember. You must add empty lines after and before the var table: use the insert function inside print()

+1
source

All Articles