I have code similar to the following, which is dynamically generated and stored in a variable:
---
title: "test"
author: "me"
date: "3 June 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, comment=""}
for (i in 1 : 3) {
cat('\n')
print(summary(iris[i*50 : 50, "Sepal.Length"] ))
}
```
Let's say it's stored in a variable named rmkdown_code
Now, I need to be able to execute the code in this variable and render it as html or pdf. I am aware of the results = asis
options, and I've tried different variations of it. But still, I'm not able to get the above code to render in html. The html page comes up in rstudio, and it shows the contents of rmkdown_code
variable. It's just not executing it.
I need the content of rmkdown_code
to be rendered as though it was from a flat file.
I have tried the following, none of which worked. Some formatted the text better, but the graph is not plotted. I would like to avoid writing the content of the rmkdown_code
variable to a flat file.
```{r,echo=FALSE,eval=TRUE}
knitr::knit_expand(text=rmkdown_code)
#eval(parse(text = rmkdown_code))
#eval(c(paste0("- `", rmkdown_code, "`"), sep = "\n"))
#eval(knitr::asis_output(rmkdown_code))
#res <- knitr::knit_expand(text=rmkdown_code);knitr::knit(text=res, quiet=F)
# knitr::inline_expr("2+2")
```
output of dput(rmarkdown)
:
## "---\ntitle: \"test\"\nauthor: \"me\"\ndate: \"3 June 2019\"\noutput: html_document\n---\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(echo = TRUE)\n```\n\n```{r, comment=\"\"}\nfor (i in 1 : 3) { \n cat('\\n') \n print(summary(iris[i*50 : 50, \"Sepal.Length\"] ))\n}\n```\n"