使用 R 创建 Power BI 视觉对象Create Power BI visuals using R

借助 Power BI Desktop,可以使用 R 将数据可视化。With Power BI Desktop, you can use R to visualize your data.

安装 RInstall R

Power BI Desktop 既不包括,也不会部署或安装 R 引擎。Power BI Desktop does not include, deploy or install the R engine. 若要在 Power BI Desktop 中运行 R 脚本,必须在本地计算机上单独安装 RTo run R scripts in Power BI Desktop, you must separately install R on your local computer. 可以从很多位置免费下载并安装 R,其中包括 Revolution Open 下载页,以及 CRAN 存储库You can download and install R for free from many locations, including the Revolution Open download page, and the CRAN Repository. Power BI Desktop 中的最新版 R 脚本支持在安装路径中使用 Unicode 字符以及空格(空字符)。The current release of R scripting in Power BI Desktop supports Unicode characters as well as spaces (empty characters) in the installation path.

启用 R 视觉对象Enable R visuals

若要启用 R 视觉对象,请选择文件 > 选项和设置 > 选项,并在出现的选项页中确保本地 R 安装在选项窗口的 R 脚本部分中指定,如下图所示。To enable R visuals, select File > Options and settings > Options and in the Options page that appears, make sure your local R installation is specified in the R Scripting section of the Options window, as shown in the following image. 下图中,R 的本地安装路径是 C:\Program Files\R\R-3.2.0,并在文本框中显式提供该路径。In the following image, the path local installation of R is C:\Program Files\R\R-3.2.0 and that path is explicitly provided in the text box. 请确保显示的路径正确地反映了想要 Power BI Desktop 使用的本地 R 安装。Make sure the path it displays properly reflects the local R installation you want Power BI Desktop to use.

一旦指定 R 安装,就可以开始创建 R 视觉对象。Once you specify your R installation, you’re ready to begin creating R visuals.

在 Power BI Desktop 中创建 R 视觉对象Create R visuals in Power BI Desktop

  1. 可视化效果窗格中选择 R 视觉对象图标以添加 R 视觉对象,如下图中所示。Select the R Visual icon in the Visualization pane, as shown in the following image, to add an R visual.

  2. 向报表添加 R 视觉对象后,Power BI Desktop 执行以下任务:When you add an R visual to a report, Power BI Desktop does the following:

    • 占位符 R 视觉对象图像位于报表画布上。A placeholder R visual image appears on the report canvas.

    • R 脚本编辑器位于中央窗格底部边缘处。The R script editor appears along the bottom of the center pane.

  3. 接下来,在“字段”的“”部分中添加要在 R 脚本中使用的字段,就像处理其他任何 Power BI Desktop 视觉对象一样。Next, add fields you want to consume in your R script to the Values section in the Fields well, just as you would with any other Power BI Desktop visual. 只有已添加到“字段”的字段才可供 R 脚本使用。可以在 Power BI Desktop R 脚本编辑器中处理 R 脚本的同时,添加新字段,或从“字段”中删除不必要的字段。Only fields that have been added to the Fields well are available to your R script, and you can add new fields, or remove unneeded fields from the Fields well while working on your R script in the Power BI Desktop R script editor. Power BI Desktop 会自动检测已添加或删除的字段。Power BI Desktop automatically detects which fields you have added or removed.

    备注

    R 视觉对象的默认聚合类型是“不汇总”。The default aggregation type for R visuals is do not summarize.

  4. 现在你可以使用你选择用来创建绘图的数据。Now you can use the data you selected to create a plot. 选择了字段,R 脚本编辑器将基于编辑器窗格顶部旁边灰色部分中的选择生成支持 R 脚本绑定代码。As you select fields, the R script editor generates supporting R script binding code based on your selections in the gray section along the top of the editor pane. 随着你选择或删除其他字段,R 脚本编辑器中的支持代码将相应地被自动生成或删除。As you select or remove additional fields, supporting code in the R script editor is automatically generated or removed accordingly.

    下图所示示例中,选择了三个字段:hp、gear 和 drat。In the example shown in the following image, three fields were selected: hp, gear, and drat. 由于这些选择,R 脚本编辑器将生成以下绑定代码:As a result of those selections, the R script editor generated the following binding code:

    • 创建了名为数据集的数据帧A dataframe called dataset was created
      • 该数据帧由用户选择的不同字段组成That dataframe is comprised of the different fields selected by the user
    • 默认聚合是不求和The default aggregation is do not summarize
    • 类似于表格视觉对象,字段将进行分组,并且重复行只出现一次Similar to table visuals, fields are grouped and duplicate rows only appear once

    提示

    在某些情况下,你可能不希望进行自动分组,或者可能希望所有行都出现,包括重复项。In certain cases you may not want automatic grouping to occur, or you may want all rows to appear, including duplicates. 在这种情况下,你可以向将导致所有行被视为唯一且阻止分组的数据集添加索引字段。In that case you can add an index field to your dataset which causes all rows to be considered unique, and prevents grouping.

    生成的数据帧被称为数据集,并且可以通过相应所选列名称访问所选列。The generated dataframe is called dataset, and selected columns can be accessed by their respective names. 例如,gear 字段可通过在 R 脚本中编写 dataset$gear 进行访问。For example, the gear field can be accessed by writing dataset$gear in your R script. 对于具有空格或特殊字符的字段,请使用单引号。For fields with spaces or special characters, use single quotes.

  5. 借助所选字段自动生成的数据帧,就可以编写将导致绘制到 R 默认设备的 R 脚本。With the dataframe automatically generated by the fields you selected, you’re ready to write R script that results in plotting to the R default device. 该脚本完成时,从 R 脚本编辑器标题栏选择运行运行位于标题栏右侧)。When the script is complete, select Run from the R script editor title bar (Run is on the right side of the title bar).

    选择“运行”后,Power BI Desktop 会识别绘图,并在画布上予以呈现。When Run is selected, Power BI Desktop identifies the plot and present it on the canvas. 由于该过程是在本地 R 安装上执行,请确保安装了所需的包。Since the process is executed on your local R installation, make sure required packages are installed.

    当以下任一事件发生时,Power BI Desktop 会重新绘制视觉对象:Power BI Desktop replots the visual when any of the following events occur:

    • R 脚本编辑器标题栏选择运行Run is selected from the R script editor title bar
    • 每当数据更改发生时(由于数据刷新、筛选或突出显示所导致)Whenever a data change occurs, due to data refresh, filtering, or highlighting

下图显示相关绘图代码的示例,并绘制不同类型汽车特征间的关联。The following image shows an example of the correlation plot code, and plots the correlations between attributes of different types of cars.

若要获取可视化效果的较大视图,你可以尽量减小 R 脚本编辑器To get a larger view of the visualizations, you can minimize the R script editor. 当然,如同 Power BI Desktop 中的其他视觉对象,你可以只选择环形视觉对象(在上面的示例图像中,为右侧的圆形视觉对象)中的跑车,交叉筛选相关绘图。And of course, like other visuals in Power BI Desktop, you can cross filter the correlation plot by selecting only sport cars in the donut visual (the round visual on the right, in the above example image).

还可修改 R 脚本,自定义视觉对象,并通过向绘图命令添加参数来利用 R 的强大功能。You can also modify the R script to customize the visual, and take advantage of the power of R by adding parameters to the plotting command.

原始的绘图命令如下所示:The original plotting command was the following:

corrplot(M, method = "color",  tl.cex=0.6, tl.srt = 45, tl.col = "black")

R 脚本中进行了一些更改,该命令现如下所示:With a few changes in the R script, the command is now the following:

corrplot(M, method = "circle", tl.cex=0.6, tl.srt = 45, tl.col = "black", type= "upper", order="hclust")

结果是,R 视觉对象现在绘制圆形,只考虑了上半部分,对矩形图重新排序以集中相关特征,如下图中所示。As a result, the R visual now plots circles, only considers at the upper half, and reorders the matrix to cluster correlated attributes, as shown in the following image.

如果执行 R 脚本时导致错误,则不会绘制 R 视觉对象,并且画布上将显示一条错误消息。When executing a R script that results in an error, the R visual is not plotted and an error message is displayed on the canvas. 有关错误详细信息,请从画布上 R 视觉对象选择查看详细信息For details on the error, select See details from the R visual error on the canvas.

R 脚本安全性:R 视觉对象从 R 脚本创建,这可能包含具有安全风险或隐私风险的代码。R scripts security: R visuals are created from R scripts, which could contain code with security or privacy risks. 当尝试查看 R 视觉对象或首次与其交互时,用户会看到一条安全警告消息。When attempting to view or interact with an R visual the first time, a user is presented with a security warning message. 仅当你信任作者和来源,或者在查看并了解 R 脚本之后,才启用 R 视觉对象。Only enable R visuals if you trust the author and source, or after you review and understand the R script.

已知限制Known limitations

Power BI Desktop 中的 R 视觉对象有一些限制:R visuals in Power BI Desktop has a few limitations:

  • 数据大小限制 - R 视觉对象用于绘制的数据仅限 150,000 行。Data size limitations – data used by the R visual for plotting is limited to 150,000 rows. 如果选择了 150,000 行以上,则只会使用前 150,000 行,且在图像上显示一条消息。If more than 150,000 rows are selected, only the top 150,000 rows are used and a message is displayed on the image.
  • 计算时间限制 - 如果 R 视觉对象计算时间超过 5 分钟,则执行将超时,从而导致错误。Calculation time limitation – if an R visual calculation exceeds 5 minutes the execution times out, resulting in an error.
  • 关系 - 如同其他 Power BI Desktop 视觉对象,如果选择的不同表中数据字段间没有定义关系,则会发生错误。Relationships – as with other Power BI Desktop visuals, if data fields from different tables with no defined relationship between them are selected, an error occurs.
  • R 视觉对象在数据更新、筛选和突出显示时进行刷新。R visuals are refreshed upon data updates, filtering, and highlighting. 但是,图像本身不是交互的并且不能为交叉筛选的源。However, the image itself is not interactive and cannot be the source of cross-filtering.
  • R 视觉对象响应突出显示的其他视觉对象,但你不能单击 R 视觉对象中的元素以进行其他元素的交叉筛选。R visuals respond to highlighting other visuals, but you cannot click on elements in the R visual in order to cross filter other elements.
  • 只有绘制到 R 默认显示设备的绘图会正确地显示在画布上。Only plots that are plotted to the R default display device are displayed correctly on the canvas. 避免显式使用不同的 R 显示设备。Avoid explicitly using a different R display device.
  • 在此版本中,RRO 安装未由 32 位版本的 Power BI Desktop 自动标识,因此你必须在选项和设置 > 选项 > R 脚本中手动提供 R 安装目录的路径。In this release, RRO installations are not automatically identified by the 32-bit version of Power BI Desktop, so you must manually provide the path to the R installation directory in Options and settings > Options > R Scripting.

后续步骤Next steps

查看以下更多信息,了解有关 Power BI 中的 R。Take a look at the following additional information about R in Power BI.