How to Generate Your Own Error Messages in R
How to Step Through Debugging an R Function
How to Perform Multiple Calculations with Vectors Using R

How to Put Arguments in Your R Functions

Most functions in R have arguments that give them more information about exactly what you want them to do. If you use print("Hello world!"), you give the argument x of the print() function a value: "Hello world!". Indeed, the first default argument of the print() function is called x. You can check this yourself by looking at the Help file of print().

In R, you have two general types of arguments:

  • Arguments with default values

  • Arguments without default values

If an argument has no default value, the value may be optional or required. In general, the first argument is almost always required. Try entering the following:

> print()

R tells you that it needs the argument x specified:

Error in .Internal(print.default(x, digits, quote, na.print, print.gap, : 'x' is missing

You can specify an argument like this:

> print(x = "Isn't this fun?")

Sure it is. But wait — when you entered the print("Hello world!") command you didn’t add the name of the argument, and the function worked. That’s because R knows the names of the arguments and just assumes that you give them in exactly the same order as they’re shown in the usage line of the Help page for that function.

If you type the values for the arguments in Help-page order, you don’t have to specify the argument names. You can list the arguments in any order you want, as long as you specify their names.

Try entering the following example:

> print(digits=4, x = 11/7)
[1] 1.571

You may wonder where the digits argument comes from, because it’s not explained in the Help page for print(). That’s because it isn’t an argument of the print() function itself, but of the function print.default(). Take a look again at the error you got if you typed print(). R mentions the print.default() function instead of the print() function.

In fact, print() is called a generic function. It determines the type of the object that’s given as an argument and then looks for a function that can deal with this type of object. That function is called the method for the specific object type.

In case there is no specific function, R will call the default method. This is the function that works on all object types that have no specific method. In this case, that’s the print.default() function. Keep in mind that a default method doesn’t always exist. For now, just remember that arguments for a function can be shown on the Help pages of different methods.

If you forgot which arguments you can use, you can find that information in the Help files. Don’t forget to look at the arguments of specific methods as well. You often find a link to those specific methods at the bottom of the Help page.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Use Apply to Create Tabular Summaries in R
How to Extract Values from a Matrix in R
How to Do Matrix Arithmetic in R
How to Use the Formula Interface in R
How to Use read.csv() to Import Data in R
Advertisement

Inside Dummies.com