knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/"
)

MNE-R: fast acccess to MNE-Python from within R

The MNE-Python project provides a full tool stack for processing and visualizing electrophysiology data. That is, electroencephalography (EEG), magnetoencephalography but also intracranial EEG. MNE-R facilitates integrating this mature and extensive functionality into R-based data processing, visualization and statisticasl modeling. This is made possible through the reticulate package, which enables seamless integration of Python into R.

Currently, MNE-R is focussing on documenting how to use MNE-Python through R, based on familiar MNE-examples while also showcasing what R can add to the game in terms of statistics and visualization functionality.

In the future, more R-functions may be added that facilitate the interaction with MNE-Python or implement complementary functionality.

The project is open to contributions.

Getting Started

In order to use MNE-R, MNE-Python has to be installed with all its dependencies. Some configuration may be needed to make sure reticulate knows which Python installation to use. Please consider the reticulate and the MNE documentation. We generally recommend using the Anaconda Python distribution and Python 3 instead of Python 2.

For seamlessly combining R and Python code in one Rmarkdown script, the latest Rstudio 1.2 is needed.

Currently, MNE-R can be installed from github.

library(devtools)
devtools::install_github("mne-tools/mne-r")

To get started, simply load the mne library

library(mne)  # load mne and get the mne object

# the mne object wraps the loaded mne module inside Python
print(names(mne)[1:10])

# use dollar signs to access MNE modules, functions and objects
cat(mne$datasets$sample$data_path$`__doc__`)

Troubleshooting and known issues.

export RETICULATE_PYTHON=$HOME/anaconda3/bin/python
import mne

This should not give you any error message.

library(mne)
plt <- import("matplotlib.pyplot")  # get matplotlib handle

data_path <- mne$datasets$sample$data_path()
raw_fname <- paste(data_path,
                   'MEG',
                   'samlpe',
                   'sample_audvis_filt-0-40_raw.fif',
                   sep = '/')
raw <- mne$io$read_raw_fif(raw_fname)
raw$plot()  # plot it!
plt$show()  # show the figure, then search for the window popping up.
devtools::install_github("mne-tools/mne-r", INSTALL_opts=c("--no-multiarch"))

Roadmap

  1. Add many vignettes and examples
  2. Find common inconvenient steps and add convenience R functions
  3. Motivate contributed examples and code from the R-EEG community
  4. Add advanced content that leverages functionality unique to R


mne-tools/mne-r documentation built on Feb. 28, 2020, 9:45 p.m.