Tables type GT not printing inside loop in Rmarkdown

Asked

Viewed 78 times

1

I’m programming a report generator that exports html. I use two files, the first is a loop that will determine the number of reports and within my template. Rmd I have another loop to print several tables.

The . rmd below works well

---
title: "Report"
author: "Me"
date: "`r format(Sys.time(), '%d de %B de %Y')`"
output:
  html_document
---

# First Section    

{r , results='asis', echo=FALSE, message=FALSE}
library(tidyverse)
library(gt)

i=1

df <- cbind(m=c(rep(1,16),rep(2,16)),mtcars)

gears <- unique(df$gear)

for(g in gears)
{
  cat("## gear", g, "\n")
  
  print(
  df %>% filter(gear==g & m==i) %>%
    gt()
  )
}

The way out

saida com tabelas

but your i Gero report from the R script the tables are not printed

library(tidyverse)
library(gt)

df <- cbind(m=c(rep(1,16),rep(2,16)),mtcars)

for (i in 1:2) 
{

  rmarkdown::render(
    'report_template_2.Rmd', output_file = paste0("report", 
                                                    "_",i, 
                                                  '.html'), encoding="UTF-8")
}

saida sem tabelas

In time, other printouts of tables using the GT package to the script logo are printed normally.

Any idea what might be going on?

2 answers

3

Rscript does not locate Pandoc, you need to set in the execution file. Check the pandoc directory with the function Sys.getenv("RSTUDIO_PANDOC")

I made some simplifications in the example.

Rscript cron.R

cron.R

Sys.setenv(RSTUDIO_PANDOC='/usr/lib/rstudio/bin/pandoc')
for (i in 1:2){
  rmarkdown::render('report.Rmd', 
  output_file = paste0("report","_", i, '.html'), encoding="UTF-8")
}

report.Rmd

---
title: "Report"
author: "Me"
date: "`r format(Sys.time(), '%d de %B de %Y')`"
output:
  html_document
---

# First Section    

```{r , results='asis', echo=FALSE, message=FALSE}
library(tidyverse)
library(gt)

df <- cbind(m=c(rep(1,16),rep(2,16)),mtcars)

gears <- unique(df$gear)

for(g in gears){
  cat("<h2><b>gear", g, "</b></h2>")
  df %>% filter(gear==g) %>% gt() %>% print
}
```
  • So, there was a breakthrough, but it didn’t solve, now I see the table gt() in the View tab, but it’s still not generated in html. I found the Pandoc directory here in Sys.setenv(RSTUDIO_PANDOC='C:/Program Files/Rstudio/bin/pandoc'), la só isso >>> <p># First Section</p> <H2> <b>Gear 4 </b> </H2> <H2> <H2> <b> </H2> <H2> < <b>Gear 5 </b> </H2<>

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.