如何从函数生成rmarkDown文件
如果有人能帮助我,我会非常感谢。
我想创建一个函数,该函数从函数中指定的参数中返回一些文件,例如数据范围和图。
我还希望此功能生成一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的答案是“是”。这是一个潜在的解决方案:
输出(“ tmp.html”):
output(“ tmp.html”):
Short answer is "yes" you can. Here is one potential solution:
Output ("tmp.html"):
Output ("tmp.html"):
这将采用MyFunction的输出,并使用Knitr :: Spin将其作为HTML或PDF呈现。如果输出称为测试,则它将创建一个称为test.r的文件,该文件将其插入test.md中,然后将其呈现为test.html(test.pdf(如果已指定了PDF_Document(),则已指定)。所有文件都在当前目录中。请注意,这将模块化的列表的生成和HTML或PDF输出的生成分开。
生成的测试是:
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.
The generated test.R is: