I'd like to take an R script and write it into an Rmarkdown document as a functioning code chunk.
For example, my .R file is called analysis.R and contains some R code such as
plot(rnorm(100))
My template Rmarkdown document is as follows:
---
title: "Insert R as Code Chunk"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
#Here's where I pipe in the analysis.R script as a functioning code chunk
The output .Rmd I'm looking for is simply:
---
title: "Insert R as Code Chunk"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
plot(rnorm(100))
```
I have a similar issue. I want to write generic code inside a Rmarkdown document which generically creates (in my case for flexdashboard) rows and corresponding plots. Is something like this even possible?
It's also possible to write the plot into a tempfile and read it out in a chunk via the code-option. But then I can't write the row configuration since that has to be outside of the chunk.
Here a repex:
in the Rmd-file. Then I copy this file to a temp-Rmd file and append the dynamic code to the new file by readr::write_lines(). I then call rmarkdown::render() on the appended file and done.
I would still be interested in a solution that works inside the Rmd itself, if someone knows how to do that .