# NOT RUN {
# get filename for example NII file included in nifti.io package
ref.nii <- system.file("extdata", "egBrain.nii", package="nifti.io")
# create a temporary file to write into
tdir <- tempdir()
new.nii <- paste0(tdir, "/new.nii")
init.nii(new.nii = new.nii, ref.nii = ref.nii)
# generate an array of random values the same size as the image volume
xyz.dims <- info.nii(ref.nii, "xyz")
new.values <- array(rnorm(prod(xyz.dims)), dim=xyz.dims)
# write out volume all at once
write.nii.volume(nii.file = new.nii, vol.num = 1, values = new.values)
# }
Run the code above in your browser using DataLab