Functions
Site: | Saylor Academy |
Course: | PRDV420: Introduction to R Programming |
Book: | Functions |
Printed by: | Guest user |
Date: | Wednesday, May 14, 2025, 8:00 AM |
Description
You have used a few functions already. Here is just a bit more formal introduction. You should be able to understand the inputs (arguments) you specify when calling a function and the output it returns. For most functions, you can get help by executing ?function_name
in the console.
Calling functions
R has a large collection of built-in functions that are called using:
function_name(arg1 = val1, arg2 = val2, ...)
Let's try using seq()
which makes regular sequences of numbers and, while we're at it, learn more helpful features of RStudio. Type se
and hit TAB. A popup shows you possible completions. Specify seq()
by typing more (a "q") to disambiguate or by using ↑/↓ arrows to
select. Notice the floating tooltip that pops up, reminding you of the
function's arguments and purpose. If you want more help, press F1 to get
all the details in the help tab in the lower right pane.
Press TAB once more when you've selected the function you want. RStudio will add a matching opening ((
) and closing ()
) parentheses for you. Type the arguments 1, 10
and hit return.
seq(1, 10)
#> [1] 1 2 3 4 5 6 7 8 9 10
Type this code and notice you get similar assistance with the paired quotation marks:
x <- "hello world"
Quotation marks and parentheses must always come in a pair. RStudio does its best to help you, but it's still possible to mess up and end up with a mismatch.
If this happens, R will show you the continuation character "+":
> x <- "hello
+
The +
says that R is waiting for more input. Usually, that means you've forgotten either a "
or a )
. Either add the missing pair or press ESCAPE to abort the expression and try again.
If you make an assignment, you do not see the value, and you are then tempted to double-check the result immediately:
y <- seq(1, 10, length.out = 5)
y
#> [1] 1.00 3.25 5.50 7.75 10.00
This common action can be shortened by surrounding the assignment with parentheses, which causes the assignment and "print to screen" to happen.
(y <- seq(1, 10, length.out = 5))
#> [1] 1.00 3.25 5.50 7.75 10.00
Now look at your environment in the upper right pane:

Here you can see all of the objects that you've created.
Source: H. Wickham and G. Grolemund, https://r4ds.had.co.nz/workflow-basics.html This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 3.0 License.
Getting help with functions and features
R has an inbuilt help facility similar to the man
facility of
UNIX. To get more information on any specific named function, for
example solve
, the command is
> help(solve)
An alternative is
> ?solve
For a feature specified by special characters, the argument must be
enclosed in double or single quotes, making it a "character string":
This is also necessary for a few words with syntactic meaning including
if
, for
and function
.
> help("[[")
Either form of quote mark may be used to escape the other, as in the
string "It's important"
. Our convention is to use
double quote marks for preference.
On most R installations help is available in HTML format by running
> help.start()
which will launch a Web browser that allows the help pages to be browsed
with hyperlinks. On UNIX, subsequent help requests are sent to the
HTML-based help system. The ‘Search Engine and Keywords' link in the
page loaded by help.start()
is particularly useful as it is
contains a high-level concept list which searches though available
functions. It can be a great way to get your bearings quickly and to
understand the breadth of what R has to offer.
The help.search
command (alternatively ??
)
allows searching for help in various
ways. For example,
> ??solve
Try ?help.search
for details and more examples.
The examples on a help topic can normally be run by
> example(topic)
Windows versions of R have other optional help systems: use
> ?help
for further details.
Source: R Core Team, https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Getting-help This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.