January 29, 2021
R and Power BI – Debugging
For Power BI version 2.76.5678.782 (December 2019).
When debugging scripts we usually follow values of variables. R script editor in Power BI unfortunately doesn’t provide a variable list, which can make debugging frustrating. We can use a trick proposed by Praveen N: adding a block of code to the script that writes out variables into a file. We can then open the file in RStudio and examine the variables.
We can insert the code in anywhere in R script in Power BI.
########## DEBUG IN R
filename = “C:/temp/Data.Rda”
if(file.exists(dirname(filename))){
if(Sys.getenv(“RSTUDIO”)!=””)
load(file = filename)
else
save(list = ls(all.names = TRUE), file = filename)
}
Example
Let’s look at dataset variable, which is always the name of incoming dataframe. We follow the steps below.
We have a simple table in Power Query. We open R script editor by selecting Run R script.
We enter the following commands.
Confirm with OK.
Open RStudio.
In RStudio open file from location C:/temp/Data.Rda.
Confirm with Yes to load all the variables.
Check the dataset variable.
Value of variable is printed out, in our case the original table.