4

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"
Dev Ops
  • 127
  • 1
  • 10
  • Can you provide the output of: `dput(rmarkdown_code)`? It's not clear how the code is stored in a variable. – Matt Oct 15 '21 at 15:41
  • @Matt I've updated the original post with the details. is there anything else i can provide? – Dev Ops Oct 15 '21 at 16:26
  • Is the variable an `expression` or a `character` string of code? **EDIT**: Just saw your update and it appears to be the latter. – Greg Oct 15 '21 at 16:26
  • @Greg did you have any recommendations? – Dev Ops Oct 15 '21 at 17:05
  • I'm afraid not, as my background in this particular area is purely academic and lacking in experience. In fact, I'm very interested to see any answers that might come up. – Greg Oct 15 '21 at 17:06

1 Answers1

2

Here's a solution, not sure if it's ideal for your use case but it does successfully render rmkdown_code as an HTML report.

Be sure that you set your working directory or are working in a project, because the .rmd will render to your current wd.

rmkdown_code <- "---\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"

render_func <- function(x){
  writeLines(x, "temp.rmd")
  rmarkdown::render("temp.rmd")
}

render_func(rmkdown_code)

This gives us:

Matt
  • 7,255
  • 2
  • 12
  • 34
  • Thank you. The problem with this answer is that it creates a temporary file. I would very much like to avoid writing the variable content into any file. – Dev Ops Oct 15 '21 at 20:26
  • I'm not sure of a way that `rmarkdown::render()` will work if the input isn't a `markdown` file. Is it possible to create a temp file on your C:/ drive and then automatically delete it? – Matt Oct 15 '21 at 21:41