Skip to content

Commit

Permalink
niftiHeader() will now additionally retrieve string versions of image…
Browse files Browse the repository at this point in the history
… codes, and the print method will show them
  • Loading branch information
jonclayden committed Oct 25, 2019
1 parent 3efb459 commit cb39157
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion R/nifti.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,16 @@ print.niftiHeader <- function (x, ...)
cat("NIfTI-1 header\n")
widths <- nchar(names(x), "width")
maxWidth <- max(widths)
names <- names(x)
strings <- attr(x, "strings")

for (i in seq_along(widths))
cat(paste0(paste(rep(" ",maxWidth-widths[i]),collapse=""), names(x)[i], ": ", paste(format(x[[i]],trim=TRUE),collapse=" "), "\n"))
{
if (!is.null(strings[[names[i]]]))
cat(paste0(paste(rep(" ",maxWidth-widths[i]),collapse=""), names[i], ": ", paste(format(x[[i]],trim=TRUE),collapse=" "), " (", strings[[names[i]]], ")\n"))
else
cat(paste0(paste(rep(" ",maxWidth-widths[i]),collapse=""), names[i], ": ", paste(format(x[[i]],trim=TRUE),collapse=" "), "\n"))
}
}

#' @rdname niftiHeader
Expand Down
8 changes: 8 additions & 0 deletions inst/include/lib/NiftiImage_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1597,8 +1597,16 @@ inline Rcpp::RObject NiftiImage::headerToList () const
result["intent_name"] = std::string(header.intent_name, 16);
result["magic"] = std::string(header.magic, 4);

Rcpp::List strings;
strings["datatype"] = nifti_datatype_string(header.datatype);
strings["intent_code"] = nifti_intent_string(header.intent_code);
strings["qform_code"] = nifti_xform_string(header.qform_code);
strings["sform_code"] = nifti_xform_string(header.sform_code);
strings["slice_code"] = nifti_slice_string(header.slice_code);

internal::addAttributes(result, *this, false, false);
result.attr("class") = Rcpp::CharacterVector::create("niftiHeader");
result.attr("strings") = strings;

return result;
}
Expand Down

0 comments on commit cb39157

Please sign in to comment.