
We have R set up the list of x values by using the built-in sequence function: x = seq( from = -2, to = 2, by = 0.1 ).

Let's arbitrarily select x values from −2 to +2, separated by intervals of 0.1. We must tell R where all those densely packed points should be positioned.Įvery point is specified by its x and y coordinates, so we have to provide R with a list of x values and a list of corresponding y values. The example in the next section should help clarify.Īs a simple example of what R can do, let's plot a quadratic function: y = x 2.What looks like a smooth curve on a graph is actually a set of points connected by straight lines, but the lines are so small that the graph looks like a smooth curve. To run a program stored in a file named MyProgram.R, we could type “ source(“MyProgram.R”)” at R's command line, or click the “source” button in the R or RStudio menu. In R, running a script is called source-ing the script, because you are redirecting the source of commands from the command line to the script. Then we could run the program and R would step through the list of commands, in order.

We could save the list of commands as a program, or script, in a simple text file. This is because R assumes that when you type in x+x, you are really asking for the value of the sum of the value of x with the value of x, and you are not asking for an algebraic reformulation such as 2*x. For example, you could first type in x=2 and then, as a second command, type in x+x, to which R will reply 4. Throughout this book, programming commands are typeset in a distinctive font, like this, to distinguish them from English prose and to help demarcate the scope of the programming command when it is embedded in an English sentence.Ī program, also known as a script, is just a list of commands that R executes. As we will see later, variables in R are often multi-component structures, and therefore it can be informative to the user to display which component is being shown. The answer, of course, is 5, but the line begins with a bracketed “ ” to indicate that the first component of the answer is 5.


Again, the “ >“ symbol above indicates the R command prompt, at which we typed “ 2+3.” The next line above, “ 5,” shows R's reply.
