Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
FlyKick
Helper II
Helper II

How To Return Data From R Function As List

Hi Guru's,
 
I have dirty data that contains mac addresses in Power BI and have learnt R today so that I can use a regular expression to extract them. I can get everything working but it only returns the first mac address it finds. If the data contains multiple (which they all do) they don't get returned.

 

I would like to return all the mac addresses into a new column as a list so that within Power query I can extract them to new lines.

Here is my Power Query code form within Power Query Editor.

 

 

# 'dataset' holds the input data for this script

# Variables
pattern <- "([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})"

# Functions
getMacs <- function(x) {paste(unlist(regmatches(x, gregexpr(pattern, x))))}

# Return
output <- within(dataset,{MACs1=getMacs(dataset$NICs)})

 

 

 

What I get returned into my new Macs1 column is the very first mac address from the NICs column of each row. I can't for the life of me figure out how to return all of the mac addresses as a list.

 

I have verified using Visual Studio Code that the regular expression works and that the R code does return all mac addresses as a character vector.

L6YxC.png

 

Doing some research I believe I need to convert the character vector to a data.frame before returning it to Power BI. I tried amending the following lines without success.

 

 

 

output <- within(dataset,{MACs1=as.list(getMacs(dataset$NICs))})
output <- within(dataset,{MACs1=as.data.frame(getMacs(dataset$NICs))})

 

 

 

I know it's going to be some #newbieFail but I just can't seem to figure it out. Appreciate any pointers or advice.

 

Thanks in advance. Cheers

 

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@FlyKick  You can run regex directly in Power BI now, without needing native R/stringR

 

check out these two links

https://community.powerbi.com/t5/Community-Blog/How-to-use-JavaScript-inside-power-query-for-data-ex...

https://community.powerbi.com/t5/Community-Blog/Using-JavaScript-in-power-query-for-regex-Part2/ba-p...

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

3 REPLIES 3
smpa01
Super User
Super User

@FlyKick  You can run regex directly in Power BI now, without needing native R/stringR

 

check out these two links

https://community.powerbi.com/t5/Community-Blog/How-to-use-JavaScript-inside-power-query-for-data-ex...

https://community.powerbi.com/t5/Community-Blog/Using-JavaScript-in-power-query-for-regex-Part2/ba-p...

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

This is awesome! Completely bypasses the need to use R. Thank you 

lbendlin
Super User
Super User

Where does Power BI get the MAC addresses from in the first place?  Are you running your R script in Power Query or in a visual?

 

Please provide sanitized sample data that fully covers your issue. Paste the data into a table in your post or use one of the file services. Please show the expected outcome.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.