R programming language functions provide an easy way to develop reusable code. The structure of R Programming functions is very similar to R expressions. A few examples in this post will expand on the data set from the R Programming Introduction blog post. Finally, these examples were run in a working directory of C:\R on a Windows 10 workstation.
R Programming – Functions – Setup
R Framework and RStudio
R Framework and RStudio must be installed on your workstation to run these examples. More information on how to install and configure R can be found in the R Programming Introduction blog post.
- Go to the following github repository
- Download R-prog-functions.zip into working directory -> C:\R
- Extract files into C:\R working directory.
There will three files in the C:\R directory:
nfl_2016_games_allseason.txt – input data file.
R_nflgames_example.R – R script with environment set up and calling functions commands.
R_nflgames_Functions.R – R script with functions for NFL 2016 season data.
After working directory has been setup, open RStudio. Type the following command from R prompt:
After working directory is set, then run the two R scripts using R’s source command.
Calling R Functions in RStudio
R_nflgames_example.R script has function call commands that can be executed from R prompt. Simply copy command from the text file and paste into a R prompt.