One of the new features that Microsoft announced in February’s Power BI Desktop update is the Improved Python & R Script Editor. The script editor now contains the same features as the DAX editor, including IntelliSense for code completion. After spending some time with the new editor though, I made a discovery about the code completion.
Look closely at the following screenshot. Any guesses as to how IntelliSense currently works?
If I type the letter a into the R Script editor, my code completion options are acts, always, and, and as. Power BI’s editor is not offering any IntelliSense options from a Python or R dictionary. Instead, it’s pulling from the text already in the editor. Note the comment in Line 1 and the inclusion of words beginning with the letter a — always, and, acts, as.
By comparison, the DAX editor contains a detailed function list and helpful annotations for code completion. Can we get something similar for R and Python? Not exactly… Let’s hope that Microsoft continues to improve the IntelliSense capabilities in the editor. But there’s a workaround for now that I’m almost embarrassed to suggest. If you are a user who codes directly into the script editor, the following hack could be helpful. If you use the option to Edit script in External IDE, keep doing that and ignore the following guidance.
Add a Temporary Custom Dictionary
There’s no way that I know of to easily add to the script editor’s IntelliSense dictionary, but we can fake it by adding temporary comments. This will allow us to use code completion, but it’s not a robust IntelliSense that offers function definitions on par with the DAX editor (“true” IntelliSense).
The trick is to build your own custom dictionary of your most used Python or R functions from various packages and paste text as comments into your R Editor or Python Editor temporarily. You wouldn’t want all this text in your final script, so I want to stress that it’s only a temporary dictionary. For example, I added all the text from the R “stats” package to my script editor as an insanely lengthy (yet temporary) series of comments below where my real R code resides.
Having the stats functions available in the comments allows me to create some basic, helpful code completion if I want to directly edit code in the R or Python editor.
It takes dedication to use this workaround, so it may be worth using RStudio as Power BI’s external IDE. If you’re determined to edit directly in Power BI, however, you can build your own code completion dictionary. To assemble the dictionary, I went to R’s documentation for the stats package (and you can extend with any other packages you commonly use). I grabbed all the function names, appended the comment character # to the beginning of each, copied, and pasted. It’s just that simple-not-simple.
Remove Dictionary Comments When Done
When you’re done editing your code, simply remove the comments. Don’t store them in the final text of your R or Python script. I repeat–don’t leave this stuff in your final script. If you ever need to edit the R or Python script again, simply paste them back temporarily as needed. While not elegant, this solution could work in a pinch. Happy coding.