1

I have written the R function myplot() which draws a curve corresponding to the supplied function FUN over the interval [-10, 10].

myplot <- function(FUN)
{
  curve(FUN(x), xlim = c(-10, 10))
}

For example

myplot(FUN = dnorm)

gives

enter image description here

How can I add arguments to FUN? For example, let's say I want to plot the normal density with mean 5.

Following @akrun's comment, I can do something like that:

myplot <- function(FUN, ...)
{
  args <- list(...)
  curve(FUN(x, unlist(args)), xlim = c(-10, 10))
}
myplot(dnorm, mean = 5)

But then

   > myplot(FUN = dnorm)
    Error in FUN(x, unlist(args)) : 
      Argument non numérique pour une fonction mathématique

Also, myplot(FUN = dnorm, mean = 5, sd = 2) does not give the expected picture...

3
  • Try dots <- list(...); curve(FUN(x, unlist(dots)), xlim = c(-10, 10)) Commented Feb 3, 2016 at 7:44
  • @akrun: Thank you! I have edited my post with further questions. Thank you again. Commented Feb 3, 2016 at 8:26
  • If you have a different question post a new one Commented Feb 3, 2016 at 8:52

1 Answer 1

2

Your original function works fine (but your original example had a typo)

myplot <- function(FUN, ...)
{
    curve(FUN(x, ...), xlim = c(-10, 10))
}

myplot(dnorm)
myplot(dnorm, mean = 5)
myplot(dnorm, mean = 5, sd=2)

all seem to work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.