如何从函数生成rmarkDown文件

发布于 2025-02-05 04:30:13 字数 563 浏览 0 评论 0原文

如果有人能帮助我,我会非常感谢。

我想创建一个函数,该函数从函数中指定的参数中返回一些文件,例如数据范围和图。

我还希望此功能生成一个rmarkDown文件,其中将显示该函数中生成的表和图。这也许可以使用rmarkDown :: Render()在功能中,但是我不确定如何确切。

该函数将是类似的:

# 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")

想法是该函数在计算机上的文件夹中生成rmarkdown文件,并且用户可以免费指定参数。谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2025-02-12 04:30:13

简短的答案是“是”。这是一个潜在的解决方案:

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")

输出(“ 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`.

”

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`.

“

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

自我难过 2025-02-12 04:30:13

这将采用MyFunction的输出,并使用Knitr :: Spin将其作为HTML或PDF呈现。如果输出称为测试,则它将创建一个称为test.r的文件,该文件将其插入test.md中,然后将其呈现为test.html(test.pdf(如果已指定了PDF_Document(),则已指定)。所有文件都在当前目录中。请注意,这将模块化的列表的生成和HTML或PDF输出的生成分开。

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")

生成的测试是:

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

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

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

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']])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文