使用 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. 例如,在 R 指令碼中撰寫 dataset$gear 即可存取 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.
  • 在此版本中,32 位元版本的 Power BI Desktop 不會自動識別 RRO 安裝,因此您必須在 [選項及設定] > [選項] > [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.