在 Power BI Desktop 中运行 R 脚本Run R scripts in Power BI Desktop

可以直接在 Power BI Desktop 中运行 R 脚本并将所得数据集导入 Power BI Desktop 数据模型。You can run R scripts directly in Power BI Desktop, and import the resulting datasets into a Power BI Desktop data model.

安装 RInstall R

若要在 Power BI Desktop 中运行 R 脚本,需要在本地计算机上安装 RTo run R scripts in Power BI Desktop, you need to install R on your local machine. 可以从很多位置免费下载并安装 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 脚本Run R scripts

在 Power BI Desktop 中只需几个步骤即可运行 R 脚本并创建数据模型,从中你可创建报表并在 Power BI 服务上共享它们。With just a few steps in Power BI Desktop you can run R scripts and create a data model, from which you can create reports, and share them on the Power BI service. Power BI Desktop 中的 R 脚本现在支持包含小数点 (.) 和逗号 (,) 的数字格式。R scripting in Power BI Desktop now supports number formats that contain decimals (.) and commas (,).

准备 R 脚本Prepare an R script

若要在 Power BI Desktop 中运行 R 脚本,请在本地 R 开发环境中创建脚本并确保其已成功运行。To run an R script in Power BI Desktop, create the script in your local R development environment, and make sure it runs successfully.

若要在 Power BI Desktop 中运行脚本,请确保该脚本可在未修改的新工作区中成功运行。To run the script in Power BI Desktop, make sure the script runs successfully in a new and unmodified workspace. 这意味着必须以显式方式加载和运行所有包和依赖项。This means that all packages and dependencies must be explicitly loaded and run. 可以使用 source() 运行依赖脚本。You can use source() to run dependent scripts.

在 Power BI Desktop 中准备和运行 R 脚本时,会有一些限制:When preparing and running an R script in Power BI Desktop, there are a few limitations:

  • 仅会导入数据帧,因此请确保要导入到 Power BI 的数据都位于数据帧中Only data frames are imported, so make sure the data you want to import to Power BI is represented in a data frame
  • 不会导入类型为“复杂”和“向量”的列,且会在创建的表中用错误值将其取代。Columns that are typed as Complex and Vector are not imported, and are replaced with error values in the created table.
  • N/A 值将被转换为 Power BI Desktop 中的 NULL 值Values that are N/A are translated to NULL values in Power BI Desktop
  • 任何 R 脚本若运行时间超过 30 分钟就会超时Any R script that runs longer than 30 minutes times out
  • R 脚本中的交互式调用(例如等待用户输入)会终止脚本运行Interactive calls in the R script, such as waiting for user input, halts the script’s execution
  • 在 R 脚本中设置工作目录时,必须定义工作目录的完整路径,而非相对路径When setting the working directory within the R script, you must define a full path to the working directory, rather than a relative path

运行 R 脚本并导入数据Run your R script and import data

  1. 在 Power BI Desktop 中,可在获取数据中找到 R 脚本数据连接器。In Power BI Desktop, the R Script data connector is found in Get Data. 若要运行 R 脚本,请选择获取数据 > 更多...,然后选择其他 > R 脚本,如下图所示。To run your R Script, select Get Data > More..., then select Other > R Script as shown in the following image.

  2. 如果本地计算机上安装了 R,则会选择已安装的最新版本作为 R 引擎。If R is installed on your local machine, the latest installed version is selected as your R engine. 只需将脚本复制到脚本窗口,然后选择确定Simply copy your script into the script window and select OK.

  3. 如果 R 尚未安装、无法识别,或者如果本地计算机上有多个安装,则展开 R 安装设置以显示安装选项或选择你想要用于运行 R 脚本的安装。If R is not installed, is not identified, or if there are multiple installations on your local machine, expand R Installation Settings to display installation options, or to select which installation you want to run the R script.

    如果已安装 R 但无法识别,则可以在展开 R 安装设置时所提供的文本框中以显式形式输入其位置。If R is installed is not identified, you can explicitly provide its location in the text box provided when you expand R Installation Settings. 在上图中,已在文本框中以显式形式输入路径 C:\Program Files\R\R-3.2.0In the above image, the path C:\Program Files\R\R-3.2.0 is explicitly provided in the text box.

    R 安装设置集中位于“选项”对话框的 R 脚本部分。R installation settings are centrally located in the R Scripting section of the Options dialog. 若要指定 R 安装设置,请选择文件 > 选项和设置,然后选择选项 > R 脚本To specify your R installation settings, select File > Options and settings and then Options > R Scripting. 如果有多个 R 安装可用,则会显示一个下拉菜单,让你选择要使用的安装。If multiple installations of R are available, a drop-down menu appears that allows you to select which installation to use.

  4. 选择确定运行 R 脚本。Select OK to run the R Script. 脚本成功运行后,即可选择要将其添加到 Power BI 模型的所得数据帧。When the script runs successfully, you can then choose the resulting data frames to add to the Power BI model.

刷新Refresh

你可以在 Power BI Desktop 中刷新 R 脚本。You can refresh an R script in Power BI Desktop. 刷新 R 脚本时,Power BI Desktop 会再次在 Power BI Desktop 环境中运行 R 脚本。When you refresh an R script, Power BI Desktop runs the R script again in the Power BI Desktop environment.

后续步骤Next steps

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