将外部 R IDE 与 Power BI 一起使用Use an external R IDE with Power BI

借助 Power BI Desktop,可以使用外部的 R IDE(集成开发环境)创建并优化 R 脚本,然后将这些脚本用于 Power BI 中。With Power BI Desktop, you can use your external R IDE (Integrated Development Environment) to create and refine R scripts, then use those scripts in Power BI.

启用外部 R IDEEnable an external R IDE

在这之前,需使用 Power BI Desktop 中的 R 脚本编辑器创建并运行 R 脚本。Previously, you had to use the R script editor in Power BI Desktop to create and run R scripts. 借助此版本,可以从 Power BI Desktop 启动外部 R IDE,然后自动导入数据并显示在 R IDE 中。With this release, you can launch your external R IDE from Power BI Desktop and have your data automatically imported and displayed in the R IDE. 之后,可以修改此外部 R IDE 中的脚本,然后将其重新粘贴至 Power BI Desktop 中来创建 Power BI 视觉对象和报表。From there, you can modify the script in that external R IDE, then paste it back into Power BI Desktop to create Power BI visuals and reports.

Power BI Desktop(版本 2.39.4526.362)的 2016 年 9 月发行版开始,你可以指定使用哪个 R IDE,并使其在 Power BI Desktop 中自动启动。Beginning with the September 2016 release of Power BI Desktop (version 2.39.4526.362), you can specify which R IDE you would like to use, and have it launch automatically from within Power BI Desktop.

要求Requirements

要使用此功能,需要在本地计算机上安装 R IDETo use this feature, you need to install an R IDE on your local computer. Power BI Desktop 不包含、部署和安装 R 引擎,因此必须在本地计算机上独立安装 RPower BI Desktop does not include, deploy or install the R engine, so you must separately install R on your local computer. 通过以下选项,你可以选择使用哪个 R IDE:You can choose which R IDE to use, with the following options:

  • 你可以安装最喜欢的 R IDE,其中大部分都是免费的,例如 Revolution Open 下载页面CRAN 存储库You can install your favorite R IDE, many of which are available for free, such as the Revolution Open download page, and the CRAN Repository.
  • Power BI Desktop 还支持 R Studio 和具有 R Tools for Visual Studio 编辑器的 Visual Studio 2015Power BI Desktop also supports R Studio and Visual Studio 2015 with R Tools for Visual Studio editors.
  • 此外,还可以安装不同的 R IDE,并通过执行以下任一操作,使 Power BI Desktop 启动相应 R IDEYou can also install a different R IDE and have Power BI Desktop launch that R IDE by doing one of the following:

    • 可以将 .R 文件与 Power BI Desktop 要启动的外部 IDE 相关联。You can associate .R files with the external IDE you want Power BI Desktop to launch.
    • 通过从“ 选项 ”对话框的“ R 脚本选项 ”部分中选择“ 其他 ”,你可以指定 Power BI Desktop 应启动的 .exe。You can specify the .exe that Power BI Desktop should launch by selecting Other from the R Script Options section of the Options dialog. 通过转到“文件”>“选项和设置”>“选项”,你可以打开“选项”对话框。You can bring up the Options dialog by going to File > Options and settings > Options.

如果安装了多个 R Ide,则通过在“ 选项 ”对话框中“ 检测到的 R Ide ”下拉列表中进行选择就可以指定要启动哪个 R Ide。If you have multiple R IDEs installed, you can specify which will be launched by selecting it from the Detected R IDEs drop-down in the Options dialog.

默认情况下,Power BI Desktop 将启动 R Studio 作为外部 R IDE(如果它已安装在本地计算机上);如果未安装 R Studio,而是安装了具有 R Tools for Visual StudioVisual Studio 2015,则将启动 Visual Studio 2015。By default, Power BI Desktop will launch R Studio as the external R IDE if it's installed on your local computer; if R Studio is not installed and you have Visual Studio 2015 with R Tools for Visual Studio, that will be launched instead. 如果这些 R IDE 均未安装,则将启动与 .R 文件关联的应用程序。If neither of those R IDEs is installed, the application associated with .R files is launched.

如果 .R 文件不存在任何关联,则可以在“ 选项 ”对话框的“ 浏览到你的首选 R IDE ”部分中指定自定义 IDE 的路径。And if no .R file association exists, it's possible to specify a path to a custom IDE in the Browse to your preferred R IDE section of the Options dialog. 通过选择 Power BI Desktop 中“启动 R IDE”箭头图标旁边的“设置”齿轮图标,你还可以启动不同的 R IDE。You can also launch a different R IDE by selecting the Settings gear icon beside the Launch R IDE arrow icon, in Power BI Desktop.

通过 Power BI Desktop 启动 R IDELaunch an R IDE from Power BI Desktop

要从 Power BI Desktop 启动 R IDE,请执行以下步骤。To launch an R IDE from Power BI Desktop, take the following steps.

  1. 将数据加载至 Power BI DesktopLoad data into Power BI Desktop.
  2. 从“字段”窗格选择要使用的字段。Select some fields from the Fields pane that you want to work with. 如果尚未启用脚本视觉对象,则会提示你完成此操作。If you haven't enabled script visuals yet, you'll be prompted to do so.

  3. 启用脚本视觉对象后,可以从“可视化效果”窗格中选择 R 视觉对象,此操作将创建空白的 R 视觉对象来显示脚本结果。When script visuals are enabled, you can select an R visual from the Visualizations pane, which creates a blank R visual that's ready to display the results of your script. 同时也会显示“R 脚本编辑器”窗格。The R script editor pane also appears.

  4. 现在你可以选择要用于 R 脚本的字段。Now you can select the fields you want to use in your R script. 选择字段后,“R 脚本编辑器”字段会基于所选的字段自动创建脚本代码。When you select a field, the R script editor field automatically creates script code based on the field or fields you select. 可以直接在“R 脚本编辑器”窗格中创建(或粘贴)R 脚本,或者将其保留为空。You can either create (or paste) your R script directly in the R script editor pane, your you can leave it empty.

    备注

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

  5. 现在可以直接从 Power BI Desktop 中启动 R IDE。You can now launch your R IDE directly from Power BI Desktop. 如下图所示,从“R 脚本编辑器”标题栏的右侧找到并选中“启动 R IDE”按钮。Select the Launch R IDE button, found on the right side of the R script editor title bar, as shown below.

  6. 如下图所示,Power BI Desktop 将启动特定的 R IDE(在该图中,RStudio 是默认 R IDE)。Your specified R IDE is launched by Power BI Desktop, as shown in the following image (in this image, RStudio is the default R IDE).

    备注

    Power BI Desktop 会添加脚本的前三行,这样一旦运行该脚本,就可以从 Power BI Desktop 中导入数据。Power BI Desktop adds the first three lines of the script so it can import your data from Power BI Desktop once you run the script.

  7. Power BI DesktopR 脚本编辑器窗格中创建的任何脚本都会从 R IDE 的第 4 行开始显示。Any script you created in the R script editor pane of Power BI Desktop appears starting in line 4 in your R IDE. 此时,可以在 R IDE 中创建自己的 R 脚本。At this point you can create your R script in the R IDE. 在 R IDE 中完成 R 脚本后,必须将其复制并重新粘贴至 Power BI DesktopR 脚本编辑器 窗格中,注意 不包括 Power BI Desktop 自动生成的前三行脚本。Once your R script is complete in your R IDE, you need to copy and paste it back into the R script editor pane in Power BI Desktop, excluding the first three lines of the script that Power BI Desktop automatically generated. 请勿将脚本的前三行复制到 Power BI Desktop,这三行仅用于将数据从 Power BI Desktop 导入到 R IDE。Do not copy the first three lines of script back into Power BI Desktop, those lines were only used to import your data to your R IDE from Power BI Desktop.

已知限制Known limitations

直接从 Power BI Desktop 中启动 R IDE 具有部分限制:Launching an R IDE directly from Power BI Desktop has a few limitations:

  • 不支持自动将脚本从 R IDE 中导出到 Power BI DesktopAutomatically exporting your script from your R IDE into Power BI Desktop is not supported.
  • 不支持 R 客户端编辑器 (RGui.exe),因为该编辑器自身不支持打开文件。R Client editor (RGui.exe) is not supported, because the editor itself does not support opening files.

后续步骤Next steps

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