0

I have the following code:

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

Which create this:

enter image description here

What I want to do is to put those code inside a function so that it returns an object that holds these plots with a single handler. How can I achieve that?

I tried this but doesn't work:

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?

}
littleworth
  • 4,781
  • 6
  • 42
  • 76

1 Answers1

0

Write the function return() inside the function like this:

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

Hope it helps.

antecessor
  • 2,688
  • 6
  • 29
  • 61