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 😉
Pingback: Power BI Error with R Packages – Curated SQL
Just to be sure, reinstalling the R packages did not help?
I only have one libPath. Also in my case I don’t get an error at all. A feature related to “ggpubr” is not showing up when I publish the visualization.
“Publish the visualization “meaning you are getting an error in PowerBI service ?
I have the same issue but unable to solve it even after following your steps. Some extra information – the 1st path in your step-1 is a default one created by Windows when Rstudio/Rclient runs as non-administrator. I deleted that 1st path from my laptop and now I always execute Rstudio/Rclient as administrator. Currently I have only the 2nd path (“Microsoft open R”) but still the problem exist 🙁
I noticed that PowerBI tries to load packages from a different library path (“C:/User/User_name/Library” – in my case) when using the R visuals and I printed the list of libraries inside the Rvisuals using .libPaths() command. So after installing the packages into this new location, I am able to run my R scripts in Rvisuals without any issues. No more namespace conflicts!
Thanks for this additional insight.