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.
Create a custom R visualization and import it into Power BI Desktop and Service.
Please download and install R and the required R packages. Download NodeJS from https://nodejs.org/.
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.
Create a folder to save custom visual objects. (Example path: C:\CustomRvisual)
Go to the folder in the Node.js command prompt and download the "rhtml" template.
pbiviz new rVisualSample -t rhtml
Then you can find the rVisualSample file in that folder.
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.
Fill in the displayName, supportUrl, description, author's name and email, and any other important information in the pbivis.json file.
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.
Package the visual from the root directory of the visual:
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.
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.
Add the fields and the final rendered result is as follows.