-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle data.tables with column names containing spaces #149
Conversation
Thanks. rlang is not really an option here, I want to keep the dependency list short. I am sure there are ways to do this in base. |
I remembered that data.table nowadays supports |
Codecov Report
@@ Coverage Diff @@
## master #149 +/- ##
==========================================
- Coverage 87.27% 87.25% -0.03%
==========================================
Files 43 43
Lines 1635 1632 -3
==========================================
- Hits 1427 1424 -3
Misses 208 208
Continue to review full report at Codecov.
|
Hi, is there anything else you want me to change? Best regards, |
Hi Stefan, sorry for the delay. I am doing similar problematic Will work on an update soon. |
You can just turn a list into a call. But stuff like that sometimes is problematic with x <- 1:13
c("paste",
"letters",
"LETTERS",
"x") %>%
map(as.name) %>%
as.call %>%
eval For the current use case (i.e. calling a command on a list of arguments) I'd use |
Your solution does not work if the column name itself contains backticks. I'd advise against constructing calls as text and then using parse because it's so hard to know if and when it will break (cough - although security is obviously not an issue here). As far as I can see from a quick glance all your adjustments could be done with a
|
I certainly agree that
In other cases, as for the So here is an attempt for a more elegant solution, by directly manipulating the expression> by_expr2 <- function(x) {
as.call(c(quote(list), lapply(x, as.name)))
}
by_expr2(c("hello", "fs%%df"))
#> list(hello, `fs%%df`)
by_expr2(c("hello", "fs%```%df"))
#> list(hello, `fs%\`\`\`%df`)
by_expr2(character(0))
#> list()
library(data.table)
.by <- by_expr2("Species")
as.data.table(iris)[, .SD[1], by = .by]
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1: setosa 5.1 3.5 1.4 0.2
#> 2: versicolor 7.0 3.2 4.7 1.4
#> 3: virginica 6.3 3.3 6.0 2.5 Created on 2019-07-23 by the reprex package (v0.3.0) Handles all the quoting stuff correctly, and does not produce any overhead. Still objections? |
combine_cols_data.table <- function(dt, cols, sep = '_') {
paste_sep <- function(...) paste(..., sep = sep)
qq <- as.call(c(quote(paste_sep), lapply(cols, as.name)))
z <- dt[, id := eval(qq)]
z[, (setdiff(cols, "id")) := NULL] # but this is the right way to do it
setcolorder(z, c("id", setdiff(names(z), "id")))
z[]
} |
Fix is on CRAN now, feel free to re-open if there are still issues |
Hi,
I've made some changes to combine_cols_data.table() so it handles column names with spaces correctly.
It should also work with all other special characters as they're now converted to symbols before being evaluated.
dt[, eval(qq)]
) but made assignments like this was unintended. I don't know if anything in tsbox requires an unmodified inputdt
; then we'd have to make a deep copy of the input.Ciao,
Stefan