2020-12-28

When an arguments is missing in R, how to call another function to use its default argument?

I have a function with multiple optional arguments to modify a plot using ggplot2. If any of these argument is missing, I want to use theirs default values in ggplot2. If there is only one argument, this can be done like:

simple_plot <- function (color) {
   p <- ggplot(mtcars, aes(cyl, mpg))

   if(!missing(color))
   {
      p <- p + geom_point(col = color)
   } else {
      p <- p + geom_point()            # use its default if the argument is missing
   }
}

However, this is not a good solution when there are multiple arguments to modify the plot's point shape, fill, axis titles, breaks, limits, etc. Is there a better way?



from Recent Questions - Stack Overflow https://ift.tt/3mX2UnE
https://ift.tt/eA8V8J

No comments:

Post a Comment