You can use an R dataset in Power BI by running an R script that reads the data and outputs a data frame, which Power BI then turns into a table. The short version: install R, enable R scripting in Power BI, add an R script data source, and write a script that ends with a data frame. This guide walks through each step, plus what to check when the data does not appear as expected.

What You'll Need

  • Power BI Desktop installed on Windows (the R script connector is not available in Power BI Service or on Mac).
  • An R distribution installed locally, such as CRAN R. Power BI does not ship with R.
  • Any R packages your script uses, installed in that R environment.
  • Your dataset, either as a file on disk (CSV, Excel, RDS) or as a built-in R dataset.
  • Basic familiarity with R syntax and data frames.

Note that menu labels and dialog wording can differ slightly between Power BI Desktop versions and regions. The steps below use the standard English interface.

Step-by-Step

  1. Install R. Download the Windows installer from the official CRAN website and run it with default options. Remember the installation folder; you will point Power BI to it later.
  2. Install any required packages. Open RGui or an R console and run install.packages("readr") (or whichever package you need). Packages must be installed in the same R installation that Power BI will use.
  3. Tell Power BI where R lives. In Power BI Desktop, open File > Options and settings > Options. Under Global, select R scripting. Set the Detected R home directories and Detected R IDEs fields to your R folder (for example, C:\Program Files\R\R-4.x.x). Click OK.
  4. Start a new R script data source. On the Home ribbon, click Get data, search for R script, and select it. Click Connect. (If you do not see it, check that you are in Power BI Desktop, not the Service.)
  5. Write your script. In the R script window, enter code that reads your data and ends with a data frame. For a built-in dataset, for example: df <- mtcars. For a file: df <- read.csv("C:/Data/sales.csv"). Use forward slashes or double backslashes in paths. The last object in the script must be a data frame; Power BI uses that as the table.
  6. Load the data. Click OK. In the Navigator pane, select the table Power BI detected (often named after your data frame) and click Load, or Transform Data if you want to clean it first.
  7. Check the result. In the Data view, confirm the columns and types look right. If a column came in as text but should be numeric, use Transform Data to change the type.
  8. Refresh when the source changes. If your R script reads a file, updating the file does not automatically update the report. Click Refresh on the Home ribbon to re-run the script and reload the data.

Common Mistakes

  • Script does not end with a data frame. Power BI only imports the last object if it is a data frame. Assign your result to a variable and leave that variable as the final line.
  • R path not set. If Power BI cannot find R, the R script connector fails. Recheck the folder in Options > R scripting.
  • Package not installed. A script that works in RGui can fail in Power BI if the package was installed in a different R library. Install packages in the same R installation Power BI points to.
  • Backslashes in file paths. R treats a single backslash as an escape character. Use forward slashes or double backslashes.
  • Expecting automatic refresh. R script data sources refresh only when you refresh the report or dataset; they are not live connections.
  • Trying this in Power BI Service. The R script connector is a Desktop feature. Publishing a report that uses it requires a gateway or a different data source for scheduled refresh.

Tips

  • Keep the script simple: read, clean, output. Complex scripts are harder to debug inside Power BI.
  • Use readr::read_csv() for faster and more predictable CSV imports than base read.csv().
  • Name your final data frame clearly; the Navigator often uses that name.
  • Test the script in RGui first. If it fails there, it will fail in Power BI.
  • For repeated use, save the script in a text file and paste it in, so you can version it.
  • If you need to combine multiple R outputs, run separate R script queries and relate them in the model.

Troubleshooting

If the R script connector is missing, confirm you are using Power BI Desktop on Windows and that R is installed. If you get an error about R not being found, reopen Options > R scripting and correct the home directory. If the script runs but no table appears, check that the last line returns a data frame and that column names are valid. If a package error appears, install the package in the correct R library and restart Power BI Desktop. If the data loads but types are wrong, use Transform Data to set types manually. If refresh fails after publishing, remember that scheduled refresh of R script sources needs an on-premises data gateway configured for R; otherwise refresh manually in Desktop and republish.

Frequently Asked Questions

Can I use an R dataset in Power BI Service?

You can view a published report, but the R script connector itself runs in Power BI Desktop. Scheduled refresh in the Service requires a gateway set up for R.

Does Power BI include R?

No. You install R separately from CRAN and point Power BI to it in Options.

Why does my R script return no table?

The last object in the script must be a data frame. Assign your result to a variable and end with that variable.

Can I use R visuals as well as R data sources?

Yes, Power BI supports R visuals, but they are separate from the R script data source and have their own setup.

How do I update data from an R script?

Click Refresh in Power BI Desktop. The script re-runs and reloads the data.

Do I need to know R to do this?

Yes, at least basic R. You need to write a script that reads your data and outputs a data frame.