6

I'm using an R script within Power Query to do some data transformations and return a scaled table. My R code is like this:

# 'dataset' 

1 Answers1

1

It does seem like odd that this fails to return. A quick glance online gave this 3 minute youtube video, which uses the same method, which you are using. Further searching down a source, one may come across the Microsoft Documentation, which gives a possible reason for why there might be an issue.

When preparing and running an R script in Power BI Desktop, there are a few limitations:

  • Only data frames are imported, so make sure the data you want to import to Power BI is represented in a data frame

  • Columns that are typed as Complex and Vector are not imported, and are replaced with error values in the created table

These seem like the most obvious reasons. Betting that there is no complex columns in your dataset, I'd believe the prior is likely the reason. A quick recreation of your dataset shows that the scale functions changes your dataset into a matrix class object. This is kept by cbind, and as such output is of class matrix and not data.frame.

>dataset <- as.data.frame(abs(matrix(rnorm(1000),ncol=4)))
>class(dataset)
[1]"data.frame"
>library(dplyr)
>df_normal <- log(dataset + 1) %>%
>    select(c(2:4)) %>%
>    scale 
>class(df_normal)
[1] "matrix"
>df_normal <- cbind(dataset[,1], df_normal)
>output <- df_normal
>class(output)
[1] "matrix"

A simple fix would then seem to be adding output <- as.data.frame(output), as this is in line with the documentation of powerBI. Maybe it would need a return like statement at the end. Adding a line at the end of the script simply stating output should fix this.

Edit

For clarification, I believe the following edited script (of yours) should return the data expected

# 'dataset' contém os dados de entrada neste script

library(dplyr)

df_normal <- log(dataset+1) %>%
select(c(2:4)) %>%
scale
df_normal <-cbind(dataset[,c(1)], df_normal)
output <- as.data.frame(df_normal)
#output  ##This line might be needed without the first comment 
Community
  • 1
  • 1
Oliver
  • 8,169
  • 3
  • 15
  • 37
  • thanks for your help! I did search the documentation, but couldn't find where "dataset" and "output" are defined.Anyway, it seems obvious now that they mean the table we just imported and the table to we want to export to Power Query. Are you aware of where I can find other relevant syntax for integrating R scripts with Power Query, by the way? Now, to answer your comments above, you're absolutely right! I didn't realyze the normalization would change the df to a matrix (nor why). But converting it back to a df, made it, thanks! –  May 11 '19 at 19:31
  • 1
    I am glad i could help. The documentation seems very sparse. I'd suggest looking at other's examples, some which are availible [here](https://community.powerbi.com/t5/R-Script-Showcase/bd-p/RVisuals). I sadly do not have enough experience with powerBI to give better pointers. That said, from the two points above, you could do any kind of calculations, and then transform the data into a `data.frame`. If one used a linear model and wanted the coefficients, you could then have a column that specifies the coefficient, such that it afterwards could be identified in powerBI directly. – Oliver May 11 '19 at 19:44
  • 1
    Power BI is a great tool for BI (as it is supposed to be). Combining its capabilities in terms of data cleaning and presentation with R algorithms, it can be incredibly powerful, I believe (since R's interface is for the data scientist). How to get data out of R into Power BI is something I'm exploring just now, so thanks a lot again for your help! –  May 11 '19 at 20:43