Command Line versus Scripts

When writing functions and other multi-line sections of input you will find it useful to use a text editor rather than execute everything directly at the command line. I always use Word for this, because it is so easy to keep a copy of all the output and graphics produced by R using Copy and Paste. Other people prefer to use R's own built-in editor. It is accessible from the RGui menu bar. Click on File then click on New script. At this point R will open a window entitled Untitled - R Editor. You can type and edit in this, then when you want to execute a line or group of lines, just highlight them and press Ctrl + R (the Control key and R together). The lines are automatically transferred to the command window and executed.

By pressing Ctrl + S you can save the contents of the R Editor window in a file that you will have to name. It will be given a .R file extension automatically. In a subsequent session you can click on File/Open script ... when you will see all your saved .R files and can select the one you want to open.

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.