knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/" )
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.
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__`)
reticulate
which Python interpreter to use. To use the recommended Python 3 from Aanaconda, assuming it is installed, you can include this line in your .bashrc
(Linux) or .bash_profile
(Mac):export RETICULATE_PYTHON=$HOME/anaconda3/bin/python
mne
works.import mne
This should not give you any error message.
For optimal Python and reticulate
support in Rstudio, make sure you have the latest version of Rstudio.
Currently, when making matplotlib figures from within R, the resulting image will not be rendered inside the Rstudio Rmarkdown chunk. You will need to save, load and display the figure or explicitly, write Python code in a Python chunk, or explicitly make maptlotlib open a window:
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.
Some of the examples may depend on the latest MNE-Python code. We will try to provide patches little by little in MNE-Python or here.
Currently, if 32-bit and 64-bit versions of R are installed, it seems devtools tries to build for both of them. This yields a loading failed for 'i386'
error. To force building the package for your currently running R version use the INSTALL_opts
argument of install_github
:
devtools::install_github("mne-tools/mne-r", INSTALL_opts=c("--no-multiarch"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.