From cb39157aedfeea800d3b1142715cc056fc12c058 Mon Sep 17 00:00:00 2001 From: Jon Clayden Date: Fri, 25 Oct 2019 16:57:13 +0100 Subject: [PATCH] niftiHeader() will now additionally retrieve string versions of image codes, and the print method will show them --- R/nifti.R | 9 ++++++++- inst/include/lib/NiftiImage_impl.h | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/R/nifti.R b/R/nifti.R index 871c9ed..08e2e4e 100644 --- a/R/nifti.R +++ b/R/nifti.R @@ -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 diff --git a/inst/include/lib/NiftiImage_impl.h b/inst/include/lib/NiftiImage_impl.h index 34ebbe4..e1d0048 100644 --- a/inst/include/lib/NiftiImage_impl.h +++ b/inst/include/lib/NiftiImage_impl.h @@ -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; }