Home > Net >  How to generate an Rmarkdown file from a function
How to generate an Rmarkdown file from a function

Time:06-07

if anyone could help me, I would be very grateful.

I would like to create a function that returns a list with some files, such as dataframes and plots, from the parameters specified in the function.

I would also like this function to generate an Rmarkdown file, in which the tables and plots generated in the function would be displayed. This could perhaps be done with rmarkdown::render() inside the function, but I'm not sure how exactly.

The function would be something like:

# dataset:
data=data.frame(value=rnorm(100))


myfunction <- function(data, color){
  
p <- ggplot(data, aes(x=value))   
  geom_histogram(color = color)

data2 <- data$col2 <- 1:100

mylist <- list(plot = p, data = data2)
return(mylist)

}

test <- myfunction(data, "red")

The idea is that the function generates the Rmarkdown file in a folder on the computer, and the user is free to specify the parameters. Thanks!

CodePudding user response:

Short answer is "yes" you can. Here is one potential solution:

library(ggplot2)

# dataset:
data=data.frame(value=rnorm(100))


myfunction <- function(data, color){
  
  p <- ggplot(data, aes(x=value))   
    geom_histogram(color = color)
  
  cat("---
title: \"example Rmd\"
author: \"Jared_Mamrot\"
date: \'2022-06-06\'
output: html_document
---
  
\`\`\`{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
\`\`\`

## Example plot
\`\`\`{r example, echo=TRUE}
p <- ggplot(data, aes(x=value))   
  geom_histogram(color = color)
p
\`\`\`", 
file = "tmp.Rmd")
  rmarkdown::render("tmp.Rmd")
  data2 <- data$col2 <- 1:100
  
  mylist <- list(plot = p, data = data2)
  return(mylist)
  
}

test <- myfunction(data, "red")

Output ("tmp.html"):

example Rmd
Jared_Mamrot
2022-06-06
Example plot
p <- ggplot(data, aes(x=value))   
  geom_histogram(color = color)
p
## `stat_bin()` using `bins = 30`. Pick better value with
## `binwidth`.

example.png

test2 <- myfunction(data, "deepskyblue")

Output ("tmp.html"):

example Rmd
Jared_Mamrot
2022-06-06
Example plot
p <- ggplot(data, aes(x=value))   
  geom_histogram(color = color)
p
## `stat_bin()` using `bins = 30`. Pick better value with
## `binwidth`.

example_2.png

CodePudding user response:

This takes the output of myfunction and renders it as html or pdf using knitr::spin. If the output is called test then it creates a file called test.R which it spins into test.md and then renders it as test.html (test.pdf if pdf_document() had been specified). All files are in the current directory. Note that this separates the generation of the list and generation of the html or pdf output for modularity.

library(gglot2)
library(knitr)
library(rmarkdown)

# inputs are a named list of R objects and optional output type
spin_list <- function(List, output_format = html_document()) {
  stem <- deparse(substitute(List))
  Rfile <- paste0(stem, ".R")
  cat("#' ---\n#' title: \"", stem, "\"\n#' ---\n", file = Rfile, sep = "")
  for(nm in names(List)) {
    s <- sprintf("\n#  %s, echo=FALSE\nprint(List[['%s']])\n", nm, nm)
    cat(s, file = Rfile, append = TRUE)
  }
  spin(Rfile)
  md <- paste0(stem, ".md")
  render(md, output_format)
}

spin_list(test) # test is created in question
browseURL("test.html")

The generated test.R is:

#' ---
#' title: "test"
#' ---

#  plot, echo=FALSE
print(List[['plot']])

#  data, echo=FALSE
print(List[['data']])
  • Related