Skip to content

Commit

Permalink
uncode/code: use type.convert() not as.character()for converting …
Browse files Browse the repository at this point in the history
…from factor

 - uncode: Use `NASISDomainsAsFactor()` to check package option
  • Loading branch information
brownag committed Jun 29, 2022
1 parent ca38f37 commit f28f379
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
13 changes: 7 additions & 6 deletions R/uncode.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ uncode <- function(df,
df <- droplevels(df, except = idx)
}

# convert factors to strings, check soilDB option first
if ((length(stringsAsFactors) > 0 && !stringsAsFactors) ||
!getOption("soilDB.NASIS.DomainsAsFactor", default = FALSE)) {
# convert factors to strings/numeric, check soilDB option first
if (invert || (length(stringsAsFactors) > 0 && !stringsAsFactors) || !NASISDomainsAsFactor()) {
idx <- unlist(lapply(df, is.factor))
df[idx] <- lapply(df[idx], as.character)
df[idx] <- lapply(df[idx], function(x) {
type.convert(x, as.is = TRUE)
})
}

return(df)
Expand All @@ -152,11 +153,11 @@ uncode <- function(df,
#' @export
#' @rdname uncode
code <- function(df,
db = "NASIS",
db = NULL,
droplevels = FALSE,
stringsAsFactors = NULL,
dsn = NULL) {
res <- uncode(df, invert = TRUE, db = db, droplevels = droplevels, stringsAsFactors = stringsAsFactors, dsn = dsn)
res <- uncode(df, invert = TRUE, droplevels = droplevels, stringsAsFactors = stringsAsFactors, dsn = dsn)
return(res)
}

Expand Down
2 changes: 1 addition & 1 deletion man/get_NASIS_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/uncode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/testthat/test-uncode.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test_that("uncode() works", {
x <- data.frame(texcl = 1:10)
expect_equal(uncode(x)$texcl, c("cos", "s", "fs", "vfs", "lcos", "ls", "lfs", "lvfs", "cosl", "sl"))
})

test_that("uncode() works w/ NASISDomainsAsFactor(TRUE)", {
NASISDomainsAsFactor(TRUE)
x <- data.frame(texcl = 1:10)
expect_equal(uncode(x)$texcl, structure(1:10, .Label = c("cos", "s", "fs", "vfs", "lcos", "ls",
"lfs", "lvfs", "cosl", "sl", "fsl", "vfsl", "l", "sil", "si",
"scl", "cl", "sicl", "sc", "sic", "c"), class = "factor"))
NASISDomainsAsFactor(FALSE)
})

test_that("code() works", {
x <- data.frame(texcl = c("cos", "s", "fs", "vfs", "lcos", "ls", "lfs", "lvfs", "cosl", "sl"))
expect_equal(code(x)$texcl, 1:10)
})

test_that("code() works w/ NASISDomainsAsFactor(TRUE)", {
NASISDomainsAsFactor(TRUE)
x <- data.frame(texcl = c("cos", "s", "fs", "vfs", "lcos", "ls", "lfs", "lvfs", "cosl", "sl"))
expect_equal(code(x)$texcl, 1:10)
NASISDomainsAsFactor(FALSE)
})

0 comments on commit f28f379

Please sign in to comment.