R Programming Examples

    1. Code editing and executing

      General information

      The RStudio code editor includes a number of options for productive work – in particular, code highlighting, code completion, simultaneous editing of multiple files, and searching and replacing certain parts of the code.

      File management

      RStudio supports syntax highlighting and other specialized options for working with the code in the following file types:

      • R-scripts
      • Sweave documents
      • TeX documents

      To create a new file, use the menu File -> New:

To open an existing file, use the menu File -> Open or Open Recent. If you open multiple files at the same time, the rapid transition from one document to another is performed using the corresponding tabs at the top of the code editor window. To navigate between a large number of open files, there is the >> icon at the top right of the window of the editor. You can also use the menu View -> Switch to.

Code completion

As noted above in the description of work with the console, RStudio supports automatic code completion using the Tab key. For example, if the workspace has an object called pollResults, you can type on the keyboard poll is, press Tab, and RStudio automatically completes the name of the object.

Search and replace the parts of the code

RStudio Editor has the ability to find and replace the necessary parts of the code. For this you need to use the keyboard shortcut Ctrl + F or the Edit menu -> Find and Replace. At the top of the editor window, there is a button in the form of a magnifying glass – pressing it also leads to the discovery of the dialog box in which you specify your search.

Automatic functions creation

RStudio Editor can analyze the selected part of the text and automatically convert it into a function for later reuse. All the “free” variables in the selected code (that is, objects that are referenced but are not created in isolation) will then be converted into function arguments:

Code comments

To comment on the selected parts of the code (or to remove comments) use the menu item Edit -> Comment / Uncomment Lines.

Code execution

Studio supports execution of code directly from the Editor window. To execute the current line of the code, you can use the keyboard shortcut Ctrl + Enter or click Run Line (s):

After the code execution, the cursor will automatically move to the next line. This mechanism makes it possible to step through the code.

There are two ways to run multiple lines of code:

      • Select the required line and press the keys Ctrl + Enter;
      • Press Ctrl + Shift + Enter to execute all lines in the script.

Hotkeys

    • Ctrl + Shift + N – Create a new document
    • Ctrl + O – open the document
    • Ctrl + S – save the document
    • Ctrl + 1 – move the cursor to the Code Editor
    • Ctrl + 2 – move the cursor to the command line console
  1. 2. Navigation in the code

    General information

    RStudio includes a number of options that provide fast navigation in the code. The study of these options may lead to a significant increase in efficiency, as well as a better understanding of the code written by others.

    Transition to File or function

    If you know the file name of the script or the function name, you can quickly locate them using the search box Go to File / Function (Go to File / Options) in the main toolbar:

    The option “Go to File / Function” works by continuous indexing of the created code.

    Transition to function definition

    Due to the continuously implemented indexing of the code (see above), RStudio can also help you to find the definition of any function. To navigate to the code of the function, move the cursor to its name, and then click Go to Function Definition:

    Going to the definition of the functions can also be done in the following ways:

    • Using F2.
    • Using Ctrl + click on the function name.
    • Using the Code Editor or console.

    Navigation in the script

    There are several ways to navigate through the code stored in a separate script file:

    The above option “Go to File / Function” Allows you to search within the file with the script.

    The command Jump to Line (Go to line) is designed to move to a specific line of the script.

    You can also use the options from the appropriate menu (Code Tools) using the Code Editor or console.

    Two-way navigation

    During the navigation through the code, it is often necessary to quickly return to the previous location. RStudio keeps a list of edits and allows you to jump from one to another in any direction by using the command Back (Back) and Forward (Forward) (available from the Edit menu, as well as in the upper left corner of the Code Editor). The Back and Forward commands allow you to repeat the following steps:

    • Opening a document (or switch between tabs)
    • The transition to the definition of function
    • Go to a specific line of code
    • Go to the function using the Function menu

    The Back and Forward commands can be accessed by using the keyboard shortcut Ctrl + F9 / Ctrl + F10 (Cmd + F9 / Cmd + F10 on Mac).

You have just gotten acquainted with one of our R programming examples. Actually, this was only the second part of our sample. The first and the third parts you can also find on our website. All of them are absolutely free, so you don’t need to worry that you will have to pay. We have completed this free sample with the aim to show our clients how tasks of this type should look. In other words, we wanted to present to you how theoretical rules are applied in practice. We hope our example really helped you to understand the topic better.

Our example was completed by an expert that is knowledgeable in R programming. Our other samples are also completed by people that have achieved mastery in specific disciplines. We test our candidates extremely thoroughly so that we are confident that they are able to provide our clients with high-quality samples.

If the R programming examples that are presented on our blog didn’t help you, you can apply to us for additional help. What kind of help do we mean? You have the ability to order a sample that will be completed just for you. You should fill the order form where you will need to specify the discipline, the deadline and the topic, and then provide us with materials that we’ll need to use to complete your order. Afterward, you will get pricing offers from experts and you will need to choose the expert that you like most of all. During the process, you should keep in touch with your expert so that your sample will be written according to your needs.

No matter how much time is left before your deadline, contact us if you need help. We are available 24/7!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.