我有以下代码:
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
创建哪个:
我要做的是将这些代码放在函数中,以便 它返回一个通过单个处理程序保存这些图的对象。 我该如何实现?
我尝试了此方法,但不起作用:
test_handle <- function() {
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
# how can I put 4 objects into 1 handler and return it?
}
答案 0 :(得分:0)
在函数内部编写函数return()
,如下所示:
test_handle <- function() {
attach(mtcars)
return(par(mfrow=c(2,2)))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
}
希望有帮助。