The M Code Behind the Power Query M function RData.FromBinary

Understanding RData

Before we dive into the M code behind RData.FromBinary, it’s important to understand what RData is. R is a programming language and software environment for statistical computing and graphics. It is widely used among statisticians and data scientists for data analysis, data visualization, and machine learning.

RData is a file format used by R to store data objects. It is a binary format that is optimized for storage and retrieval of large datasets. RData files can contain a variety of data objects, including data frames, lists, matrices, and vectors.

Using RData.FromBinary

The RData.FromBinary function in Power Query allows users to read RData files into Power Query for further manipulation and analysis. The function takes a single argument, which is the path and filename of the RData file to be read.

Here is an example of how to use RData.FromBinary to read an RData file into Power Query:


Source = RData.FromBinary(“C:Datamydata.RData”)



This code will read the file "mydata.RData" located in the "C:Data" folder into Power Query.

The M Code Behind RData.FromBinary

Behind the scenes, the RData.FromBinary function uses M code to read the binary data from the RData file and convert it into a Power Query table. Here is the M code that is generated when the RData.FromBinary function is used:


Source = Binary.Buffer(File.Contents(“C:Datamydata.RData”)),

BinaryData = Source,

RawData = Binary.Buffer(BinaryData),

RObject = R.Invoke(“load”, RawData),

OutputTable = RObject{[Name=”mydata”]}[Value]



Let's break down this code line by line:

`Source = Binary.Buffer(File.Contents("C:Datamydata.RData"))` - This line reads the binary data from the RData file and stores it in a binary buffer.

`BinaryData = Source` - This line assigns the binary buffer to a new variable called "BinaryData".

`RawData = Binary.Buffer(BinaryData)` - This line converts the binary data into a raw binary buffer.

`RObject = R.Invoke("load", RawData)` - This line invokes the R function "load" on the raw binary buffer, which loads the R data objects contained in the file into memory.

`OutputTable = RObject{[Name="mydata"]}[Value]` - This line extracts the data frame called "mydata" from the R object and converts it into a Power Query table.

Customizing RData.FromBinary

The RData.FromBinary function can be customized to suit your specific needs. For example, you can specify the name of the data frame to be extracted using the "Name" parameter in the last line of the M code:


Source = RData.FromBinary(“C:Datamydata.RData”, [Name=”mydata”])



You can also modify the M code to perform additional data transformations on the table after it has been read into Power Query. For example, you could add a step to filter the data based on a specific condition:


Source = RData.FromBinary(“C:Datamydata.RData”),

FilteredTable = Table.SelectRows(Source, each [Column1] > 0)



This code reads the RData file into Power Query and then filters the table to only include rows where the value in "Column1" is greater than 0.

The RData.FromBinary function in Power Query is a powerful tool for reading and manipulating binary data files in RData format. By understanding the M code behind this function, you can customize it to suit your specific data transformation needs and unlock even more value from your data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

10-12 Jul 23 (London or Online)
25-27 Sep 23 (London or Online)
20-22 Nov 23 (London or Online)

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)