Reading_Data_R
How to read/write in Neuroimaging Data in R
There are many packages in R that allow you to read in neuroimaging data. Primarily, the format we will be using will be NIfTI. The documentation for R has a lot information here NIfTI files contain all header information, such as image dimensions, pixel resolution, intensity values, scaling factors, etc, is contained in one file. ANALYZE files are similar, which have a .img and .hdr files that contain the image (3D array) and header information, respectively.
Reading in NIfTI
library(oro.nifti)
## Loading required package: bitops
## oro.nifti: Rigorous - NIfTI+ANALYZE+AFNI Input / Output (version = 0.3.9)
img <- readNIfTI("MNI152_T1_2mm_brain.nii.gz")
## Warning: scl_slope = 1 and data must be rescaled.
image(img, z = 40, plot.type = "single")
arr <- img@.Data
# writeNIfTI(nifti-object, filename='fileWithoutNii')
The package oro.nifti is (in my opinion) the best reader of NIfTI files because you can store data gzipped as .gz without having to do any work around using gunzip from the R.utils package. The data is stored in slots which can be called using the \@ symbol. Methods should still work by
In order to write a nifti file, you need a nifti object, which you create by using, (guess which function), nifti(array), where array is a 3D array.
library(fmri)
read.NIFTI("file.nii")
write.NIFTI(3-or-4D-array, filename)
library(AnalyzeFMRI)
f.read.nifti.volume("file.nii")
f.write.nifti(3D-array, filename="file.nii")
Reading in DICOM
Also, one format to read in is DICOM, which is a format where each slice is contained within one file. oro.dicom is similar to oro.nifti and has the functions readDICOM for reading an entire DICOM directory and readDICOMFile to read individual DICOM files. One very useful flag: is pixelData=FALSE which will read the header information from the DICOM.
