Source Code Files in R

May 29, 2010

(This article was first published on R-Chart, and kindly contributed to R-bloggers)

R’s interactive programming style is similar to what I have seen in other environments (e.g. ruby’s irb and Oracle’s SQL*Plus, etc). There are a few commands that you need to be aware of to get up and running with developing R programs.

To identify your current working directory:
This is relevant when you are reading or writing out data or programs. It becomes more important as you move from doing simple interactive sequences of commands to codifying your work in R functions, objects and packages. To change this directory, you can specify a different relative or absolute path:

R functions are the simplest building blocks that you are likely to use in development. They provide a mechanism for encapsulating a reusable sequence of commands. A simple example of how to create a function within an R session:

As you continue to work with a function, you will likely use an external editor. You can specify the editor that will be invoked.

The editor is then invoked whenever you want to modify code using the edit command.

Rather than starting with an interactive session, you can also start with a specific text file containing R source code. This approach more closely mirrors development in other programming languages. The file can be opened and executed as follows:
To run the function, simply call it with any relevant parameters – but make sure to include the parenthesis:

If you call a function without the parenthesis, a listing of the contents of the function will be displayed. This feature is useful when analyzing how functions provided in other R packages have been implemented.

To leave a comment for the author, please follow the link and comment on their blog: R-Chart. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training



CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)