Skip to content
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

Parse backslash #3375

Merged
merged 2 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

3. Default print output (top 5 and bottom 5 rows) when ncol>255 could display the columns in the wrong order, [#3306](https://github.com/Rdatatable/data.table/issues/3306). Thanks to Kun Ren for reporting.

4. Grouping `by = 'string_with_\\'` would fail, [#3319](https://github.com/Rdatatable/data.table/issues/3319). Thanks to @HughParsonage for reporting and @MichaelChirico for the fix.

#### NOTES

1. When upgrading to 1.12.0 some Windows users might have seen `CdllVersion not found` in some circumstances. We found a way to catch that so the [helpful message](https://twitter.com/MattDowle/status/1084528873549705217) now occurs for those upgrading from versions prior to 1.12.0 too, as well as those upgrading from 1.12.0 to a later version. See item 1 in notes section of 1.12.0 below for more background.
Expand Down
10 changes: 6 additions & 4 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -862,12 +862,14 @@ chmatch2 <- function(x, table, nomatch=NA_integer_) {
} else if (is.call(bysub) && bysub[[1L]] == ".") bysub[[1L]] = quote(list)

if (mode(bysub) == "character") {
if (length(grep(",",bysub))) {
if (length(grep(",", bysub, fixed = TRUE))) {
if (length(bysub)>1L) stop("'by' is a character vector length ",length(bysub)," but one or more items include a comma. Either pass a vector of column names (which can contain spaces, but no commas), or pass a vector length 1 containing comma separated column names. See ?data.table for other possibilities.")
bysub = strsplit(bysub,split=",")[[1L]]
}
tt = grep("^[^`]+$",bysub)
if (length(tt)) bysub[tt] = paste0("`",bysub[tt],"`")
backtick_idx = grep("^[^`]+$",bysub)
if (length(backtick_idx)) bysub[backtick_idx] = paste0("`",bysub[backtick_idx],"`")
backslash_idx = grep("\\", bysub, fixed = TRUE)
if (length(backslash_idx)) bysub[backslash_idx] = gsub('\\', '\\\\', bysub[backslash_idx], fixed = TRUE)
bysub = parse(text=paste0("list(",paste(bysub,collapse=","),")"))[[1L]]
bysubl = as.list.default(bysub)
}
Expand Down Expand Up @@ -1842,7 +1844,7 @@ chmatch2 <- function(x, table, nomatch=NA_integer_) {
attrs = attr(x, 'index')
skeys = names(attributes(attrs))
if (!is.null(skeys)) {
hits = unlist(lapply(paste0("__", names(x)[cols]), function(x) grep(x, skeys)))
hits = unlist(lapply(paste0("__", names(x)[cols]), function(x) grep(x, skeys, fixed = TRUE)))
hits = skeys[unique(hits)]
for (i in seq_along(hits)) setattr(attrs, hits[i], NULL) # does by reference
}
Expand Down
2 changes: 1 addition & 1 deletion R/fread.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fread <- function(input="",file=NULL,text=NULL,cmd=NULL,sep="auto",sep2="auto",d
on.exit(unlink(tmpFile), add=TRUE)
# nocov end
}
else if (length(grep(' ', input)) && !file.exists(input)) { # file name or path containing spaces is not a command
else if (length(grep(' ', input, fixed = TRUE)) && !file.exists(input)) { # file name or path containing spaces is not a command
cmd = input
if (input_has_vars && getOption("datatable.fread.input.cmd.message", TRUE)) {
message("Taking input= as a system command ('",cmd,"') and a variable has been used in the expression passed to `input=`. Please use fread(cmd=...). There is a security concern if you are creating an app, and the app could have a malicious user, and the app is not running in a secure envionment; e.g. the app is running as root. Please read item 5 in the NEWS file for v1.11.6 for more information and for the option to suppress this message.")
Expand Down
4 changes: 2 additions & 2 deletions R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ss = body(tt)
if (class(ss)[1L]!="{") ss = as.call(c(as.name("{"), ss))
prefix = if (!missing(pkgname)) "data.table::" else "" # R provides the arguments when it calls .onLoad, I don't in dev/test
if (!length(grep("data.table",ss[[2L]]))) {
if (!length(grep("data.table", ss[[2L]], fixed = TRUE))) {
ss = ss[c(1L, NA, 2L:length(ss))]
ss[[2L]] = parse(text=paste0("if (!identical(class(..1),'data.frame')) for (x in list(...)) { if (inherits(x,'data.table')) return(",prefix,"data.table(...)) }"))[[1]]
body(tt)=ss
Expand All @@ -27,7 +27,7 @@
tt = base::rbind.data.frame
ss = body(tt)
if (class(ss)[1L]!="{") ss = as.call(c(as.name("{"), ss))
if (!length(grep("data.table",ss[[2L]]))) {
if (!length(grep("data.table", ss[[2L]], fixed = TRUE))) {
ss = ss[c(1L, NA, 2L:length(ss))]
ss[[2L]] = parse(text=paste0("for (x in list(...)) { if (inherits(x,'data.table')) return(",prefix,".rbind.data.table(...)) }"))[[1L]] # fix for #4995
body(tt)=ss
Expand Down
4 changes: 2 additions & 2 deletions R/setkey.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ setkeyv <- function(x, cols, verbose=getOption("datatable.verbose"), physical=TR
cols = colnames(x) # All columns in the data.table, usually a few when used in this form
} else {
# remove backticks from cols
cols <- gsub("`", "", cols)
cols <- gsub("`", "", cols, fixed = TRUE)
miss = !(cols %chin% colnames(x))
if (any(miss)) stop("some columns are not in the data.table: ", paste(cols[miss], collapse=","))
}
Expand Down Expand Up @@ -321,7 +321,7 @@ setorderv <- function(x, cols = colnames(x), order=1L, na.last=FALSE)
}
if (!all(nzchar(cols))) stop("cols contains some blanks.") # TODO: probably I'm checking more than necessary here.. there are checks in 'forderv' as well
# remove backticks from cols
cols <- gsub("`", "", cols)
cols <- gsub("`", "", cols, fixed = TRUE)
miss = !(cols %chin% colnames(x))
if (any(miss)) stop("some columns are not in the data.table: ", paste(cols[miss], collapse=","))
if (".xi" %chin% colnames(x)) stop("x contains a column called '.xi'. Conflicts with internal use by data.table.")
Expand Down
6 changes: 6 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -13320,6 +13320,12 @@ test(1982.1, tt[1L], "")
tt = as.integer(tt[tt!=""])
test(1982.2, tt, seq_along(tt))

# parse(text = 'list(`\\phantom{.}`)') fails, #3319
library(data.table)
DT <- data.table(x = 1, y = 1:5)
setnames(DT, "x", "\\phantom{.}")
test(1983.1, DT[, .(y = mean(y)), keyby = "\\phantom{.}"],
data.table(`\\phantom{.}` = 1, y = 3, key = '\\phantom{.}'))

###################################
# Add new tests above this line #
Expand Down