Next Gen Data Learning – Amplify Your Skills

Blog Home

Blog

R Scripting For Power BI Using RStudio

by | Power BI, R for Power BI

In this blog, we’re going to go through the basics of R scripting for Power BI using RStudio.

The RStudio is used for creating statistical computations and graphics that are in line with data analysis and research.

It’s a great tool for coding because it is an integrated development environment (IDE). This means it has features that you can use in coding like source code editor, autocomplete, syntax highlighting, and debugging tool.

RStudio is a more convenient setting for R scripting rather than the default features of R programming language’s installation.

R Scripting In RStudio

When you open RStudio for the first time, you’ll see 3 different windows. But in this tutorial, we’re going to focus on where you can see the blinking cursor. This part is called the Console where we can do R scripting in RStudio.

R scripting

For the first example, let’s create a simple arithmetic operation and execute it by pressing Enter.

By typing and executing “1 + 1” in the Console, we’re able to get “2” as a result. If you’re curious about the purpose of [1], let’s do an example to further elaborate. I’ll create a sequence of numbers from 1 to 50 using the colon (:) operator.

R scripting

After executing “1:50”, you’ll see [1], [18], and [35] before each line of the result. There’s no term used for this part in RStudio. But these numbers act as a placeholder for the index position for each line of the result. If you look at the first line, it begins with the 1st value which is 1. The second line begins with the 18th value in the result, and the third line begins with the 35th value.

For the next example, we’ll try using functions that you might be familiar with. Most of these are the same with the functions that we use in Power BI. Let’s get the square root of 25.

R scripting

To calculate the square root of 25, we’ll use the SQRT function. Then in the parameters, let’s set 25 as the value to be calculated by the function. The SQRT function is one of the functions that we also use in Power BI which is associated with DAX.

Creating A New R Scripting File In RStudio

In RStudio, you need to create an R Script file if you want to make sure that you keep all the scripts you’ve added in case you’ve accidentally closed the application. To do this, simply click on the File menu. Then hover on New File and select R Script. The keyboard shortcut for creating a new R Script file is Ctrl+Shift+N.

R scripting

After this procedure, you should now have a 4th window that holds the new R Script you’ve created. By default, the R Script file will be named as Untitled1, but you can change this later on once you save the file.

R scripting

Understanding The Autocomplete Feature In RStudio

There are advantages in implementing R scripts in the R Script file. Being able to save R scripts is one of them. Another good thing is being able to utilize the autocomplete feature that’s included in RStudio.

This is how the autocomplete feature looks like in RStudio. This’ll only show once you start typing any function in the R Script file. This is a great feature as it also provides the definition on how a specific function works.

Once you see this while typing the function that you want to use, just press the Tab key on your keyboard to autocomplete.

R scripting

Let’s finish this example by adding 25 as our value in the parameters and clicking on the Run button to execute the script. The result of this script will appear in the Console window.

R scripting

Utilizing An Ordered Operation In RStudio

Let’s have another example and create an ordered operation.

R scripting

In the example, we added the ordered operation “1 – 2 ^ 4”. The R programming language follows BEDMAS (Brackets, Exponents, Division, Multiplication, Addition, and Subtraction) for the ordered operations.

Because of that, it performed the exponent (^) operation first before subtraction (-) and we got -15 as a result.

Getting Help Documentation For R Scripting

The R programming language also has a lot of great help documentation. In case you don’t have an idea about how a certain function works, you can use the question mark (?) operator to search for documentation.

For example, let’s say we want to get documentation about the plot function. We’ll type the ? operator before the function name. Then execute it by pressing Ctrl+Enter.

R scripting

Upon executing, we’ll see a help documentation about plot function under the Help window. We can click on the links provided by the help documentation to learn more about a specific topic regarding the plot function. Let’s click on the Generic X-Y Plotting.

R scripting

Upon clicking, we should see further description about plot function and some examples at the end of the documentation.

R scripting
R scripting

We can also copy and paste one of the examples in this documentation and run it in the R script file. As an example, let’s take the highlighted script above.

For this blog, we’re not going to discuss this example as this is merely just for demonstration about some of the things that you can do in RStudio.

However, there’s one important part that you need to know which is the Comment that is represented by the # symbol. Let’s try running this script by highlighting it and clicking on the Run button.

After executing the script, it will generate a plot as a result.

Saving An R Script File

Once we’re done with our scripts, we can save the R script file the same way we save files in other softwares (by pressing Ctrl+S). After that, a Save File window will pop-up. From here we can select the location where we want to save the R script file and include a file name.

Upon saving, we can close and open this file anytime we want for editing.

The Environment Pane In RStudio

The last thing we’ll discuss is the Environment window. In this window, we can see useful data such as values and functions that we have created in RStudio.

As we can see in the example, we have the value x which was created when we executed the script that we took from the help documentation example. In addition to the script that we used earlier, the <- symbol is usually used to give or assign data to an object.

***** Related Links *****
Three Ways To Use R Script In Power BI
How to Use Python Script In Power BI
Create Power BI Report To SQL Server Using R Script

Conclusion

In summary, we learned about the basics of R scripting by checking out some of RStudio’s interface and how they work. We’re also able to implement some functions and operations that are similar with the ones we use in Power BI.

We’ve learned that creating an R scripting file is important to keep a record of the scripts that we’ve executed.
We also discussed some great features and operators such as the autocomplete and the question mark (?) symbol in searching for a help document. We also learned about saving an R script file and the use of the environment pane in RStudio.

All the best,

George Mount

Related Posts