在 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.