Reputation: 2800
I am writing a paper in Rmarkdown about an ongoing project. I have my own .Rmd
file where I am writing it.
At the same time, I have several scripts in R stored in different files with the extension .R
.
In different parts of the paper I need to describe what it is in those R scripts, so that I need to embed the codes of the scripts in the Rmarkdown file without running it.
To summarize:
I tried this chunk with no success:
```{r eval=F}
source("script1.R")
Upvotes: 0
Views: 556
Reputation: 24888
One option would be to readLines
on the script instead of sourcing it.
Consider this trivial R script:
writeLines("foo <- function(x) x + 2", con = "foo.R")
system("cat foo.R")
# foo <- function(x) x + 2
Instead of using source
use readLines
.
exp <- readLines("foo.R")
Now you have the text of the Rscript. You could use cat
to print it.
cat(exp)
#foo <- function(x) x + 2
Or you could evaluate it.
eval(parse(text=exp))
foo(2)
#[1] 4
Upvotes: 2