Book Image

Learning Shiny

By : Hernan Resnizky
Book Image

Learning Shiny

By: Hernan Resnizky

Overview of this book

Table of Contents (19 chapters)
Learning Shiny
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Introducing R, RStudio, and Shiny
Index

Control structures in R


Control structures in computer programming are statements that decide the execution (or not) of certain pieces of code. In while and if, they are based on a condition that evaluates to TRUE or FALSE, and in for, the statement is executed for every element of the input sequence.

In R, all the control structures have the same coding pattern, as follows:

control_structure(condition or sequence){code block}

The if...else block

The following is a small example of an if...else block in R. You can play with it by changing the value of a:

> a <- 5
> if(a > 0){print("a is greater than 0")} else
+ { print("a is smaller than 0")}
[1] "a is greater than 0"

Note

The else clause must start in the same line where the if clause ends.

With an else...if statement, it would be:

> a <- 10
> if(a < 0 ){
+   print("a is smaller than 0")} else if(a >= 0 & a <= 5)
+     { print("a is between 0 and 5")} else
+   { print("a is greater than 5")}
[1] "a is greater...