是否可以在 Power BI 的 R Script Visual 中使用 R Plotly 库? [英] Is it possible to use R Plotly library in R Script Visual of Power BI?

查看:22
本文介绍了是否可以在 Power BI 的 R Script Visual 中使用 R Plotly 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人尝试在 Power BI 的 R Script Visual 中使用 PlotlyHighchart

当我在 R 脚本编辑器中尝试此操作并运行时:

库(ggplot2)图书馆(情节)x <- 1:5y <- c(1, 3, 2, 3, 1)plot_ly(x = dataset$period, y = dataset$mean, name = "spline", line = list(shape = "spline"))

错误信息:

<块引用>

没有创建图像.R 代码没有产生任何视觉效果.确保您的 R 脚本生成到 R 默认设备的绘图.

但可以在我的 R 桌面上完美运行.有什么想法吗?

解决方案

对于较新版本的 PowerBI,还可以使用 R 生成 Plotly 图表ggplot 作为 自定义 PowerBI 可视化.使用下面描述的方法,您可以从 PowerBI 表中生成密度图,如下所示:

<小时>

资源:

我建议的解决方案使用 nodejs,可以在

<小时>

第 2 部分:制作 PowerBI 自定义视觉对象

<小时>

2.1:创建一个文件夹来包含您的自定义可视化.

我正在使用命令提示符来执行此操作

# 来自 C: 目录:md pbiCustomRcd pbiCustomR

在该文件夹中,运行以下命令:

pbiviz new pbiDensity -t html

这将创建一个新的视觉对象并安装一些所需的包.您可以将 pbiDensity 更改为您想要的任何内容.

2.2:导航到文件夹C:pbiCustomRpbiDensity并仔细查看内容.

那里有很多东西,但我们只关注文件 script.R 以及文件 pbiDensity.pbiviz (位于子文件夹中dist).script.R 是设置 R 脚本的模板.我们稍后将对其进行编辑.pbiDensity.pbiviz 是一个 PowerBI 自定义可视化,您稍后也会将其导入 Power BI.

2.3:打开文件C:pbiqpscript.R可以看到这个(强烈推荐RStudio):

source('./r_files/flatten_HTML.r')############### 库声明###############libraryRequireInstall("ggplot2");libraryRequireInstall(情节")#####################################################################实际代码####################g = qplot(`Petal.Length`, data = iris,fill = `Species`, main = Sys.time());################################################################ 创建和保存小部件###############p = ggplotly(g);internalSaveWidget(p, 'out.html');###################################################

上面的代码片段使用来自 Iris 数据集的数据集,但我们将通过添加以下行来利用 PowerBI 文件中可用的数据:

df <- data.frame(X = Values$Data)

该行从现有列 i PowerBI 文件构建数据框,我们将其命名为 Data.但首先,将上面的完整代码段更改为:

source('./r_files/flatten_HTML.r')############### 库声明###############libraryRequireInstall("ggplot2");libraryRequireInstall(情节")#####################################################################实际代码####################df <- data.frame(X = Values$Data)# 构建基本的ggplotg <- ggplot(df, aes(x = X))# 添加密度图g = g + geom_density(颜色='蓝色')############# 创建和保存小部件###############p = ggplotly(g);internalSaveWidget(p, 'out.html');###################################################

<小时>

2.4:完成您的自定义可视化.

<小时>

在文件夹 C:pbiCustomRpbiDensity 中,使用命令提示符运行以下命令:

pbiviz 包

这将触发此输出:

当涉及到更高级的东西时,就是这样.剩下的很简单!

<小时>

第 3 部分 - 在 PowerBI 中使用 R 制作一些随机数据

<小时>

3.1Home标签下,点击Edit Queries打开Power Query Editor.

3.2点击输入数据点击OK.p>

3.3 选择变换>运行 R Script 并插入以下代码段:

set.seed(123)输出 <- data.frame(rnorm(100, mean=100, sd=10))

这将在 Query Settings 下生成一个名为 "output" 的新步骤,以及一个包含随机数且列名不是世界上最好的表.

3.4Applied Steps下的步骤名称改为tblRandom,列名改为SampleData 所以你最终会得到这个:

3.5 选择 首页 >关闭并应用以返回 PowerBI Desktop.

<小时>

**第 4 部分 - 导入并使用您的自定义可视化

<小时>

4.1Visualizations下,点击三点图标并选择Import from file:

4.1导航到C:pbiCustomRpbiDensitydist,选择pbiDensity.pbiviz,点击OKVisualizations 下应该会出现一个新图标:

这很重要:为了让您的可视化效果发挥作用,您的列名称以匹配您的 R 脚本中的引用.在我们的例子中,他们没有(只是为了说明这一点).因此将名称列 SampleData 更改为 Data.

4.2点击您的新图标插入一个可视化占位符,然后将您的Data列拖到它上面:

你去吧:

现在您可以随意使用 plotly 工具栏的全部灵活性:

<小时>

第 5 部分 - 编辑 R 脚本、更新和重新导入自定义可视化

<小时>

5.1 在 RStudio 中编辑您的脚本并在不更改文件名的情况下保存它

5.2 导航到您的自定义文件夹并运行 pbiviz package

5.3 在 PowerBI 中删除现有的自定义视觉对象并重新导入

Has anyone tried using Plotly or Highchart in R Script Visual of Power BI,

when I try this in R script editor and Run:

library(ggplot2)
library(plotly)
x <- 1:5
y <- c(1, 3, 2, 3, 1)
plot_ly(x = dataset$period, y = dataset$mean, name = "spline", line = list(shape = "spline"))

Error Message:

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

But runs perfectly on my R desktop. Any thought?

解决方案

For newer versions of PowerBI, it's also possible to produce Plotly charts using R and ggplot as custom PowerBI visualizations. With the approach described below, you can produce a density plot from a PowerBI table like this:


Resources:

My suggested solution uses nodejs that can be found here. That, as well as the main parts of my suggestion builds on this excellent blogpost that only has some few shortcomings when it comes to the details about using and updating custom PowerBI Visualizations. I could just refer to that link and point out the things I did differently, but for the sake of clarity and completeness, I've done the whole thing from scratch.


Part 1 - Download and install node.js


1.1: Link: https://nodejs.org/en/

1.2: Restart your computer, launch a command prompt, and run this:

npm install -g powerbi-visuals-tools

1.3: Check that your installation is successful by running the following in your command prompt:

Input:

pbiviz

Output:


Part 2: Make a PowerBI custom visual


2.1: Create a folder to contain your custom visualizations.

I'm using a command prompt to do this

# from the C: directory:
md pbiCustomR
cd pbiCustomR

In that folder, run the following command:

pbiviz new pbiDensity -t html

This will create a new visual and install some required packages. You can change pbiDensity to whatever your want.

2.2: Navigate to the folder C:pbiCustomRpbiDensity and take a closer look at the contents.

There's a lot of stuff there, but we are only going to focus on the file script.R as well as the file pbiDensity.pbiviz (located in the subfolder dist). script.R is a template that sets up your R script. We're going to edit that later. pbiDensity.pbiviz is a PowerBI custom visualization that you'll import in Power BI later too.

2.3: Open the file C:pbiqpscript.R to see this (I highly recommend RStudio):

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
####################################################

################### Actual code ####################
g = qplot(`Petal.Length`, data = iris,fill = `Species`, main = Sys.time());
####################################################

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################

The snippet above uses a dataset from the Iris dataset, but we're going to make use of data available in a PowerBI file by adding this line:

df <- data.frame(X = Values$Data)

That line builds a dataframe from an existing column i PowerBI file that we're going to to name Data. But first, go ahead and change the complete snippet above to:

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
####################################################

################### Actual code ####################
df <- data.frame(X = Values$Data)

# Build basic ggplot
g <- ggplot(df, aes(x = X))

# Add density plot
g = g + geom_density(colour = 'blue')

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################


2.4: Finish your custom visualization.


In the folder C:pbiCustomRpbiDensity, run the following command using the command prompt:

pbiviz package

This will trigger this output:

And that's it when it comes to the more advanced stuff. The rest is easy!


Part 3 - Make some random data in PowerBI using R


3.1 Under the Home tab, click Edit Queries to open the Power Query Editor.

3.2 Click Enter Data, and only click OK.

3.3 Select Transform > Run R Script and insert the following snippet:

set.seed(123)
output <- data.frame(rnorm(100, mean=100, sd=10))

This will produce a new step under Query Settings called "output", as well as a table with random numbers with not the best column name in the world.

3.4 Change the name of the step under Applied Steps to tblRandom, and the name of the column to SampleData so you'll end up with this:

3.5 Select Home > Close&Apply to get back to PowerBI Desktop.


**Part 4 - Import and use your custom visualization


4.1 Under Visualizations, click the icon with the three dots and select Import from file:

4.1 Navigate to C:pbiCustomRpbiDensitydist, select pbiDensity.pbiviz, click OK and a new icon should appear under Visualizations:

And this is important: In order for your visualization to work, the name of your column has to match the reference in your R script. And in our case, they don't (just to make a point out of it). So change the name column SampleData to Data.

4.2 Click your new icon to insert a visualization placeholder, and drag your Data column to it:

And there you go:

Now you have the full flexibility of the plotly toolbar at your disposal:


Part 5 - Edit R script, update and reimport custom visualization


5.1 Edit your script in RStudio and save it without changing the filename

5.2 Navigate to your custom folder and run pbiviz package

5.3 Delete your existing custom visual in PowerBI and import it again

这篇关于是否可以在 Power BI 的 R Script Visual 中使用 R Plotly 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆