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

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

问题描述

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

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

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

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"))

错误消息:

未创建图像. R代码未创建任何视觉效果.确保您的R脚本可以绘制R默认设备的图.

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.

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

But runs perfectly on my R desktop. Any thought?

推荐答案

对于较新版本的PowerBI,也可以使用Rggplot作为 custom PowerBI visualizations 生成Plotly图表>.使用下面描述的方法,可以从PowerBI表生成密度图,如下所示:

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:

资源:

我建议的解决方案使用 nodejs ,可在此处找到.那以及我建议的主要部分都建立在这篇出色的博文,在有关使用和更新 custom PowerBI Visualizations 的详细信息方面仅存在一些不足.我可以引用该链接并指出我所做的不同操作,但是为了清楚和完整起见,我从头开始做了整个事情.

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.

第1部分-下载并安装node.js

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

1.2::重新启动计算机,启动命令提示符,然后运行以下命令:

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

npm install -g powerbi-visuals-tools

1.3:通过在命令提示符下运行以下命令来检查安装是否成功:

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

输入:

pbiviz

输出:

第2部分:制作PowerBI自定义图片

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

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

这将创建一个新的视觉效果并安装一些必需的软件包.您可以将pbiDensity更改为所需的任何内容.

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

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

2.2: Navigate to the folder C:\pbiCustomR\pbiDensity and take a closer look at the contents.

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

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:打开文件C:pbiqp\script.R进行查看(我强烈推荐RStudio):

2.3: Open the file C:pbiqp\script.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');
####################################################

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

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)

该行从PowerBI文件的现有列(我们将其命名为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:.完成自定义可视化.


2.4: Finish your custom visualization.

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

In the folder C:\pbiCustomR\pbiDensity, run the following command using the command prompt:

pbiviz package

这将触发此输出:

就更高级的东西而言就是这样.其余的很简单!

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

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

3.1 Home选项卡下,单击Edit Queries以打开Power Query Editor.

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

3.2 单击Enter Data,然后 单击OK.

3.3 选择Transform > Run R Script并插入以下代码段:

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

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

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

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 Applied Steps下的步骤名称更改为tblRandom,并将列名称更改为SampleData,因此您将得到以下结果:

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 选择Home > Close&Apply返回PowerBI Desktop.

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

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

**Part 4 - Import and use your custom visualization

4.1 Visualizations下,单击带有三个点的图标,然后选择Import from file:

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

4.1 导航到C:\pbiCustomR\pbiDensity\dist,选择pbiDensity.pbiviz,单击OK,新图标应出现在Visualizations下:

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

And this is important:为了使可视化工作正常,列的名称 必须 与R脚本中的引用匹配.在我们的案例中,他们没有这样做(只是为了指出一点).因此,将名称列SampleData更改为Data.

4.2 单击新图标以插入可视化占位符,然后将您的Data列拖到该占位符:

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

然后去:

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

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

第5部分-编辑R脚本,更新并重新导入自定义可视化效果

5.1 在RStudio中编辑脚本并保存而不更改文件名

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

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

5.2 Navigate to your custom folder and run pbiviz package

5.3 在PowerBI中删除现有的自定义视觉效果,然后再次导入

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

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

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