在 Power BI 中使用 R 驱动的自定义视觉对象Use R-powered custom visuals in Power BI

在 Power BI Desktop 和 Power BI 服务中,无需掌握任何 R 知识,也无需使用任何 R 脚本,即可使用 R 驱动的自定义视觉对象。In Power BI Desktop and the Power BI service, you can use R-powered custom visuals without any knowledge of R, and without any R scripting. 这样一来,无需学习任何 R 知识,也无需自行进行任何编程,即可利用 R 视觉对象和 R 脚本的分析和视觉功能。This enables you to harness the analytic and visual power of R visuals, and R scripts, without learning R or doing any programming yourself.

若要使用 R 驱动的自定义视觉对象,请先从 Power BI 自定义视觉对象的 AppSource 库中选择并下载要使用的 R 自定义视觉对象。To use R-powered custom visuals, you first select and download the R custom visual you're interested in using from the AppSource gallery of custom visuals for Power BI.

以下部分介绍了如何在 Power BI Desktop 中选择、加载和使用 R 驱动的视觉对象。The following sections describe how to select, load, and use R-powered visuals in Power BI Desktop.

使用 R 自定义视觉对象Use R custom visuals

若要使用 R 驱动自定义视觉对象,你需要从自定义视觉对象库下载每个视觉对象,然后可以在 Power BI Desktop 中如使用其他任何类型的视觉对象一样来使用此视觉对象。To use R-powered custom visuals, you need to download each visual from the custom visuals library, then you can use the visual like any other type of visual in Power BI Desktop. 获取自定义视觉对象的方法有两种,可以从在线 AppSource 网站下载视觉对象,也可以在 Power BI Desktop 中浏览并获取视觉对象。There are two ways to get custom visuals - you can download them from the online AppSource site, or you browse and get them from within Power BI Desktop.

从 AppSource 获取自定义视觉对象Get custom visuals from AppSource

若要在在线 AppSource 网站中浏览并选择视觉对象,请按照以下步骤操作:Here are the steps to browse and select visuals from the online AppSource site:

  1. 转到 Power BI 视觉对象库 (https://appsource.microsoft.com)。选中“按产品细分”下的“Power BI 应用”复选框,再单击“查看全部”链接。Navigate to the Power BI visuals library, found at https://appsource.microsoft.com. Select the Power BI apps checkbox under Refine by product, then select the See all link.

  2. 在左侧窗格内的加载项列表中,选择“Power BI 视觉对象”。Select Power BI visuals from the list of Add-ins in the left pane.

  1. 从库中选择要使用的视觉对象,此时将会转到视觉对象的说明页。Select the visual you're interested in using from the gallery, and you're taken to a page that describes the visual. 选择“立即获取”按钮,下载视觉对象。Select the Get it now button to download.

    备注

    若要在 Power BI Desktop 中创作,需要在本地计算机上安装 R。For authoring in Power BI Desktop, you need to have R installed on your local machine. 但用户要在 Power BI 服务中查看 R 驱动的视觉对象时,他们不需要在本地安装 R。But when users want to view an R-powered visual in the Power BI service they do not need R installed locally.

    无需安装 R 即可在 Power BI 服务中使用 R 驱动的自定义视觉对象。不过,如果要在 Power BI Desktop 中使用 R 驱动的自定义视觉对象,则必须在本地计算机上安装 R。You do not need to install R to use R-powered custom visuals in the Power BI service, however, if you want to use R-powered custom visuals in Power BI Desktop you must install R on the local machine. 可以从以下位置下载 R:You can download R from the following locations:

  2. 下载视觉对象(与在浏览器中下载任何文件一样)后,立即转到“Power BI Desktop”,右键单击“可视化效果”窗格中的省略号 (...),再选择“从文件导入”。Once the visual is downloaded (which is like downloading any file from your browser), go to Power BI Desktop and right-click the ellipsis (the ...) in the Visualizations pane, and select Import from file.

  3. 导入自定义视觉对象时的注意事项,具体如下图所示:You're cautioned about importing a custom visuals, as shown in the following image:

  4. 导航到视觉对象文件保存的位置,然后选择该文件。Navigate to where the visual file was saved, then select the file. Power BI Desktop 自定义可视化效果具有.pbiviz 扩展名。Power BI Desktop custom visualizations have the .pbiviz extension.

  5. 返回 Power BI Desktop 时,你可以在“可视化效果”窗格中看到新的视觉对象类型。When you return to Power BI Desktop, you can see the new visual type in the Visualizations pane.

  6. 在你导入新的视觉对象(或打开包含 R 驱动的自定义视觉对象的报表)后,Power BI Desktop 便会安装相应的 R 包。When you import the new visual (or open a report that contains a R-powered custom visual), Power BI Desktop installs the required R packages.

在这里,你将数据添加到视觉对象,如添加到任何其他 Power BI Desktop 视觉对象一样。From there, you can add data to the visual just as you would any other Power BI Desktop visual. 完成后,你可以在画布上看到完成的视觉对象。When complete, you can see your finished visual on the canvas. 在下面的视觉对象中,R 驱动的视觉对象 Forecasting 与联合国 (UN) 出生速率预测一同使用(左侧的视觉对象)。In the following visual, the Forecasting R-powered visual was used with United Nations (UN) birth rate projections (the visual on the left).

如任何其他 Power BI Desktop 视觉对象一样,你可以将此报表及其 R 驱动的视觉对象发布到 Power BI 服务并与他人共享。Like any other Power BI Desktop visual, you can publish this report with its R-powered visuals to the Power BI service and share it with others.

请经常检查库,因为会随时添加新视觉对象。Check the library often, since new visuals are being added all the time.

在 Power BI Desktop 中获取自定义视觉对象Get custom visuals from within Power BI Desktop

也可以从 Power BI Desktop 获取自定义视觉对象。You can also get custom visuals from within Power BI Desktop. 在“Power BI Desktop”中,右键单击“可视化效果”窗格中的省略号 (...),再选择“从存储导入”。In Power BI Desktop right-click the ellipsis (the ...) in the Visualizations pane, and select Import from store.

此时,将会看到“Power BI 自定义视觉对象”对话框。在此对话框中,可以滚动浏览可用自定义视觉对象,并能选择要使用的视觉对象。When you do so, the Power BI Custom Visuals dialog appears, where you can scroll through the available custom visuals and select what you would like. 可以按名称搜索,可以选择类别,也可以直接滚动浏览可用视觉对象。You can search by name, select a catagory, or just scroll through the available visuals. 准备就绪后,只需选择“添加”,即可将自定义视觉对象添加到“Power BI Desktop”。When you're ready, just select Add to add the custom visual to Power BI Desktop.

参与 R 驱动的自定义视觉对象Contribute R-powered custom visuals

如果你创建自己的 R 视觉对象以供在报表中使用,则可以向自定义视觉对象库提供你的自定义视觉对象,从而与全世界共享你的视觉对象。If you create your own R visuals for use in your reports, you can share those visual with the world by contributing your custom visual to the custom visuals gallery. 通过 GitHub 进行贡献,相应过程在以下位置有所讲述:Contributions are made through GitHub, and the process is outlined in the following location:

R 驱动的自定义视觉对象疑难解答Troubleshoot R-powered custom visuals

必须满足 R 驱动的自定义视觉对象的特定依存关系,视觉对象才能正常运行。R-powered custom visuals have certain dependencies that must be met for the visuals to work properly. 如果 R 驱动的自定义视觉对象未运行或未正常加载,通常是出现下面的一种问题:When R-powered custom visuals don't run or load properly, the problem is usually one of the following:

  • R 引擎缺失The R engine is missing
  • 视觉对象基于的 R 脚本出错了Errors in the R script on which the visual is based
  • R 包缺失或已过期R packages are missing or out of date

下面的部分介绍了你可以用来解决所遇到的问题的故障排除步骤。The following section describes troubleshooting steps you can take to help address trouble you run into.

R 包缺失或已过期Missing or outdated R packages

在尝试安装 R 驱动的自定义视觉对象时,如果 R 包缺失或已过期,则可能会看到错误;通常可能是由于如下一种原因所致:When attempting to install an R-powered custom visual, you can run into errors when there are missing or outdated R packages; this is usually due to one of the following reasons:

  • R 安装与 R 包不兼容The R installation is incompatible with the R package,
  • 防火墙、防病毒软件或代理设置阻止 R 连接 InternetA firewall, anti-virus software, or proxy settings are preventing R from connecting to the Internet
  • Internet 连接速度慢,或无法连接 InternetThe Internet connection is slow, or there's an Internet connection problem

Power BI 团队正在努力降低你遇到这些问题的可能性,下一版 Power BI Desktop 将包含用于解决这些问题的更新程序。The Power BI team is actively working on mitigating these issues before they reach you, and the next Power BI Desktop will incorporate updates to address these problems. 在此之前,你可以按以下一个或多个步骤操作,从而降低遇到这些问题的可能性:Until then, you can take one or more of the following steps to mitigate the issues:

  1. 删除自定义视觉对象,然后重新安装。Remove the custom visual, then install it again. 这会重新开始安装 R 包。This initiates a re-installation of the R packages.
  2. 如果安装的 R 不是最新版,请升级 R 安装,然后删除/重新安装自定义视觉对象,如上一步所述。If your installation of R is not current, upgrade your R installation, then remove/re-install the custom visual as described in the previous step.

    • 每个 R 驱动的自定义视觉对象的描述中列出了支持的 R 版本,如下图所示。Supported R versions are listed in the description of each R-powered custom visual, as shown in the following image. > [!NOTE] > 可以保留原始 R 安装,仅将 Power BI Desktop 与当前安装的版本相关联。You can keep the original R installation, and only associate Power BI Desktop with the current version you install. 依次转到“文件 > 选项和设置 > 选项 > R 脚本Go to File > Options and settings > Options > R scripting.
  3. 使用任意 R 控制台手动安装 R 包。Install R packages manually, using any R console. 此方法的具体步骤如下所示:The steps for this approach are the following:

    a.a. 下载 R 驱动的视觉对象安装脚本,然后将该文件保存到本地驱动器。Download the R-powered visual installation script, and save that file to a local drive.

    b.b. 在 R 控制台中运行以下命令:From the R console, run the following:

    > source(“C:/Users/david/Downloads/ScriptInstallPackagesForForecastWithWorkarounds.R”)    
    

    典型的默认安装位置如下所示:Typical default installation locations are the following:

    c:\Program Files\R\R-3.3.x\bin\x64\Rterm.exe (for CRAN-R)
    c:\Program Files\R\R-3.3.x\bin\x64\Rgui.exe (for CRAN-R)
    c:\Program Files\R\R-3.3.x\bin\R.exe (for CRAN-R)
    c:\Program Files\Microsoft\MRO-3.3.x\bin\R.exe (for MRO)
    c:\Program Files\Microsoft\MRO-3.3.x\bin\x64\Rgui.exe (for MRO)
    c:\Program Files\RStudio\bin\rstudio.exe (for RStudio)
    
  4. 如果上述步骤不起作用,请尝试按以下步骤操作:If the previous steps don't work, try the following:

    a.a. 使用 R Studio,按上述 3.b. 中所述的步骤操作Use R Studio and follow the step outlined in 3.b. (在 R 控制台中运行脚本代码)。above (run the script line from the R console).

    b.b. 如果上一步不起作用,请更改 R Studio 中的“工具 > 全局选项 > 包”,并选中将 Internet Explorer 库/代理用于 HTTP 复选框,然后重复执行If the previous step doesn't work, change Tools > Global Options > Packages in R Studio, and enable the checkbox for Use Internet Explorer library/proxy for HTTP, then repeat step 3.b. 上述 3.b. 中的步骤。from the above steps.

后续步骤Next steps

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