Include code from an external R script, run in, display both code and output
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
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?
r r-markdown knitr
 |Â
show 1 more comment
up vote
6
down vote
favorite
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?
r r-markdown knitr
1
I guess my first question is: Why? What are you trying to do? Secondly, if I dosource("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 Evers
4 hours ago
We'll, I'm not comparing the result to what it would look like in in an R terminal. I'm comparing it to how it looks in an output .HTML file.
– haff
4 hours ago
1
I'm giving my students an assignment where they have to complete several lengthy tasks, and for organizational purposes, I think it's easier to use external scripts rather than put everything in multiple code chunks. Also, if they execute code line-by-line in a code chunk in a .Rmd, Rstudio puts the output in a "drawer" under the chunk, which is annoying to me, as I think it makes more sense for it to appear in the console like it does in a simple .R file.
– haff
4 hours ago
1
I'm not quite following. You seem to be mixing up RMarkdown and straight-up R files. If you don't like the way RMarkdown renders code&text, don't use RMarkdown. But then you are saying that you are "not comparing the result to what it would look like in in an R terminal." So what are you actually trying to do and how are you executing code? In a R terminal? In RStudio? Is code in an RMarkdown file? Or in an R script. I've had my students very successfully use RMarkdown and R Notebooks. In my opinion it encourages transparent code documentation and consistent coding standards.
– Maurits Evers
4 hours ago
I love the way Rmarkdown renders code and text. What I'm saying is that if I usesource('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
3 hours ago
 |Â
show 1 more comment
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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?
r r-markdown knitr
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?
r r-markdown knitr
r r-markdown knitr
edited 4 hours ago
asked 4 hours ago


haff
15028
15028
1
I guess my first question is: Why? What are you trying to do? Secondly, if I dosource("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 Evers
4 hours ago
We'll, I'm not comparing the result to what it would look like in in an R terminal. I'm comparing it to how it looks in an output .HTML file.
– haff
4 hours ago
1
I'm giving my students an assignment where they have to complete several lengthy tasks, and for organizational purposes, I think it's easier to use external scripts rather than put everything in multiple code chunks. Also, if they execute code line-by-line in a code chunk in a .Rmd, Rstudio puts the output in a "drawer" under the chunk, which is annoying to me, as I think it makes more sense for it to appear in the console like it does in a simple .R file.
– haff
4 hours ago
1
I'm not quite following. You seem to be mixing up RMarkdown and straight-up R files. If you don't like the way RMarkdown renders code&text, don't use RMarkdown. But then you are saying that you are "not comparing the result to what it would look like in in an R terminal." So what are you actually trying to do and how are you executing code? In a R terminal? In RStudio? Is code in an RMarkdown file? Or in an R script. I've had my students very successfully use RMarkdown and R Notebooks. In my opinion it encourages transparent code documentation and consistent coding standards.
– Maurits Evers
4 hours ago
I love the way Rmarkdown renders code and text. What I'm saying is that if I usesource('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
3 hours ago
 |Â
show 1 more comment
1
I guess my first question is: Why? What are you trying to do? Secondly, if I dosource("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 Evers
4 hours ago
We'll, I'm not comparing the result to what it would look like in in an R terminal. I'm comparing it to how it looks in an output .HTML file.
– haff
4 hours ago
1
I'm giving my students an assignment where they have to complete several lengthy tasks, and for organizational purposes, I think it's easier to use external scripts rather than put everything in multiple code chunks. Also, if they execute code line-by-line in a code chunk in a .Rmd, Rstudio puts the output in a "drawer" under the chunk, which is annoying to me, as I think it makes more sense for it to appear in the console like it does in a simple .R file.
– haff
4 hours ago
1
I'm not quite following. You seem to be mixing up RMarkdown and straight-up R files. If you don't like the way RMarkdown renders code&text, don't use RMarkdown. But then you are saying that you are "not comparing the result to what it would look like in in an R terminal." So what are you actually trying to do and how are you executing code? In a R terminal? In RStudio? Is code in an RMarkdown file? Or in an R script. I've had my students very successfully use RMarkdown and R Notebooks. In my opinion it encourages transparent code documentation and consistent coding standards.
– Maurits Evers
4 hours ago
I love the way Rmarkdown renders code and text. What I'm saying is that if I usesource('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
3 hours ago
1
1
I guess my first question is: Why? What are you trying to do? Secondly, if I do
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 Evers
4 hours ago
I guess my first question is: Why? What are you trying to do? Secondly, if I do
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 Evers
4 hours ago
We'll, I'm not comparing the result to what it would look like in in an R terminal. I'm comparing it to how it looks in an output .HTML file.
– haff
4 hours ago
We'll, I'm not comparing the result to what it would look like in in an R terminal. I'm comparing it to how it looks in an output .HTML file.
– haff
4 hours ago
1
1
I'm giving my students an assignment where they have to complete several lengthy tasks, and for organizational purposes, I think it's easier to use external scripts rather than put everything in multiple code chunks. Also, if they execute code line-by-line in a code chunk in a .Rmd, Rstudio puts the output in a "drawer" under the chunk, which is annoying to me, as I think it makes more sense for it to appear in the console like it does in a simple .R file.
– haff
4 hours ago
I'm giving my students an assignment where they have to complete several lengthy tasks, and for organizational purposes, I think it's easier to use external scripts rather than put everything in multiple code chunks. Also, if they execute code line-by-line in a code chunk in a .Rmd, Rstudio puts the output in a "drawer" under the chunk, which is annoying to me, as I think it makes more sense for it to appear in the console like it does in a simple .R file.
– haff
4 hours ago
1
1
I'm not quite following. You seem to be mixing up RMarkdown and straight-up R files. If you don't like the way RMarkdown renders code&text, don't use RMarkdown. But then you are saying that you are "not comparing the result to what it would look like in in an R terminal." So what are you actually trying to do and how are you executing code? In a R terminal? In RStudio? Is code in an RMarkdown file? Or in an R script. I've had my students very successfully use RMarkdown and R Notebooks. In my opinion it encourages transparent code documentation and consistent coding standards.
– Maurits Evers
4 hours ago
I'm not quite following. You seem to be mixing up RMarkdown and straight-up R files. If you don't like the way RMarkdown renders code&text, don't use RMarkdown. But then you are saying that you are "not comparing the result to what it would look like in in an R terminal." So what are you actually trying to do and how are you executing code? In a R terminal? In RStudio? Is code in an RMarkdown file? Or in an R script. I've had my students very successfully use RMarkdown and R Notebooks. In my opinion it encourages transparent code documentation and consistent coding standards.
– Maurits Evers
4 hours ago
I love the way Rmarkdown renders code and text. What I'm saying is that if I use
source('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
3 hours ago
I love the way Rmarkdown renders code and text. What I'm saying is that if I use
source('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
3 hours ago
 |Â
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
There is another way of doing it so it looks exactly like having the code in the markdown file.
Your external.R
file:
## @knitr answer
x <- 1
y <- 3
z <- x + y
z
Your Rmarkdown file:
---
title: "Untitled"
output: html_document
---
```r echo=FALSE
knitr::read_chunk('external.R')
```
```r
<<answer>>
```
That produces:
Yep. Good call. This isn't exactly what I requested in my question, but as it turns out, this it is what I was looking for.
– haff
3 hours ago
This works for me but in RStudio, the editor indicates the line with<<answer>>
is anunexpected token '<' error
. I didn't know you could use info like@knitr
.
– steveb
3 hours ago
FYI, the error RStudio was giving me appears to have gone away.
– steveb
2 hours ago
add a comment |Â
up vote
4
down vote
You could set comment = NA
in your code chunk options.
Example:
---
title: "Untitled"
output: html_document
---
```r setup, include=FALSE
knitr::opts_chunk$set(
echo = TRUE,
comment=NA)
```
## Example
```r
source("example.R", echo = T, prompt.echo = "", spaced = F)
```
This produces
1
I think at last, Mr. Baggins, you and I understand one another. That does the trick. Thanks Maurits.
– haff
3 hours ago
1
No worries @haff;-) Glad it was helpful.
– Maurits Evers
3 hours ago
Ok, I hate to go back on what I said, but this doesn't exactly produce what I want. It's almost there, but maybesource
isn't the right function. In the example you have above, the linesx <- 3
andy <- 4
don't have syntax highlighting. These lines are prepended with a>
(like they were run in the R shell), which I think this is probably an artifact of how thesource
function works?
– haff
3 hours ago
1
@haff No problem at all. Everything makes a lot more sense now that I understand what you are trying to do. You can change the prompt character withprompt.echo
withinsource
. So for example,source("example.R", echo = T, prompt.echo = "")
would remove the>
. You can also remove the additional extra linebreak withsource("example.R", echo = T, prompt.echo = "", spaced = F)
. Does that help?
– Maurits Evers
3 hours ago
Yes - that's good to know. I think spadarian's answer captures what I wanted to do a bit better, but I can foresee of other cases in which the source options you listed will be helpful to me. Thanks.
– haff
2 hours ago
 |Â
show 1 more comment
up vote
0
down vote
Although the accepted answer provides a simple and working solution, I think the most idiomatic way of doing this (without having to modify the external script at all) is to use the chunk option code
to set the contents of external.R
as chunk code:
```r, code = readLines("external.R")
```
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
There is another way of doing it so it looks exactly like having the code in the markdown file.
Your external.R
file:
## @knitr answer
x <- 1
y <- 3
z <- x + y
z
Your Rmarkdown file:
---
title: "Untitled"
output: html_document
---
```r echo=FALSE
knitr::read_chunk('external.R')
```
```r
<<answer>>
```
That produces:
Yep. Good call. This isn't exactly what I requested in my question, but as it turns out, this it is what I was looking for.
– haff
3 hours ago
This works for me but in RStudio, the editor indicates the line with<<answer>>
is anunexpected token '<' error
. I didn't know you could use info like@knitr
.
– steveb
3 hours ago
FYI, the error RStudio was giving me appears to have gone away.
– steveb
2 hours ago
add a comment |Â
up vote
4
down vote
accepted
There is another way of doing it so it looks exactly like having the code in the markdown file.
Your external.R
file:
## @knitr answer
x <- 1
y <- 3
z <- x + y
z
Your Rmarkdown file:
---
title: "Untitled"
output: html_document
---
```r echo=FALSE
knitr::read_chunk('external.R')
```
```r
<<answer>>
```
That produces:
Yep. Good call. This isn't exactly what I requested in my question, but as it turns out, this it is what I was looking for.
– haff
3 hours ago
This works for me but in RStudio, the editor indicates the line with<<answer>>
is anunexpected token '<' error
. I didn't know you could use info like@knitr
.
– steveb
3 hours ago
FYI, the error RStudio was giving me appears to have gone away.
– steveb
2 hours ago
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
There is another way of doing it so it looks exactly like having the code in the markdown file.
Your external.R
file:
## @knitr answer
x <- 1
y <- 3
z <- x + y
z
Your Rmarkdown file:
---
title: "Untitled"
output: html_document
---
```r echo=FALSE
knitr::read_chunk('external.R')
```
```r
<<answer>>
```
That produces:
There is another way of doing it so it looks exactly like having the code in the markdown file.
Your external.R
file:
## @knitr answer
x <- 1
y <- 3
z <- x + y
z
Your Rmarkdown file:
---
title: "Untitled"
output: html_document
---
```r echo=FALSE
knitr::read_chunk('external.R')
```
```r
<<answer>>
```
That produces:
answered 3 hours ago
spadarian
73237
73237
Yep. Good call. This isn't exactly what I requested in my question, but as it turns out, this it is what I was looking for.
– haff
3 hours ago
This works for me but in RStudio, the editor indicates the line with<<answer>>
is anunexpected token '<' error
. I didn't know you could use info like@knitr
.
– steveb
3 hours ago
FYI, the error RStudio was giving me appears to have gone away.
– steveb
2 hours ago
add a comment |Â
Yep. Good call. This isn't exactly what I requested in my question, but as it turns out, this it is what I was looking for.
– haff
3 hours ago
This works for me but in RStudio, the editor indicates the line with<<answer>>
is anunexpected token '<' error
. I didn't know you could use info like@knitr
.
– steveb
3 hours ago
FYI, the error RStudio was giving me appears to have gone away.
– steveb
2 hours ago
Yep. Good call. This isn't exactly what I requested in my question, but as it turns out, this it is what I was looking for.
– haff
3 hours ago
Yep. Good call. This isn't exactly what I requested in my question, but as it turns out, this it is what I was looking for.
– haff
3 hours ago
This works for me but in RStudio, the editor indicates the line with
<<answer>>
is an unexpected token '<' error
. I didn't know you could use info like @knitr
.– steveb
3 hours ago
This works for me but in RStudio, the editor indicates the line with
<<answer>>
is an unexpected token '<' error
. I didn't know you could use info like @knitr
.– steveb
3 hours ago
FYI, the error RStudio was giving me appears to have gone away.
– steveb
2 hours ago
FYI, the error RStudio was giving me appears to have gone away.
– steveb
2 hours ago
add a comment |Â
up vote
4
down vote
You could set comment = NA
in your code chunk options.
Example:
---
title: "Untitled"
output: html_document
---
```r setup, include=FALSE
knitr::opts_chunk$set(
echo = TRUE,
comment=NA)
```
## Example
```r
source("example.R", echo = T, prompt.echo = "", spaced = F)
```
This produces
1
I think at last, Mr. Baggins, you and I understand one another. That does the trick. Thanks Maurits.
– haff
3 hours ago
1
No worries @haff;-) Glad it was helpful.
– Maurits Evers
3 hours ago
Ok, I hate to go back on what I said, but this doesn't exactly produce what I want. It's almost there, but maybesource
isn't the right function. In the example you have above, the linesx <- 3
andy <- 4
don't have syntax highlighting. These lines are prepended with a>
(like they were run in the R shell), which I think this is probably an artifact of how thesource
function works?
– haff
3 hours ago
1
@haff No problem at all. Everything makes a lot more sense now that I understand what you are trying to do. You can change the prompt character withprompt.echo
withinsource
. So for example,source("example.R", echo = T, prompt.echo = "")
would remove the>
. You can also remove the additional extra linebreak withsource("example.R", echo = T, prompt.echo = "", spaced = F)
. Does that help?
– Maurits Evers
3 hours ago
Yes - that's good to know. I think spadarian's answer captures what I wanted to do a bit better, but I can foresee of other cases in which the source options you listed will be helpful to me. Thanks.
– haff
2 hours ago
 |Â
show 1 more comment
up vote
4
down vote
You could set comment = NA
in your code chunk options.
Example:
---
title: "Untitled"
output: html_document
---
```r setup, include=FALSE
knitr::opts_chunk$set(
echo = TRUE,
comment=NA)
```
## Example
```r
source("example.R", echo = T, prompt.echo = "", spaced = F)
```
This produces
1
I think at last, Mr. Baggins, you and I understand one another. That does the trick. Thanks Maurits.
– haff
3 hours ago
1
No worries @haff;-) Glad it was helpful.
– Maurits Evers
3 hours ago
Ok, I hate to go back on what I said, but this doesn't exactly produce what I want. It's almost there, but maybesource
isn't the right function. In the example you have above, the linesx <- 3
andy <- 4
don't have syntax highlighting. These lines are prepended with a>
(like they were run in the R shell), which I think this is probably an artifact of how thesource
function works?
– haff
3 hours ago
1
@haff No problem at all. Everything makes a lot more sense now that I understand what you are trying to do. You can change the prompt character withprompt.echo
withinsource
. So for example,source("example.R", echo = T, prompt.echo = "")
would remove the>
. You can also remove the additional extra linebreak withsource("example.R", echo = T, prompt.echo = "", spaced = F)
. Does that help?
– Maurits Evers
3 hours ago
Yes - that's good to know. I think spadarian's answer captures what I wanted to do a bit better, but I can foresee of other cases in which the source options you listed will be helpful to me. Thanks.
– haff
2 hours ago
 |Â
show 1 more comment
up vote
4
down vote
up vote
4
down vote
You could set comment = NA
in your code chunk options.
Example:
---
title: "Untitled"
output: html_document
---
```r setup, include=FALSE
knitr::opts_chunk$set(
echo = TRUE,
comment=NA)
```
## Example
```r
source("example.R", echo = T, prompt.echo = "", spaced = F)
```
This produces
You could set comment = NA
in your code chunk options.
Example:
---
title: "Untitled"
output: html_document
---
```r setup, include=FALSE
knitr::opts_chunk$set(
echo = TRUE,
comment=NA)
```
## Example
```r
source("example.R", echo = T, prompt.echo = "", spaced = F)
```
This produces
edited 3 hours ago
answered 3 hours ago
Maurits Evers
22.5k41432
22.5k41432
1
I think at last, Mr. Baggins, you and I understand one another. That does the trick. Thanks Maurits.
– haff
3 hours ago
1
No worries @haff;-) Glad it was helpful.
– Maurits Evers
3 hours ago
Ok, I hate to go back on what I said, but this doesn't exactly produce what I want. It's almost there, but maybesource
isn't the right function. In the example you have above, the linesx <- 3
andy <- 4
don't have syntax highlighting. These lines are prepended with a>
(like they were run in the R shell), which I think this is probably an artifact of how thesource
function works?
– haff
3 hours ago
1
@haff No problem at all. Everything makes a lot more sense now that I understand what you are trying to do. You can change the prompt character withprompt.echo
withinsource
. So for example,source("example.R", echo = T, prompt.echo = "")
would remove the>
. You can also remove the additional extra linebreak withsource("example.R", echo = T, prompt.echo = "", spaced = F)
. Does that help?
– Maurits Evers
3 hours ago
Yes - that's good to know. I think spadarian's answer captures what I wanted to do a bit better, but I can foresee of other cases in which the source options you listed will be helpful to me. Thanks.
– haff
2 hours ago
 |Â
show 1 more comment
1
I think at last, Mr. Baggins, you and I understand one another. That does the trick. Thanks Maurits.
– haff
3 hours ago
1
No worries @haff;-) Glad it was helpful.
– Maurits Evers
3 hours ago
Ok, I hate to go back on what I said, but this doesn't exactly produce what I want. It's almost there, but maybesource
isn't the right function. In the example you have above, the linesx <- 3
andy <- 4
don't have syntax highlighting. These lines are prepended with a>
(like they were run in the R shell), which I think this is probably an artifact of how thesource
function works?
– haff
3 hours ago
1
@haff No problem at all. Everything makes a lot more sense now that I understand what you are trying to do. You can change the prompt character withprompt.echo
withinsource
. So for example,source("example.R", echo = T, prompt.echo = "")
would remove the>
. You can also remove the additional extra linebreak withsource("example.R", echo = T, prompt.echo = "", spaced = F)
. Does that help?
– Maurits Evers
3 hours ago
Yes - that's good to know. I think spadarian's answer captures what I wanted to do a bit better, but I can foresee of other cases in which the source options you listed will be helpful to me. Thanks.
– haff
2 hours ago
1
1
I think at last, Mr. Baggins, you and I understand one another. That does the trick. Thanks Maurits.
– haff
3 hours ago
I think at last, Mr. Baggins, you and I understand one another. That does the trick. Thanks Maurits.
– haff
3 hours ago
1
1
No worries @haff;-) Glad it was helpful.
– Maurits Evers
3 hours ago
No worries @haff;-) Glad it was helpful.
– Maurits Evers
3 hours ago
Ok, I hate to go back on what I said, but this doesn't exactly produce what I want. It's almost there, but maybe
source
isn't the right function. In the example you have above, the lines x <- 3
and y <- 4
don't have syntax highlighting. These lines are prepended with a >
(like they were run in the R shell), which I think this is probably an artifact of how the source
function works?– haff
3 hours ago
Ok, I hate to go back on what I said, but this doesn't exactly produce what I want. It's almost there, but maybe
source
isn't the right function. In the example you have above, the lines x <- 3
and y <- 4
don't have syntax highlighting. These lines are prepended with a >
(like they were run in the R shell), which I think this is probably an artifact of how the source
function works?– haff
3 hours ago
1
1
@haff No problem at all. Everything makes a lot more sense now that I understand what you are trying to do. You can change the prompt character with
prompt.echo
within source
. So for example, source("example.R", echo = T, prompt.echo = "")
would remove the >
. You can also remove the additional extra linebreak with source("example.R", echo = T, prompt.echo = "", spaced = F)
. Does that help?– Maurits Evers
3 hours ago
@haff No problem at all. Everything makes a lot more sense now that I understand what you are trying to do. You can change the prompt character with
prompt.echo
within source
. So for example, source("example.R", echo = T, prompt.echo = "")
would remove the >
. You can also remove the additional extra linebreak with source("example.R", echo = T, prompt.echo = "", spaced = F)
. Does that help?– Maurits Evers
3 hours ago
Yes - that's good to know. I think spadarian's answer captures what I wanted to do a bit better, but I can foresee of other cases in which the source options you listed will be helpful to me. Thanks.
– haff
2 hours ago
Yes - that's good to know. I think spadarian's answer captures what I wanted to do a bit better, but I can foresee of other cases in which the source options you listed will be helpful to me. Thanks.
– haff
2 hours ago
 |Â
show 1 more comment
up vote
0
down vote
Although the accepted answer provides a simple and working solution, I think the most idiomatic way of doing this (without having to modify the external script at all) is to use the chunk option code
to set the contents of external.R
as chunk code:
```r, code = readLines("external.R")
```
add a comment |Â
up vote
0
down vote
Although the accepted answer provides a simple and working solution, I think the most idiomatic way of doing this (without having to modify the external script at all) is to use the chunk option code
to set the contents of external.R
as chunk code:
```r, code = readLines("external.R")
```
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Although the accepted answer provides a simple and working solution, I think the most idiomatic way of doing this (without having to modify the external script at all) is to use the chunk option code
to set the contents of external.R
as chunk code:
```r, code = readLines("external.R")
```
Although the accepted answer provides a simple and working solution, I think the most idiomatic way of doing this (without having to modify the external script at all) is to use the chunk option code
to set the contents of external.R
as chunk code:
```r, code = readLines("external.R")
```
answered 33 mins ago


CL.
9,14732144
9,14732144
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52397430%2finclude-code-from-an-external-r-script-run-in-display-both-code-and-output%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
I guess my first question is: Why? What are you trying to do? Secondly, if I do
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 Evers
4 hours ago
We'll, I'm not comparing the result to what it would look like in in an R terminal. I'm comparing it to how it looks in an output .HTML file.
– haff
4 hours ago
1
I'm giving my students an assignment where they have to complete several lengthy tasks, and for organizational purposes, I think it's easier to use external scripts rather than put everything in multiple code chunks. Also, if they execute code line-by-line in a code chunk in a .Rmd, Rstudio puts the output in a "drawer" under the chunk, which is annoying to me, as I think it makes more sense for it to appear in the console like it does in a simple .R file.
– haff
4 hours ago
1
I'm not quite following. You seem to be mixing up RMarkdown and straight-up R files. If you don't like the way RMarkdown renders code&text, don't use RMarkdown. But then you are saying that you are "not comparing the result to what it would look like in in an R terminal." So what are you actually trying to do and how are you executing code? In a R terminal? In RStudio? Is code in an RMarkdown file? Or in an R script. I've had my students very successfully use RMarkdown and R Notebooks. In my opinion it encourages transparent code documentation and consistent coding standards.
– Maurits Evers
4 hours ago
I love the way Rmarkdown renders code and text. What I'm saying is that if I use
source('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
3 hours ago