Skip to content

Commit

Permalink
Merge pull request #208 from iNZightVIT/hotfix/1.13.3
Browse files Browse the repository at this point in the history
Release 1.13.3
  • Loading branch information
tmelliott authored Jun 15, 2023
2 parents ed93145 + f630307 commit 1d5b2f3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: iNZightTools
Type: Package
Title: Tools for 'iNZight'
Version: 1.13.1
Version: 1.13.3
Depends: R (>= 4.0)
Imports:
dplyr,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# iNZightTools 1.13.3

- `read_text()` function replaces spaces with underscores in column names

# iNZightTools 1.13.2

- add global options to set/override default comment character (this will allow Lite to change the default without changing the package's default behaviour; default set as # at load time)
Expand Down
12 changes: 9 additions & 3 deletions R/read_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
#' @author Tom Elliott
#' @export
read_text <- function(txt, delim = "\t", ...) {
if (txt == "clipboard")
if (txt == "clipboard") {
txt <- readr::clipboard()
}

if (utils::packageVersion('readr') < numeric_version('2.0.0')) {
if (utils::packageVersion("readr") < numeric_version("2.0.0")) {
d <- readr::read_delim(I(txt), delim = delim)
} else {
d <- readr::read_delim(I(txt), delim = delim, show_col_types = FALSE)
}

d %>% dplyr::mutate_if(is.character, as.factor)
d <- d |> dplyr::mutate_if(is.character, as.factor)

if (any(grepl(" ", colnames(d)))) {
colnames(d) <- gsub(" ", "_", colnames(d))
}
d
}
10 changes: 10 additions & 0 deletions tests/testthat/test_read_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ test_that("Tab-delim text reads", {
readr::read_tsv(I(text), show_col_types = FALSE) %>% dplyr::mutate(y = as.factor(y))
)
})

text <- "var 1\tvar 2
1\t2
3\t4
5\t6
"
test_that("Spaces in names converted to underscores", {
d <- read_text(text)
expect_equal(colnames(d), c("var_1", "var_2"))
})

0 comments on commit 1d5b2f3

Please sign in to comment.