r - 从函数返回图形而不绘制它
问题描述
我想编写返回图表但不应该绘制图表的函数。它应该只在我要求时绘制图表。
这是一个MWE。
graph_functions <- function(x) {
plot(1:length(x), x)
points(1:length(x), x^2)
t <- recordPlot()
return(t)
}
answer <- graph_functions(1:10)
library(cowplot)
plot_grid(answer, answer)
在上面的代码中,当我第一次通过调用计算答案时,我不希望它绘制图形graph_functions(1:10)
。我只希望它在我使用plot_grid()
.
解决方案
graph_functions<- function(x) {
plot(1:length(x),x)
points(1:length(x),x^2)
t<- recordPlot()
return(t)
}
answer <- c(1:10)
library(cowplot)
plot_grid(graph_functions(answer),graph_functions(answer))
您可以将函数放在 plot_grid() 函数中,并将参数存储在 answer 变量中。
推荐阅读
- symfony - 将 Symfony 4 应用程序部署到 Google App Engine 时出现安全检查器错误
- c++11 - 基于#define变量类型的条件编译
- vue.js - VueJS 在两个页面之间传递数据
- python-3.x - Numpy 点积返回 [inf] 数组
- ruby-on-rails - 目前在 Rails 3 上,开始编写单元测试,那么考虑到 Rails 6 和单元测试的未来,使用 Rspec 还是 Minitest 更好?
- javascript - 尝试在新标签(AngularJS)中下载 PDF 时获取垃圾字符
- python - 如何编写使用其他 C 程序的父程序?
- php - 循环遍历 JSON 对象,另一个数组中的一个数组
- javascript - 每次循环更改变量
- c - 替换键盘中断(中断 9)进行两次调用而不是一次调用