crumbs357
crumbs357

Reputation: 25

R: Variable Titles for Plotting

I'm trying to write a function that plots a set of data using the basic plot command. It looks something like the following.

myfunction = function(input.data,title.str) {
    # commands to plot input.data using plot()
    title(main=title.str)
}

myfunction(object1,'show this title')

Basically I'm trying to pass a string as an argument and use it as the title of my plot. So far all the plotting works great and the problem is that I'm getting the following error.

Error in myfunction(object1,"show this title") : unused argument(s) ("show this title")

Upvotes: 0

Views: 638

Answers (2)

Gavin Simpson
Gavin Simpson

Reputation: 174778

We don't have object1 but this works for me:

myfunction = function(input.data, title.str) {
    plot(input.data)
    title(main = title.str)
}

object1 <- data.frame(x = runif(10), y = runif(10))
myfunction(object1, "foo")

Upvotes: 2

Tyler
Tyler

Reputation: 10032

I suspect the version of myfunction that gives you that error is not the same as the one that you posted. The posted code works as expected for me.

You can check this by entering myfunction on the console (without brackets) and examining the function body that is printed.

Upvotes: 1

Related Questions