jcarlos
jcarlos

Reputation: 435

Printing gt table with rmarkdown::render when chunk code is in an external file

I have monthly report generator that works fine with gt table inside a chunk, but not when the code for the chunk has a external source like the example below.

the main script

  rmarkdown::render('report.Rmd', output_file = paste0('report_', i, '.html'))

this way the report.Rmd works fine and prints the gt table

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

## Test

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

```

```{r second,  results='asis', echo=FALSE, message=FALSE}
    #source("mtcars_gt.R")

mtcars %>% gt()
```

but this way not

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

## Test

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

```

```{r second,  results='asis', echo=FALSE, message=FALSE}
    source("mtcars_gt.R")
```

mtcars_gt.R is just the gt

mtcars %>% gt()

Upvotes: 0

Views: 361

Answers (1)

akrun
akrun

Reputation: 886958

We can use readLines

```{r code = readLines('mtcars_gt.R')}
```

Upvotes: 1

Related Questions