Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Manipulate Package
The manipulate
from RStudio allows you to create simple Tcl/Tk operators for interactive visualization. I will use it for a simple slider to view different slices of an image.
library(manipulate)
fslr package
I'm calling the fslr
package because I know that if you have it installed, you will likely have FSL and have a 1mm T1 template from MNI in a specific location. fslr
also loads the oro.nifti
package so that readNIfTI
is accessible after loading fslr
. You can download a test NIfTI image here if you don't have access to any and don't have FSL downlaoded.
Here I will read in the template image:
library(fslr) options(fsl.path='/usr/local/fsl') template = file.path(fsldir(), "data/standard", "MNI152_T1_1mm_brain.nii.gz") img = readNIfTI(template)
The iplot function
The iplot
function defined below takes in a nifti
object, the specific plane to be plotted and additional options to be passed to oro.nifti::image
. The function is located on my GitHub here.
iplot = function(img, plane = c("axial", "coronal", "sagittal"), ...){ ## pick the plane plane = match.arg(plane, c("axial", "coronal", "sagittal")) # Get the max number of slices in that plane for the slider ns= switch(plane, "axial"=dim(img)[3], "coronal"=dim(img)[2], "sagittal"=dim(img)[1]) ## run the manipulate command manipulate({ image(img, z = z, plot.type= "single", plane = plane, ...) # this will return mouse clicks (future experimental work) pos <- manipulatorMouseClick() if (!is.null(pos)) { print(pos) } }, ## make the slider z = slider(1, ns, step=1, initial = ceiling(ns/2)) ) }
Example plots
Here are some examples of how this iplot
function would be used:
iplot(img) iplot(img, plane = "coronal") iplot(img, plane = "sagittal")
The result will be a plotted image of the slice with a slider. This is most useful if you run it within RStudio.
Below are 2 example outputs of what you see in RStudio:
Slice 91:
Slice 145:
Conclusions
The iplot
function allows users to interactively explore neuroimages. The plotting is not as fast as I'd like, I may try to speed up the oro.nifti::image
command or implement some subsampling. It does however show a proof of concept how interactive neuroimaging visualization can be done in R
.
Note
manipulate
must be run in RStudio for manipulation. The fslr
function fslview
will call FSLView from FSL for interactive visualization. This is an option of interactive neuroimaging “in R
”, but not a real or satisfactory implementation for me (even though I use it frequently). If anyone has implemented such a solution in R
, I'd love to hear about it.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.