-1

In R studio results will sometimes stop being returned/printed to the console, I restart R studio and everything works again. For example, when I run the script below, before running it results print to the console fine, then I run it and I get nothing? What can I do other than restart R everytime? Thank you!

[Workspace loaded from ~/.RData]

> print(1+2)
[1] 3
> print('Everything works')
[1] "Everything works"
> 
> 
> for(i in 1:100){
+   if(i%%3==0 & i%%5==0){
+     print('fizzbuzz')
+   } else { 
+       if (i%%3==0 & i%%5!=0){
+         print("fizz")
+       } else {
+         if (i%%3!=0 & i%%5==0) {
+           print("buzz")
+         } else {
+           (print(i))
+         }
+       }
+ 
+ 
+ print(1+2)
+ print('Everything works')
Benzle
  • 373
  • 3
  • 9
  • 18

1 Answers1

1

Working as intended. You haven't closed your curly braces correctly. To escape this, you can push the escape key. If you keep seeing + at the start of a line instead of >, Rstudio is waiting for you to close your curly braces. It helps to keep indentation consistent so you know which level you are in.

Corrected code:

for(i in 1:100) {
  if(i%%3==0 & i%%5==0) {
    print('fizzbuzz')
  } else { 
    if (i%%3==0 & i%%5!=0){
      print("fizz")
    } else {
      if (i%%3!=0 & i%%5==0) {
        print("buzz")
      } else {
        (print(i))
      }
    }
  }
}

If you do find yourself in a scenario where your code doesn't have all curly braces properly closed and you push escape, the console will print a blank line and begin the next line with >:

> for (i in seq(100)) {
+ 
+ 
+ print(i)
+ print(i)
+ 
+ 
+ 
+ 

> 
Will Beason
  • 3,417
  • 2
  • 28
  • 46