Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this the expected behavior

Tags:

r

My apologies if this is kind of vague question as I am new to R. While experimenting with R I found one weird behavior. When I create a function like:

myfunction <- function(a,b){
    print(a,b)
}

and call it like:

myfunction(b = 10, a = 20)

it returns with result 20, but if I simply call it without function via assigning it directly to variables like:

a <- 20
b <- 10
print(a, b)

I get an error:

Error in print.default(a, b) : invalid 'digits' argument

Furthermore I have read that printing multiple variables in the same line can be accomplished via:

sprintf("%i %i",a, b)

So here is it a bug that it is appearing in function call with result as the first argument?

like image 669
Varun Tahin Avatar asked Jun 17 '17 18:06

Varun Tahin


People also ask

What is the expected behavior?

Expected behavior is simply behavior that is normal, reasonable and anticipated. Unexpected behavior is behavior that is out of the norm, and is unusual.

Which is correct behavior or Behaviour?

Behavior is the preferred spelling in American English. Behaviour is preferred everywhere else. Other than the spelling, there is no difference between the two words. The spelling distinction extends to all derivatives, including behaviors–behaviours, behavioral–behavioural, and behaviorally–behaviourally.

What is expected behavior in society?

Some times may call for different behavior with any given circumstance. In general expected behavior in society is to be an upstanding law abiding citizen who contributed and continues to contribute to society by good means such as having a job, and most importantly treating other people with kindness and manners.

Why do people do what is expected?

Doing what is expected is different based on where we are and who we are with. What makes a behavior expected is that it encourages others to feel calm , happy, and pleased in response to the social behavior. When others feel positively, they tend to treat the person who produced the behavior more positively.

What is expected and unexpected behaviors unit?

This Expected and Unexpected Behaviors Unit is based off of the Book: Social Behavior Mapping: Connecting Behavior, Emotions and Consequences Across the Day written by Michelle Garcia Winner, SLP ©2007 Think Social Publishing, Inc. All Right Reserved. www.socialthinking.com

What happens to the person who produces unexpected behaviors?

The person who produced the unexpected behaviors, receive negative reactions and tend to feel upset with others and more discouraged about the situation. Social Behavior Mapping: Connecting Behavior, Emotions and Consequences Across the Day written by Michelle Garcia Winner, SLP ©2007 Think Social Publishing, Inc.


1 Answers

It might be revealing some underlying differences in how parameters are being handled in different scenarios but I don't think it's a bug.

If your intention is to print both values, consider changing:

print(a,b)

To something like:

print(paste(a,b))

From ?print.default:

# S3 method for default
print(x, digits = NULL, quote = TRUE,
  na.print = NULL, print.gap = NULL, right = FALSE,
  max = NULL, useSource = TRUE, …)

x       the object to be printed.

digits  a non-null value for digits specifies the minimum number of 
        significant digits to be printed in values. The default, NULL, uses 
        getOption("digits"). (For the interpretation for complex numbers see 
        signif.) Non-integer values will be rounded down, and only values 
        greater than or equal to 1 and no greater than 22 are accepted.
...

So R is expecting everything that you actually want to print to be contained in the first variable (x).

Based on your results and some of the comments, apparently in some cases the second variable is being accepted as a valid digits parameter value and in other cases it is not.

While this is a little odd, the more important point is that print(a,b) is not a syntactically correct way to print multiple values.

like image 53
flyingfinger Avatar answered Oct 19 '22 15:10

flyingfinger