Skip to content

Commit

Permalink
Merge pull request #109 from sharlagelfand/master
Browse files Browse the repository at this point in the history
Handle invalid names when source is a tibble
  • Loading branch information
MilesMcBain committed Nov 9, 2020
2 parents e569c3b + 6f64392 commit 6558034
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/tribble_paste.R
Expand Up @@ -63,7 +63,7 @@ tribble_construct <- function(input_table, oc = console_context()){
}
input_table_types <- lapply(input_table, class)
#Store types as characters so the char lengths can be computed
input_table <- as.data.frame(lapply(input_table, as.character), stringsAsFactors = FALSE)
input_table <- as.data.frame(lapply(input_table, as.character), stringsAsFactors = FALSE, check.names = FALSE)
}
# Warn if there is any factors, they will be converted to strings.
factor_cols <- which(input_table_types == "factor")
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test_dfpaste.R
Expand Up @@ -122,6 +122,24 @@ test_that("Columns with non-valid names can be parsed as a data.frame, with name
)
})

test_that("Input tibbles with columns with non-valid names can be parsed as a data.frame, with names surrounded by backticks and check.names FALSE", {
expect_equal(
{eval( parse(text = dfdt_construct(tibble::tribble(
~`!!`, ~`2015`, ~`%`, ~`TRUE`,
1L, "b", 3L, "D"
), class = "data.frame")) )},
{
data.frame(stringsAsFactors=FALSE,
check.names=FALSE,
`!!` = c(1L),
`2015` = c("b"),
`%` = c(3L),
`TRUE` = c("D")
)
}
)
})

test_that("meaningful rownames are included when input_table is a data.frame", {
expect_equal(
eval(parse(text = dfdt_construct(mtcars[1:3, 1:3], class = "data.frame"))),
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test_dtpaste.R
Expand Up @@ -102,4 +102,39 @@ test_that("data.table contruct recognises raw data with no column headings and a
)
})

test_that("Columns with non-valid names can be parsed as a table, with names surrounded by backticks and check.names FALSE", {
skip_if_not(is_clipr_available, skip_msg)
skip_on_cran()
skip_if_not(is_RStudio_session)
expect_equal(
{clipr::write_clip(readr::read_lines(file = "./non_valid_colnames.txt"))
eval(parse(text = dfdt_construct(class = "data.table")))},
{
data.frame(stringsAsFactors=FALSE,
check.names=FALSE,
`!!` = c(1L),
`2015` = c("b"),
`%` = c(3L),
`TRUE` = c("D")
)
}
)
})

test_that("Input tibbles with columns with non-valid names can be parsed as a data.frame, with names surrounded by backticks and check.names FALSE", {
expect_equal(
{eval( parse(text = dfdt_construct(tibble::tribble(
~`!!`, ~`2015`, ~`%`, ~`TRUE`,
1L, "b", 3L, "D"
), class = "data.table")) )},
{
data.table::data.table(
check.names=FALSE,
`!!` = c(1L),
`2015` = c("b"),
`%` = c(3L),
`TRUE` = c("D")
)
}
)
})
17 changes: 16 additions & 1 deletion tests/testthat/test_tribble.R
Expand Up @@ -379,6 +379,21 @@ test_that("Columns with non-valid names can be parsed, with names surrounded by
)
})

test_that("Input tibbles with columns with non-valid names can be parsed, with names surrounded by backticks", {
expect_equal(
{eval( parse(text = tribble_construct(tibble::tribble(
~`!!`, ~`2015`, ~`%`, ~`TRUE`,
1L, "b", 3L, "D"
))) )},
{
tibble::tribble(
~`!!`, ~`2015`, ~`%`, ~`TRUE`,
1L, "b", 3L, "D"
)
}
)
})

test_that("Columns that are completely blank (NA) are dropped", {
skip_if_not(is_clipr_available, skip_msg)
skip_on_cran()
Expand Down Expand Up @@ -406,7 +421,7 @@ test_that("blank final row has separator guessed as tab", {
test_that("zero length tibbles work", {

expect_equal(
eval(parse(text =
eval(parse(text =
datapasta::tribble_construct(
tibble::tibble(a = character(), b = integer()))
)
Expand Down

0 comments on commit 6558034

Please sign in to comment.