diff --git a/.Rbuildignore b/.Rbuildignore index 4f5040ab525fe6a3db1796a52fb7d4c3f515016c..1dfae08f88323f6f67911f98f41124ff7caab914 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,5 +7,5 @@ .*^(?!inst)\.nc$ .gitlab-ci.yml # unit tests should be ignored when building the package for CRAN -^tests$ +# ^tests$ ^CONTRIBUTING\.md$ diff --git a/R/ArrayToNc.R b/R/ArrayToNc.R index 6952edc978799596e26c6cf802d353a9ff176963..55d7057ba20fe12776106bf1f0410166b754d8d3 100644 --- a/R/ArrayToNc.R +++ b/R/ArrayToNc.R @@ -231,7 +231,8 @@ ArrayToNc <- function(arrays, file_path) { if (is.array(arrays)) { arrays <- list(arrays) } - if (any(!sapply(arrays, function(x) is.array(x) && (is.numeric(x) || is.logical(x))))) { + if (any(!sapply(arrays, function(x) is.array(x) && + (is.numeric(x) || is.logical(x) || is.character(x))))) { stop("The parameter 'arrays' must be one or a list of numeric or logical arrays.") } # Check parameter file_path. @@ -444,6 +445,7 @@ ArrayToNc <- function(arrays, file_path) { var_info[['prec']] <- 'short' } else if (typeof(arrays[[i]]) == 'character') { var_info[['prec']] <- 'char' + attr(arrays[[i]], "variables")[[var_info[['name']]]] <- list(prec = 'char') } else if (typeof(arrays[[i]]) == 'integer') { var_info[['prec']] <- 'integer' } else { @@ -455,6 +457,14 @@ ArrayToNc <- function(arrays, file_path) { } var_info[['prec']] <- var_info[['prec']][1] } + + if (var_info[['prec']] == 'char') { + dimnchar_name <- paste0(var_info[['name']], "_length") + dimnchar <- ncdim_def(dimnchar_name, "", 1:max(sapply(arrays[[i]], nchar)), + create_dimvar = FALSE) + defined_dims[[dimnchar_name]] <- dimnchar + var_built_dims <- c(dimnchar_name, var_built_dims) + } new_var <- list(ncvar_def(var_info[['name']], var_info[['units']], defined_dims[var_built_dims], var_info[['missval']], @@ -502,14 +512,17 @@ ArrayToNc <- function(arrays, file_path) { } add_offset <- var_info[['addOffset']][1] } - if (is.null(var_dim)) { + if (!is.null(var_info[['prec']]) && var_info[['prec']] == 'char') { + ncvar_put(ncdf_object, defined_vars[[var_counter]]$name, + arrays[[i]]) + } else if (is.null(var_dim)) { if (scale_factor != 1 || add_offset != 0) { ncvar_put(ncdf_object, defined_vars[[var_counter]]$name, (arrays[[i]] - add_offset) / scale_factor, - count = dim(arrays[[i]])) + count = dim(arrays[[i]])) } else { ncvar_put(ncdf_object, defined_vars[[var_counter]]$name, - arrays[[i]], + arrays[[i]], count = dim(arrays[[i]])) } } else { diff --git a/tests/testthat/test-ArrayToNc.R b/tests/testthat/test-ArrayToNc.R index 4a660d0234d2bc7b644e6aceb2d019b94720cdbc..c5fdd0c1d152ddc94506d50c699c331a34e4c2f6 100644 --- a/tests/testthat/test-ArrayToNc.R +++ b/tests/testthat/test-ArrayToNc.R @@ -1,5 +1,6 @@ -out_dir <- "./tests/" +out_dir <- "./tests-output/" +dir.create(out_dir, showWarnings = FALSE) ################################### test_that("1. dat1", {