Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:32:27+00:00 2026-06-15T13:32:27+00:00

When I look at R functions I often find the following structure: f <-

  • 0

When I look at R functions I often find the following structure:

f <- function(exp=T) {
  if (exp)
    a <- 1
  else
    a <- 2
}
f()
f(F)

This will run without error. But executing the inner function code throws an error as R probably assumes that the statement is finished after the first assignment a <- 1 and cannot handle the following else.

exp=T
if (exp)
  a <- 1
else
  a <- 2

Now, this makes sense to me, but I still would like to understand why the behaviour of the executed code differs when executed inside or outside a function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:32:28+00:00Added an answer on June 15, 2026 at 1:32 pm

    It’s a consequence of using an interactive shell (REPL) to run scripts:

    After the first branch the shell has seen a complete statement so it assumes that you’re done typing. Unfortunately, R uses the same shell for interpreting scripts even if they are not typed in interactively – so even when you save the if statement to a file and source it (or pipe it into R) you will get the error on the else branch.

    But the following will work just fine:

    if (exp) a <- 1 else a <- 2
    

    Here, the interpreter swallows the line and executes it.

    In your function one would assume that the same applies – and it does! However, the function itself starts with an open brace in your case, so R has to read until it finds the matching closing brace. By contrast, take this function declaration:

    f <- function (exp)
        if (exp)
            a <- 1
        else
            a <- 2
    

    In R you can define functions without braces around the body. But the above code will fail for the same reason that the standalone if without braces fails. By contrast, if I had written the if on a single line this code would once again work.

    Incidentally, your function uses an assignment to a variable that isn’t used. You can (should) do the following instead:

    f <- function (exp) {
        if (exp)
            1
        else
            2
    }
    

    … and the same when using if inside the shell:

    a <- if (exp) 1 else 2
    

    because in R, if is an expression which returns a value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Suppose I have two functions which look like this: public static void myFunction1(int a,
I have successfully worked with jQuery DataTables and they look and function great. But
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
Please look at this code: @interface myObject:NSObject -(void)function:(id)param; @end @implementation myObject -(void)function:(id)param { NSLog(@BEFORE);
take a look at this code: $(document).ready(function() { document.getElementById(sliderId).onmousedown = sliderMouseDown; }); function sliderMouseDown()
I read a documentation of preg_filter function it is look as follows. This is
Possible Duplicate: Assign function arguments to `self` Often I have constructors that look like
I find myself often needing addClassName and removeClassName functions for Element s. However, I
Case Quite often I find myself staring at some old code that doesn't look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.