# Introduction to Programming

Contents

We look at running commands from a source file. We also include an overview of the different statements that are used for control-flow that determines which code is executed by the interpreter.

## Executing a file

In the next section the ways to execute the commands in a file using the source command are given. The remaining sections are used to list the various flow control options that are available in the R language definition. The language definition has a wide variety of control functions which can be found using the help command.

> help(Control)
>


### Executing the commands in a File

A set of R commands can be saved in a file and then executed as if you had typed them in from the command line. The source command is used to read the file and execute the commands in the same sequence given in the file.

> source('file.R')
> help(source)
>


If you simply source the file the commands are not printed, and the results of commands are not printed. This can be overridden using the echo, print.eval, and verbose options.

Some examples are given assuming that a file, simpleEx.R, is in the current directory. The file is given below:

# Define a variable.
x <- rnorm(10)

# calculate the mean of x and print out the results.
mux = mean(x)
cat("The mean of x is ",mean(x),"\n")

# print out a summary of the results
summary(x)
cat("The summary of x is \n",summary(x),"\n")
print(summary(x))


The file also demonstrates the use of # to specify comments. Anything after the # is ignored. Also, the file demonstrates the use of cat and print to send results to the standard output. Note that the commands have options to send results to a file. Use help for more information.

The output for the different options can be found below:

> source('simpleEx.R')
The mean of x is  -0.4817475
The summary of x is
-2.24 -0.5342 -0.2862 -0.4817 -0.1973 0.4259
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-2.2400 -0.5342 -0.2862 -0.4817 -0.1973  0.4259
>
>
>
> source('simpleEx.R',echo=TRUE)
Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
-2.32600 -0.69140 -0.06772 -0.13540  0.46820  1.69600
>
>
>
> source('simpleEx.R',print.eval=TRUE)
The mean of x is  0.1230581
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-1.7020 -0.2833  0.1174  0.1231  0.9103  1.2220
The summary of x is
-1.702 -0.2833 0.1174 0.1231 0.9103 1.222
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-1.7020 -0.2833  0.1174  0.1231  0.9103  1.2220
>
>
>
> source('simpleEx.R',print.eval=FALSE)
The mean of x is  0.6279428
The summary of x is
-0.7334 -0.164 0.9335 0.6279 1.23 1.604
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-0.7334 -0.1640  0.9335  0.6279  1.2300  1.6040
>
>
>
>
> source('simpleEx.R',verbose=TRUE)
'envir' chosen:<environment: R_GlobalEnv>
encoding = "native.enc" chosen
--> parsed 6 expressions; now eval(.)ing them:

>>>> eval(expression_nr. 1 )
=================

> # Define a variable.
> x <- rnorm(10)
curr.fun: symbol <-
.. after ‘expression(x <- rnorm(10))’

>>>> eval(expression_nr. 2 )
=================

> # calculate the mean of x and print out the results.
> mux = mean(x)
curr.fun: symbol =
.. after ‘expression(mux = mean(x))’

>>>> eval(expression_nr. 3 )
=================

> cat("The mean of x is ",mean(x),"\n")
The mean of x is  -0.1090932
curr.fun: symbol cat
.. after ‘expression(cat("The mean of x is ",mean(x),"\n"))’

>>>> eval(expression_nr. 4 )
=================

> # print out a summary of the results
> summary(x)
curr.fun: symbol summary
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-1.3820 -1.0550 -0.1995 -0.1091  0.6813  2.1050
.. after ‘expression(summary(x))’

>>>> eval(expression_nr. 5 )
=================

> cat("The summary of x is \n",summary(x),"\n")
The summary of x is
-1.382 -1.055 -0.1995 -0.1091 0.6813 2.105
curr.fun: symbol cat
.. after ‘expression(cat("The summary of x is \n",summary(x),"\n"))’

>>>> eval(expression_nr. 6 )
=================

> print(summary(x))
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-1.3820 -1.0550 -0.1995 -0.1091  0.6813  2.1050
curr.fun: symbol print
.. after ‘expression(print(summary(x)))’


One common problem that occurs is that R may not know where to find a file.

> source('notThere.R')
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In file(filename, "r", encoding = encoding) :
cannot open file 'notThere.R': No such file or directory


R will search the current working directory. You can see what files are in the directory using the dir command, and you can determine the current directory using the getwd command.

> getwd()
[1] "/home/black/public_html/tutorial/R/rst/source/R"
> dir()


You can change the current directory, and the options available depend on how you are using R. For example on a Windows PC or a Macintosh you can use the menu options to change the working directory. You can choose the directory using a graphical file browser. Otherwise, you can change to the correct directory before running R or use the setwd command.

### if statements

Conditional execution is available using the if statement and the corresponding else statement.

> x = 0.1
> if( x < 0.2)
{
x <- x + 1
cat("increment that number!\n")
}
increment that number!
> x
[1] 1.1


The else statement can be used to specify an alternate option. In the example below note that the else statement must be on the same line as the ending brace for the previous if block.

> x = 2.0
> if ( x < 0.2)
{
x <- x + 1
cat("increment that number!\n")
} else
{
x <- x - 1
cat("nah, make it smaller.\n");
}
nah, make it smaller.
> x
[1] 1