Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

v-rzhou-msft

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

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.

 

RicoZhou_8-1653975331581.png

 

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 

 

RicoZhou_9-1653975331583.png

 

 

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/.

 

RicoZhou_10-1653975331588.png

 

  1. Install Power BI visual tools with administrator privileges.

 

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

         npm install -g powerbi-visuals-tools

 

RicoZhou_11-1653975331590.png

 

RicoZhou_12-1653975331593.png

 

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

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

 

RicoZhou_13-1653975331594.png

 

 

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

 

RicoZhou_14-1653975331595.png

 

 

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

         cd C:/CustomRvisual

         pbiviz new rVisualSample -t rhtml

 

RicoZhou_15-1653975331595.png

 

RicoZhou_16-1653975331596.png

 

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

 

RicoZhou_17-1653975331596.png

 

  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.

 

RicoZhou_18-1653975331599.png

 

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.

 

RicoZhou_19-1653975331601.png

 

For example:

 

RicoZhou_20-1653975331602.png

 

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 

 

RicoZhou_21-1653975331604.png

 

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.

 

RicoZhou_22-1653975331605.png

 

  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.

 

RicoZhou_23-1653975331607.png

 

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

 

RicoZhou_24-1653975331607.png

 

  • 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. 

 

RicoZhou_25-1653975331608.png

 

Hope this will help you.

 

 

Author: Winni Zhang

Reviewer: Kerry Wang & Ula Huang

Comments

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.

Hi @v-rzhou-msft 

 

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.