Today I spent many hours hunting an R-script error in Power BI and before Steph Locke came up with a solution for this, I came across a couple of posts and heard of other people, having the same problem. This blogpost is to make distribution of the solution a bit easier and to hopefully to help other folks with the same problem in the future.
When running R-scripts in Power BI, I got all sorts of error-messages who all had one thing in common: They were complaining about one or more packages being installed by an R version with different internals.
They ran without any problem in RStudio or on other machines, just not on my own specific laptop.
I have no idea what causes the problem, but Steph Locke showed me how she solved it before. She installed the problematic packages into R’s program-folder and pointed to this folder in a parameter, when using the function.
1) Find your paths
Display your R-paths by using this function: .libPath()
The first path is the one which Power BI most likely will reference by default for the package information and the one which RStudio uses to install the packages to. The second path in the program folder belongs to a folder that also contains the R-program itself.
Now to solve the problem you have to install the packages that turn up in the error-messages into this second library folder.
2) Install package into the program folder
Therefore you have to open the RGui with admin-rights. If you don’t have a symbol for it on your desktop, you’ll find the file in the bin-folder:
install.packages("scales",lib="C:\\Program Files\\Microsoft\\R Open\\R-3.5.2\\library")
The fist function-parameter takes the name of the problematic package and in the second parameter you have to pass in the path to the library folder within R’s program folder. That’s the 2nd folder from the step above. Make sure to turn the slashes. The double-slashes might not be necessary for everyone, but for me it wouldn’t work otherwise.
3) Adjust R-script
In this last step, you have to add one or more lines of code on top of your existing code:
library("YourPackageName", lib = .libPaths()[-1] )
This formula will load the package, and the 2nd parameter will determine the path from which the file will be taken. Here, the first item from the paths from step 1 will be skipped, so the library in the program folder will be chosen instead.
Just install one package, run the script again, see if another package pops up – rinse – repeat – until you’re done 😉
Please vote for the bugfix here: https://community.powerbi.com/t5/Issues/R-visual-error-quot-package-was-installed-by-an-R-version-with/idi-p/512759
& stay queryious 😉