From 4bc50de1a7cd247f149931b0702330f59c702c50 Mon Sep 17 00:00:00 2001 From: dipterix Date: Tue, 18 Jul 2023 11:12:11 -0400 Subject: [PATCH 1/2] moved rmarkdown to Suggests and installs it interactively when missing --- DESCRIPTION | 6 +++--- R/helpers.R | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 00e1244..9b957f1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: freesurferformats Type: Package Title: Read and Write 'FreeSurfer' Neuroimaging File Formats -Version: 0.1.17 +Version: 0.1.17.9000 Authors@R: person("Tim", "Schäfer", role = c("aut", "cre"), email = "ts+code@rcmd.org", comment = c(ORCID = "0000-0002-3683-8070")) Maintainer: Tim Schäfer Description: Provides functions to read and write neuroimaging data in various file formats, with a focus on 'FreeSurfer' formats. This includes, but is not limited to, the following file formats: 1) MGH/MGZ format files, which can contain multi-dimensional images or other data. Typically they contain time-series of three-dimensional brain scans acquired by magnetic resonance imaging (MRI). They can also contain vertex-wise measures of surface morphometry data. The MGH format is named after the Massachusetts General Hospital, and the MGZ format is a compressed version of the same format. 2) 'FreeSurfer' morphometry data files in binary 'curv' format. These contain vertex-wise surface measures, i.e., one scalar value for each vertex of a brain surface mesh. These are typically values like the cortical thickness or brain surface area at each vertex. 3) Annotation file format. This contains a brain surface parcellation derived from a cortical atlas. 4) Surface file format. Contains a brain surface mesh, given by a list of vertices and a list of faces. @@ -11,10 +11,10 @@ URL: https://github.com/dfsp-spirit/freesurferformats BugReports: https://github.com/dfsp-spirit/freesurferformats/issues Imports: pkgfilecache (>= 0.1.1), - xml2, - rmarkdown + xml2 Suggests: knitr, + rmarkdown, testthat (>= 2.1.0), oro.nifti (>= 0.9), gifti (>= 0.7.5), diff --git a/R/helpers.R b/R/helpers.R index 13ba923..09d9d35 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -83,11 +83,49 @@ fs.surface.to.tmesh3d <- function(surface) { #' #' @return logical, whether Pandoc is available. #' -#' @importFrom rmarkdown pandoc_available -#' #' @keywords internal has_pandoc <- function() { + assert_package("rmarkdown") return(rmarkdown::pandoc_available()); } +assert_package <- function(pkg) { + + package_installed <- vapply(pkg, function(p) { + return( system.file(package = p) != "" ) + }, FALSE) + if( all(package_installed) ) { return(invisible()) } + pkg <- pkg[!package_installed] + + # the package is missing + cnd <- structure(list(message = sprintf("Package(s) %s missing. Please install first.", paste(sQuote(pkg), collapse = ", ")), call = NULL), + class = c("package_not_found_error", "simpleError", "error", "condition")) + + # check if rlang has been installed, usually yes if people installs any rlib/posit packages + rlang_is_missing <- system.file(package = "rlang") == "" + pak_is_missing <- system.file(package = "pak") == "" + + # if not interactive or rlang&pak are missing, per CRAN policy, stop + if(!interactive() || (rlang_is_missing && pak_is_missing)) { + stop(cnd) + } + + if( !rlang_is_missing ) { + rlang <- asNamespace("rlang") + rlang$check_installed(pkg) + return(invisible()) + } + + # using pak + message(sprintf("Package(s) %s missing, install?", paste(sQuote(pkg), collapse = ", "))) + if (utils::menu(c("Yes", "No")) != 1) { + invokeRestart("abort", cnd) + } + pak <- asNamespace("pak") + pak$pkg_install(pkg, ask = FALSE) + + return(invisible()) + +} + From 56c3abd9a1ba67379af1abab26a8e0daa87ba573 Mon Sep 17 00:00:00 2001 From: dipterix Date: Tue, 18 Jul 2023 11:21:32 -0400 Subject: [PATCH 2/2] Updated NAMESPACE and added utils to Imports --- DESCRIPTION | 3 ++- NAMESPACE | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9b957f1..4b0474f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,6 +10,7 @@ Encoding: UTF-8 URL: https://github.com/dfsp-spirit/freesurferformats BugReports: https://github.com/dfsp-spirit/freesurferformats/issues Imports: + utils, pkgfilecache (>= 0.1.1), xml2 Suggests: @@ -20,4 +21,4 @@ Suggests: gifti (>= 0.7.5), cifti (>= 0.4.5) VignetteBuilder: knitr -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.3 diff --git a/NAMESPACE b/NAMESPACE index 498aada..d402e0b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -162,7 +162,6 @@ importFrom(pkgfilecache,erase_file_cache) importFrom(pkgfilecache,get_filepath) importFrom(pkgfilecache,get_pkg_info) importFrom(pkgfilecache,list_available) -importFrom(rmarkdown,pandoc_available) importFrom(stats,dist) importFrom(stats,na.omit) importFrom(utils,read.table)