Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to source() an external R script using Quarto?

Tags:

r

quarto

I'm trying out Quarto and 'converting' my Rmarkdown file into a quarto document. In my old Rmarkdown file, I would use source("R_datacleaning.R") and source("R_Functions.R") to run the data cleaning and to load some functions. I've also tried source(here("R_Functions.R")) but it doesn't work either. The R-scripts and the qmd files are in the same directory.

I can't seem to get that to work in the .qmd. The error I get is Error: RStudio not running. I've searched around and looked at the Quarto documentation, but couldn't find anything relating to this. I think there might be something I'm not understanding about the Quarto environment in RStudio. Would appreciate any help, thanks!

Edit:

---
title: "quarto_test2"
format: html
---

```{r}
library(here)

source(here("R_Functions.R"))
```

Error message is:

processing file: quarto_test2.qmd
  |..........                                                            |  14%
  ordinary text without R code

  |....................                                                  |  29%
label: unnamed-chunk-1
Quitting from lines 7-10 (quarto_test2.qmd) 
Error: RStudio not running

Execution halted

R_Functions.R is in the same directory as quarto_test2.qmd, as previously mentioned.

Version Info

  • RStudio version is 2022.07.2.
  • R version 4.2.0.
  • Quarto version 1.1.245.
like image 841
A. Piong Avatar asked Jul 23 '26 23:07

A. Piong


1 Answers

I was successful in pdf with

---
title: "source() external R script in Quarto, in PDF"
format: 
  pdf:
    toc: true # table of contents
---

Some text...

```{r}
#| echo: true
#| eval: false # skip evaluation
#| file: C:/directory/script.R
```

Some text...

like image 183
Yuri Gelsleichter Avatar answered Jul 26 '26 18:07

Yuri Gelsleichter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!