Blog Post

Power BI Community Blog
2 MIN READ

The R code didn't result in creation of any visuals

Anonymous's avatar
Anonymous
Not applicable
4 years ago

Background:  

 

It is well known that Power BI Desktop supports the Plotly package for R. However, in some cases, if we create a chart in Power BI Desktop using the R package Plotly, we would return the following results (using a scatter plot as an example). 

No image was created. The R code didn't result in creation of any visuals. Make sure your R script results in a plot to the R default device.

 

 

The reason of this error is that we are using Plotly to create interactive web-based graphics, while the R visual in Power BI is known to support plotting only for default devices 

 

 

 

Solution:

 

Create a custom R visualization and import it into Power BI Desktop and Service. 

 

  1. Please download and install R and the required R packages. Download NodeJS from https://nodejs.org/.

 

 

  1. Install Power BI visual tools with administrator privileges.

 

  • Node.js command prompt -> More -> Run as administrator

         npm install -g powerbi-visuals-tools

 

 

 

To confirm it is installed correctly you can run the command: pbiviz

Then you should see the information about Power BI Custom Visual Tools.

 

 

 

  1. Create a folder to save custom visual objects. (Example path: C:\CustomRvisual) 

 

 

 

  1. Go to the folder in the Node.js command prompt and download the "rhtml" template.

         cd C:/CustomRvisual

         pbiviz new rVisualSample -t rhtml

 

 

 

  • Then you can find the rVisualSample file in that folder.

 

 

  1. This is a folder that provides templates for us to create other R custom visuals, please check the script.r file inside the folder. We need to replace them with our own script.

 

 

Note that the dataset used here is "Values", because we need to add fields to the Values pane in PowerBi. The field names assigned to each axis need to be the same as the field names in PowerBI.

 

  1. Fill in the displayName, supportUrl, description, author's name and email, and any other important information in the pbivis.json file.

 

 

For example:

 

 

Note: If you cannot save the script.r and pbivis.json files due to access prohibition, please change the user's permission in Properties -> Security of these files.

 

  1. Package the visual from the root directory of the visual:

         cd C:/CustomRvisual/rVisualSample

         pbiviz package 

 

 

This command creates a pbiviz file in the dist/ directory of your visual project, and overwrites any previous pbiviz file that might exist. The package contains everything required to import the custom visual into either the Power BI service or a Power BI Desktop report.

 

 

  1. Import custom visual.
  • Open the report in Power BI Desktop or Power BI Service.
  • Select "Import a visual from a file" from the menu.

 

 

  • Navigate to the folder containing the custom visual file (*.pbiviz) and open it.

 

 

  • If you want the visual to remain on the visualization pane so you can use it in future reports, right-click on it and select Pin to visualization pane.

 

  1. Add the fields and the final rendered result is as follows. 

 

 

Hope this will help you.

 

 

Author: Winni Zhang

Reviewer: Kerry Wang & Ula Huang

Updated 4 years ago
Version 2.0

2 Comments

  • stevedep's avatar
    stevedep
    Icon for Memorable Member rankMemorable Member

    You can take a look at this blog post. It describes how to add an interactive javascript Plotly Chart in Power BI. It's quite easy. 

     

    Kind regards,

     

    Steve.

  • mahenkj2's avatar
    mahenkj2
    Icon for Solution Sage rankSolution Sage

    Hi Anonymous 

     

    Thanks for this post. This is very simple approach to create a custom R visual, though I could use it, but so far I am not able to make a working custom R visual yet. Finally, at one one point, I am able to execute the codes above, but the created visual show nothing, with no error as well.

     

    source('./r_files/flatten_HTML.r')
    
    ############### Library Declarations ###############
    libraryRequireInstall("ggplot2");
    libraryRequireInstall("plotly")
    ####################################################
    
    ################### Actual code ####################
    library(ggplot2)
    library(plotly)
    # Input load. Please do not change #
    `dataset` = read.csv('C:/Users/myname/REditorWrapper_fa15975d-207e-480d-8075-b8f9406c6106/input_df_7e16760f-ffad-4458-9c7d-76855b7e28ac.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
    
    g = ggplot(data = dataset, aes(y=measured_Value, x=measured_DateTime)) + 
      geom_line(aes(colour=measured_DisplayShortName) , show.legend = TRUE);
    ####################################################
    
    ############# Create and save widget ###############
    p = ggplotly(g);
    internalSaveWidget(p, 'out.html');
    ####################################################
    
    ################ Reduce paddings ###################
    ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-9]*,', ',"padding":0,')
    ####################################################

     

    Can you please check whats wrong here!

     

    My purpose is to use plotly in PowerBI.

     

    Thanks.