简体   繁体   English

是否可以在 Power BI 的 R Script Visual 中使用 R Plotly 库?

[英]Is it possible to use R Plotly library in R Script Visual of Power BI?

Has anyone tried using Plotly or Highchart in R Script Visual of Power BI,有没有人试过在 Power BI 的 R Script Visual 中使用PlotlyHighchart

when I try this in R script editor and Run:当我在 R 脚本编辑器中尝试此操作并运行时:

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. R 代码没有创建任何视觉效果。 Make sure your R script results in a plot to the R default device.确保您的 R 脚本生成 plot 到 R 默认设备。

But runs perfectly on my R desktop.但在我的 R 桌面上运行完美。 Any thought?任何想法?

For newer versions of PowerBI, it's also possible to produce Plotly charts using R and ggplot as custom PowerBI visualizations .对于较新版本的 PowerBI,还可以使用Rggplot作为custom PowerBI visualizations来生成Plotly图表。 With the approach described below, you can produce a density plot from a PowerBI table like this:使用下面描述的方法,您可以从 PowerBI 表中生成密度图,如下所示:

在此处输入图片说明


Resources:资源:

My suggested solution uses nodejs that can be found here .我建议的解决方案使用可在此处找到的nodejs 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 .这一点以及我的建议的主要部分都建立在这篇出色的博客文章上,该文章在使用和更新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 部分 - 下载并安装 node.js


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

1.2: Restart your computer, launch a command prompt, and run this: 1.2:重新启动计算机,启动命令提示符,然后运行:

npm install -g powerbi-visuals-tools

1.3: Check that your installation is successful by running the following in your command prompt: 1.3:通过在命令提示符中运行以下命令来检查您的安装是否成功:

Input:输入:

pbiviz

Output:输出:

在此处输入图片说明


Part 2: Make a PowerBI custom visual第 2 部分:制作 PowerBI 自定义视觉对象


2.1: Create a folder to contain your custom visualizations. 2.1:创建一个文件夹来包含您的自定义可视化。

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.您可以将pbiDensity更改为您想要的任何值。

2.2: Navigate to the folder C:\\pbiCustomR\\pbiDensity and take a closer look at the contents. 2.2:导航到文件夹C:\\pbiCustomR\\pbiDensity并仔细查看内容。

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以及文件pbiDensity.pbiviz (位于子文件夹dist )。 script.R is a template that sets up your R script. script.R是设置 R 脚本的模板。 We're going to edit that later.稍后我们将对其进行编辑。 pbiDensity.pbiviz is a PowerBI custom visualization that you'll import in Power BI later too. pbiDensity.pbiviz是一个 PowerBI 自定义可视化,稍后您也将在 Power BI 中导入它。

2.3: Open the file C:pbiqp\\script.R to see this (I highly recommend RStudio): 2.3:打开文件C:pbiqp\\script.R看看这个(我强烈推荐 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:上面的代码片段使用了来自Iris数据集的数据集,但我们将通过添加以下行来利用 PowerBI 文件中的可用数据:

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 .该行从我们将命名为Data的现有列 i PowerBI 文件构建数据框。 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. 2.4:完成您的自定义可视化。


In the folder C:\\pbiCustomR\\pbiDensity , run the following command using the command prompt:在文件夹C:\\pbiCustomR\\pbiDensity ,使用命令提示符运行以下命令:

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 部分 - 使用 R 在 PowerBI 中生成一些随机数据


3.1 Under the Home tab, click Edit Queries to open the Power Query Editor . 3.1在“ Home选项卡下,单击“ Edit Queries以打开Power Query Editor

3.2 Click Enter Data , and only click OK . 3.2单击Enter Data ,也是唯一一次点击OK

3.3 Select Transform > Run R Script and insert the following snippet: 3.3选择Transform > Run R Script并插入以下代码片段:

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.这将在Query Settings下产生一个名为"output"的新步骤,以及一个带有随机数的表,但不是世界上最好的列名。

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.4Applied Steps下的Applied Steps名称更改为tblRandom ,并将列名称更改为SampleData这样您最终会得到这样的结果:

在此处输入图片说明

3.5 Select Home > Close&Apply to get back to PowerBI Desktop. 3.5选择“ Home > Close&Apply以返回 PowerBI Desktop。


**Part 4 - Import and use your custom visualization **第 4 部分 - 导入和使用您的自定义可视化


4.1 Under Visualizations , click the icon with the three dots and select Import from file : 4.1Visualizations ,单击带有三个点的图标并选择Import from file

在此处输入图片说明

4.1 Navigate to C:\\pbiCustomR\\pbiDensity\\dist , select pbiDensity.pbiviz , click OK and a new icon should appear under Visualizations : 4.1导航到C:\\pbiCustomR\\pbiDensity\\dist ,选择pbiDensity.pbiviz ,单击OK ,一个新图标应出现在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 this is important为了使您的可视化的工作,你列的名称必须匹配您的[R脚本参考。 And in our case, they don't (just to make a point out of it).而在我们的案例中,他们没有(只是为了说明这一点)。 So change the name column SampleData to Data .因此,将名称列SampleData更改为Data

4.2 Click your new icon to insert a visualization placeholder, and drag your Data column to it: 4.2单击您的新图标以插入一个可视化占位符,并将您的Data列拖到它上面:

在此处输入图片说明

And there you go:然后你去:

在此处输入图片说明

Now you have the full flexibility of the plotly toolbar at your disposal:现在,您可以plotly工具栏的全部灵活性:

在此处输入图片说明


Part 5 - Edit R script, update and reimport custom visualization第 5 部分 - 编辑 R 脚本、更新和重新导入自定义可视化


5.1 Edit your script in RStudio and save it without changing the filename 5.1在 RStudio 中编辑您的脚本并在不更改文件名的情况下保存它

5.2 Navigate to your custom folder and run pbiviz package 5.2导航到您的自定义文件夹并运行pbiviz package

5.3 Delete your existing custom visual in PowerBI and import it again 5.3在 PowerBI 中删除您现有的自定义视觉对象并再次导入

The reason is that right now Power BI only supports render charts created by R visualization component as PNG.原因是目前 Power BI 仅支持由 R 可视化组件创建的渲染图表为 PNG。

Try the following:请尝试以下操作:

p <- plot_ly(x = dataset$period, y = dataset$mean, name = "spline", line = list(shape = "spline"))
plotly_IMAGE(p, format = "png", out_file = "out.png")

Change改变

pbiviz new pbiDensity -t html pbiviz 新 pbiDensity -t html

for为了

pbiviz new pbiDensity -t rhtml pbiviz 新 pbiDensity -t rhtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM