Create Power BI Visual Using R Script Visual - Bar Chart

Overview

Power BI provides the flexibility to create your own custom visuals using R script visual. Before you dive in I request you to refer the following articles to start with R-script visuals.

In this article, we will learn a step-by-step procedure to create R-script visual in Power BI.

I believe you have installed R Engine and R Studio in your environment. You must have an active Power BI Pro license to publish R script visual. If you have Power BI free license, you are not able to publish the R script visual.

I have uploaded Sample data file and report file in GitHub. You can directly download it using the following URL,

Now, let’s get started!

  1. Click on R-script visual from the visualization pane. It will open a pop up for “Enable script visuals”. Click on Enable button.


  2. It will create a blank R-script visual as shown in the following figure.


  3. Drag the fields, from the table. In our example, we want to prepare a report using Month, US and London column. So, I drag three columns.

    If you observe, it has created some auto-generated code in the script editor. Now, we will open the same code in R Studio.

    To open the code in R studio, click on the icon (Edit script in external R IDE).


  4. It will open R Studio with predefined code.

    Optional
    If your Power BI code doesn’t reflect in R Studio automatically, then you need to go to the following path to open your R script file in R Studio.

    Path: C:\Users\Dhruvin Shah\REditorWrapper_58dc934e-160d-427c-8d2b-4d7f8abc23bd

    Here, REditorWrapper folder with the latest timestamp covers your R file. Open it in R Studio.


    Follow the below step only if your R file does not open in R Studio automatically.
     

  5. Now, let’s add code in R Studio and check the result.

    Add following line of code in R Studio.

    1. library( ggplot2 )  
    2. #dataset$monthname = c(dataset$Month)  
    3. dataset$Month  
    4. dataset$Month <-factor(dataset$Month,  
    5. levels = c("January""February""March""April""May""June",  
    6. "July""August""September""October""November""December"))  
    7. ggplot(data=dataset, aes(x=Month, y=US )) +  
    8. geom_bar(stat="identity")  

     


  6. Here, we are using a ggplot2 library to generate the R visual. So you need to install it first.

  7. To install ggplot2 package open packages from R studio, click on install, search for ggplot2 and click on Install.


  8. Once this library  is installed, we will run the code we have applied in Step 5. 

  9. To run the code, select all the code first ( Refer numeral 1 from Figure below) and click on run button. ( Refer numeral 2 from Figure below). Select the Plot tab ( Refer numeral 3 from Figure below) to see the final result. (Refer numeral 4 from Figure below)


  10. You will find the end result in the plots option of R studio. (Refer numeral 4 from Figure above)

  11. Now, we will copy the same code from R Studio and paste it to Power BI R script visual.


  12. Select code and click on Run button. ( Refer numeral 1 and 2 from Figure below ) The visual will look like the following image. (Refer numeral 3 from the following Figure).


  13. Now, let’s publish the report to your Power BI Pro account. R script visual will not work with Power BI free license.

Conclusion:

This is how you can start the custom development with R script visual in Power BI. I hope you love this article. Stay connected with me for amazing tutorials.


Similar Articles