jmu*_*amp 4 plot r ggplot2 knitr r-markdown
我有一个工作流程Rmarkdown,render感觉远非最佳。这是我目前正在做的事情。
我有一个.R脚本,在我的环境中可能已经有数百行代码和几十个对象。在脚本附近或结尾处,我想利用render当前环境中的对象生成一些 HTML 输出。为了实现这一点,我保存感兴趣的对象并将它们重新加载到传递给render所有人的脚本中,同时注意工作目录以及项目相对于我将用来呈现 html 文档的脚本的位置。
这是我当前工作流程的可重现示例以及我想要执行的操作的示例。
# Imagine I have a local data.frame/object I am interested in plotting to html via render
iris_revised <- iris
# My current workflow is to save this object
save(iris_revised, file = 'data/iris_revised.Rdata')
# And then call another script via the render function
rmarkdown::render('R/plot_iris_revised.R', output_format = 'html_document')
Run Code Online (Sandbox Code Playgroud)
其中R/plot_iris_revised.R包含以下代码。
library(ggplot2)
load('../data/iris_revised.Rdata')
for(Species in levels(iris_revised$Species)){
cat('#', Species, '\n')
p <- ggplot(iris_revised[iris_revised$Species == Species,], aes(x =
Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我可以render直接在当前脚本的末尾进行用户操作,而不是调用不同脚本的额外开销,类似于下面的代码(显然不起作用)。
# Ideally I could just do something like this, where I could just render html in the same workflow
input_text <- "
for(Species in levels(iris_revised$Species)){
cat('#', Species, '\n')
p <- ggplot(iris_revised[iris_revised$Species == Species,], aes(x =
Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
}
"
rmarkdown::render(input_text, output_format = 'html_document')
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种解决方案,无需将原始笔记本更改.R为.RmdR 笔记本。
除了我上面介绍的理想功能之外,我还愿意接受有关如何在脚本末尾轻松呈现一些 Rmarkdown 输出的一般工作流程建议.R。
由于input参数引用输入文件,因此您可以只写入input_text(可能是临时的)文件:
tmp <- tempfile(fileext = ".R")
cat(input_text, file = tmp)
rmarkdown::render(tmp, output_format = "html_document", output_dir = getwd())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2156 次 |
| 最近记录: |