Is it possible to include code from an external R script in an .Rmd and simultaneously run the code, display the code, and display its results in the output .HTML file? For example, if I have
x <- 1
y <- 3
z <- x + y
z
in external.R
. In the output document I want to see the code above along with the result of z
, i.e. 4. Essentially, I want the equivalent of what would happen if I copy/pasted what's above in an R chunk. So I want
```{r}
some.library::some.function("external.R")
```
to be the equivalent of
```{r}
x <- 1
y <- 3
z <- x + y
z
```
In the output HTML file.
I've tried things like knitr::read_chunk('external.R)
and source('external.R
)`, but these don't display the code. Am I missing something simple?
EDIT
I found that source('external.R', echo = TRUE)
will produce what I ask, but each line of the output's displayed code/results is prepended by ##
. Any way to make it look like it would if the code was simply copy/pasted in a chunk in the .Rmd?
source("my_script.R", echo = T)
I don't see any code/results prefixed by##
. It looks exactly how it would look if I were to copy&paste code into an R terminal. Perhaps I misunderstood? – Maurits Everssource('external.R', echo = T)
, the output HTML file has##
in front of each line. If I were to simply copy/paste the contents of my external R code into a code chunk in the .Rmd, it renders without the##
. Does that make sense? My comment about the result is the terminal was in reference to your first comment about what the result looks like in an R terminal. I don't really care about that - I only care about how it looks in the output HTML file. – haff