Using
the R command line
The basic way to interact with R is through the command line interface.
In RStudio, this command line interaction occurs in the command console.
R is an interpreted programming language. This means that R will
interpret each line of code as it is entered and, if it is valid, R will
execute it, returning the result in the command console. This is a more
direct interaction than a compiled programming language, where you edit
the code, compile it, run the executable, and receive the output result.
The immediate feedback of an interpreted interface makes R relatively
easy to learn and work with. Simply enter your code, press the ENTER
key, and get the result.
A
short example exercise will help demonstrate the R interpretive command
line interface in the RStudio Command console.
Type:
45 + 56 then press ENTER
The result 101
is returned to the command console
Type:
x <- 34 then press ENTER
Type: y <-16
then press ENTER
Type: x - y
then press ENTER
The result 18
is returned to the command console
Type:
y/x then press ENTER
The result 0.4705882
is returned to the command console
Type: x - z
then press ENTER
The result Error:
object 'z' not found is returned to the command console. R
gave you this error message because you do not have an object named z.
One last example:
Create
an object v containing a list
Type:
v <- c(1, 2, 3, 4, 5, 6) then
press ENTER [the function c( ) will be explained later
when we explore the R functions]
You
can view the contents of v
Type:
v
The
result 1 2 3 4 5 6 is returned to
the command console
Start
to type the function name mean( )
As
you type each letter, RStudio begins to suggest available objects and
functions from your session environment. Any object active in the
Environment panel or any function in the active packages in the Packages
panel will be recommended by RStudio for your use. This RStudio
recommendation system makes command line interaction easier. You do not
have to remember all of the active objects or functions. You can pick
the one you want to use from the recommendation list.
Once
you choose the mean( ) function, R inserts it on the
command line and places your input cursor inside the function
parentheses so you can fill in the function arguments.
To
finish our example
Type:
v as an argument to the mean( ) function and press ENTER
The result 3.5
is returned to the command console
You
can also copy and paste code from earlier in your R session and run it
again. Simply highlight the line of code. Copy it to the clipboard [use
CNTL+c in Windows or ⌘+c in Mac]. Go
to the command prompt [you can simply press the Down Arrow on your
keyboard and your cursor will jump the command prompt]. Paste the code
at the command prompt [use CNTL+v in windows or ⌘+v
in Mac], then press ENTER.
You
can now interact with R using the command line interface. As you develop
your skills using R, you may want to save your work for a later session
or run a block of code to initialize your session environment. This can
be easily done with an R script. The next topic will introduce you to R
scripts and how they can expand your capabilities.