Even if the structure of the R language foresees particular delimiters for some program blocks, it is still useful, the indentation of the code in R, for the relative identification.
In this regard, it should be recalled that the code indentation refers to the technique used in programming through which program blocks are highlighted by inserting a certain amount of empty space at the beginning of a line of text, with the aim of increasing readability.
Although, as already mentioned, R provides appropriate delimiters for some language structures, we will use the same indentation to indicate the nested blocks; in this regard it is possible to use both a tabulation and an arbitrary number of white spaces.
In using this technique it is necessary to remember simple recommendations:
- the number of spaces to use is variable;
- all instructions in the program block must have the same number of indentation spaces.
In this context we will use the convention that provides for the exclusive use of two spaces o identify a new block and to leave out the use of the tab.
In this post we will analyze why use the R script editors available on the network. To program with R, we can use any text editor and a simple command line interface. Both of these tools are already present on any operating system, so if we want, we can ignore this passage.
R script editor
This is because, when a programmer writes a simple program, it does so using the text editor window of windows, this is because to make programs the fonts, colors and in general the graphic appearance are irrelevant, indeed they can make the work of the programmer more difficult.
This is the reason why in the software development environments are not used complex word processing programs, these programs are largely used by writers, but rather simple text editors (such as notepad or notepad in Windows or vi and emacs in linux environment).
Instead of complicated text visual management options, these editors provide advanced text processing features, such as fast text-based navigation procedures, word searches and substitutions within the file, and external files , recognition of key words of the programming language with the possibility of highlighting them by coloring them differently from the rest of the text, and finally the identification of the text.
To create a script in R just use the File menu through the following sequence of commands:
File => New script
This will open an empty script in this way, it will be up to us now to populate it with a code of great value. Once we have entered some commands in the script window, it will need to be run in the console. Of course you can do the copy-paste, as usual, but it’s better to be decidedly quicker and more professional to select with the mouse the code you want to perform and press the Ctrl + R keys. Alternatively, you can also right-click in the window and select the Execute line or selection item.
How to create a script in R
From this example comes a first consideration: typing the commands directly in the shell of R is not really the best solution. An effective way to interact with the shell is to create a script in R. In this method, you can write the code in a separate window and then you will be able to execute the code in the console, so that if you feel the need to save the code or run it several times you will not need to type it all again .