apr92
apr92

Reputation: 51

Recursion error in R (Fibonacci sequence)

So I am trying to learn R on my own and am just working through the online tutorial. I am trying to code a recursive function that prints the first n terms of the Fibonacci sequence and can't get the code to run without the error:

Error in if (nterms <= 0) { : missing value where TRUE/FALSE needed

My code does ask me for input before entering the if else statement either which I think is odd as well. Below is my code any help is appreciated.

#Define the fibonacci sequence

recurse_fibonacci <- function(n) {
    # Define the initial two values of the sequence

    if (n <= 1){
        return(n)
    } else {

    # define the rest of the terms of the sequence using recursion
    return(recurse_fibonacci(n-1) + recurse_fibonacci(n-2))
    }
}

#Take input from the user
nterms = as.integer(readline(prompt="How many terms? "))

# check to see if the number of terms entered is valid
if(nterms <= 0) {
    print("please enter a positive integer")
} else {

    # This part actually calculates and displays the first n terms of the sequence
    print("Fibonacci Sequence: ")
    for(i in 0:(nterms - 1)){
        print(recurse_fibonacci(i))
    }
}

Upvotes: 5

Views: 1119

Answers (3)

phoxis
phoxis

Reputation: 61920

To make it work with Rscript replace

nterms = as.integer(readline(prompt="How many terms? "))

with

cat ("How many terms?")
nterms = as.integer (readLines ("stdin", n = 1))

Then you can run it as Rscript fib.R, assuming that the code is in the file fib.R in the current working directory.

Otherwise, execute it with source ("fib.R") within an R shell.

Rscript does not operate in interactive mode and does not expect any input from the terminal. Check what interactive () returns in both the cases. Rscript will return FALSE as it is non-interactive, but the same function when run within an R shell (with source ()) it will be true.

?readline mentions that it cannot be used in non-interactive mode. Whereas readLines explicitely connect to stdin.

Upvotes: 2

Rui Barradas
Rui Barradas

Reputation: 76460

This is a problem of readline in non-interactive mode. readline does not wait for a keypress and immediately executes the next instruction. The solution below is the solution posted in this other SO post.

I post below a complete answer, with the Fibonnaci numbers function a bit modified.

recurse_fibonacci <- function(n) {
  # Define the initial two values of the sequence
  if (n <= 1){
    n
  } else{
    # define the rest of the terms of the sequence using recursion
    Recall(n - 1) + Recall(n - 2)
  }
}

#Take input from the user
cat("How many terms?\n")
repeat{
  nterms <- scan("stdin", what = character(), n = 1)
  if(nchar(nterms) > 0) break
}
nterms <- as.integer(nterms)

# check to see if the number of terms entered is valid
if(nterms <= 0) {
  print("please enter a positive integer")
} else {
  # This part actually calculates and displays the first n terms of the sequence
  print("Fibonacci Sequence: ")
  for(i in 0:(nterms - 1)){
    print(recurse_fibonacci(i))
  }
}

This code is the contents of file fib.R. Running in a Ubuntu 20.04 terminal gives

rui@rui:~$ Rscript fib.R
How many terms?
8
Read 1 item
[1] "Fibonacci Sequence: "
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 13
rui@rui:~$

Upvotes: 2

Jan
Jan

Reputation: 5254

The code works fine but you shouldn't enter it into the terminal as is. My suggestion: put the code into a script file (ending .R) and source it (get help about it with ?source but it's actually pretty straightforward).

In R-Studio you can simply hit the source button.

Upvotes: 0

Related Questions