R language-missing parameters and no default values

Source: Internet
Author: User

"Missing parameter, no default value"-- in the R language Self-coding function, the error is reported if the parameter has no input value.

1. Questions

For example:

> My.func
My.func<-function (p) {
Print ("TEST is ok!")
Print (P)
}
> My.func ()
[1] "TEST is ok!"
Error in print (p):

Evaluating the ' x ' parameter when selecting a method for the ' print ' function error: Errors: Missing parameter ' P ' and no default value

2. Analysis

What about this? Is it possible to add null values as in the Java language? For example:

My.func<-function (p) {
Print ("TEST is ok!")
if (Is.null (p))
Print ("p value is null.")
Else
Print (P)
}

The answer is no. This is really not the same as other object-oriented languages like Java.

So how to solve it?

3. Solve

The answer is to add the default parameters, that is, in the R language, there are no indeterminate parameters to be allowed.

Examples of improvements:

My.func<-function (p=1) {
Print ("TEST is ok!")
Print (P)
}

Run:

> My.func ()
[1] "TEST is ok!"
[1] 1
>
After you set the default value, it works even if you do not enter a parameter.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

R language-missing parameters and no default values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.