-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix fread clipboard handling on Windows (fixes #1292) #7640
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
base: master
Are you sure you want to change the base?
Changes from all commits
546de99
c68136d
43b8a22
b5a2a7c
a8abf6c
6c34cba
b95ca4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -63,6 +63,36 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC") | |||||||||
| # input is data itself containing at least one \n or \r | ||||||||||
| } else if (startsWith(input, " ")) { | ||||||||||
| stopf("input= contains no \\n or \\r, but starts with a space. Please remove the leading space, or use text=, file= or cmd=") | ||||||||||
| } else if (grepl("^clipboard(-[0-9]+)?$", tolower(input))) { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we use grepl here and not simply only read from clipboard when the input is
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used regex to consider both 'clipboard' and 'clipboard-128' because original issue #1292 shows the user trying fread('clipboard-128').
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Windows, R's own clipboard connections can be opened with the description
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes as mentioned by Ivan, using a buffer size after I would go with |
||||||||||
| os_type = .Platform$OS.type | ||||||||||
| if (identical(os_type, "windows")) { | ||||||||||
| clip = tryCatch(utils::readClipboard(), | ||||||||||
| error = function(e) stopf("Reading clipboard failed on Windows: %s", conditionMessage(e)) | ||||||||||
| ) | ||||||||||
| } else if (identical(os_type, "unix")) { | ||||||||||
| sysname = Sys.info()[["sysname"]] | ||||||||||
| clip_cmd = if (identical(sysname, "Darwin")) { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wont this be uninitialized on Solaris and friends? |
||||||||||
| "pbpaste" | ||||||||||
| } else if (identical(sysname, "Linux")) { | ||||||||||
| if (nzchar(Sys.which("wl-paste"))) "wl-paste --no-newline" | ||||||||||
| else if (nzchar(Sys.which("xclip"))) "xclip -o -selection clipboard" | ||||||||||
| else if (nzchar(Sys.which("xsel"))) "xsel --clipboard --output" | ||||||||||
| else stopf("Clipboard reading on Linux requires 'xclip', 'xsel', or 'wl-paste' to be installed and on PATH.") | ||||||||||
| } | ||||||||||
| clip = tryCatch(system(clip_cmd, intern = TRUE), | ||||||||||
| error = function(e) stopf("Reading clipboard failed: %s", conditionMessage(e)) | ||||||||||
| ) | ||||||||||
| status = attr(clip, "status") | ||||||||||
| if (!is.null(status) && status != 0L) { | ||||||||||
| stopf("Reading clipboard failed (exit %d). Ensure '%s' is working.", status, clip_cmd) | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| warning("Clipboard reading is not supported on this platform.", call. = FALSE) | ||||||||||
| } | ||||||||||
| if (!length(clip) || !any(nzchar(trimws(clip)))) { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on non windows and non-unix this will error because of |
||||||||||
| stopf("Clipboard is empty.") | ||||||||||
| } | ||||||||||
| input = paste(clip, collapse = "\n") | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Using paste might allocate a large string before dumping it to disk |
||||||||||
| } else if (length(grep(' ', input, fixed=TRUE)) && !file.exists(gsub("^file://", "", input))) { # file name or path containing spaces is not a command. file.exists() doesn't understand file:// (#7550) | ||||||||||
| cmd = input | ||||||||||
| if (input_has_vars && getOption("datatable.fread.input.cmd.message", TRUE)) { | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21520,3 +21520,48 @@ test(2365.1, melt(df_melt, id.vars=1:2), melt(dt_melt, id.vars=1:2)) | |
| df_dcast = data.frame(a = c("x", "y"), b = 1:2, v = 3:4) | ||
| dt_dcast = data.table(a = c("x", "y"), b = 1:2, v = 3:4) | ||
| test(2365.2, dcast(df_dcast, a ~ b, value.var = "v"), dcast(dt_dcast, a ~ b, value.var = "v")) | ||
|
|
||
| # Test fread clipboard input on Windows (issue #1292) | ||
| if (.Platform$OS.type == "windows") local({ | ||
| temp = c("a\tb", "1\t2") | ||
| utils::writeClipboard(temp) | ||
| on.exit(utils::writeClipboard(""), add = TRUE) | ||
| test(2366, fread("clipboard-128"), data.table(a = 1L, b = 2L)) | ||
| }) | ||
|
|
||
| # Test fread clipboard input on macOS (issue #1292) | ||
| if (.Platform$OS.type == "unix" && identical(Sys.info()[["sysname"]], "Darwin")) local({ | ||
| if (nzchar(Sys.which("pbcopy"))) { | ||
| con = pipe("pbcopy", "w") | ||
| writeLines(c("a\tb", "1\t2"), con) | ||
| close(con) | ||
| on.exit({ | ||
| cl = pipe("pbcopy", "w") | ||
| writeLines("", cl) | ||
| close(cl) | ||
| }, add=TRUE) | ||
| test(2366.1, fread("clipboard"), data.table(a=1L, b=2L)) | ||
| } | ||
| }) | ||
|
|
||
| # Test fread clipboard input on Linux via xclip/xsel/wl-paste (issue #1292) | ||
| if (.Platform$OS.type == "unix" && identical(Sys.info()[["sysname"]], "Linux")) local({ | ||
| has_wl = nzchar(Sys.which("wl-copy")) && nzchar(Sys.which("wl-paste")) | ||
| has_xclip = nzchar(Sys.which("xclip")) | ||
| has_xsel = nzchar(Sys.which("xsel")) | ||
| writer = if (has_wl) "wl-copy" | ||
| else if (has_xclip) "xclip -i -selection clipboard" | ||
| else if (has_xsel) "xsel --clipboard --input" | ||
| else "" | ||
| if (nzchar(writer)) { | ||
| con = pipe(writer, "w") | ||
| writeLines(c("a\tb", "1\t2"), con) | ||
| close(con) | ||
| on.exit({ | ||
| cl = pipe(writer, "w") | ||
| writeLines("", cl) | ||
| close(cl) | ||
| }, add=TRUE) | ||
| test(2366.2, fread("clipboard"), data.table(a=1L, b=2L)) | ||
| } | ||
| }) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing newline at end of test file |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls elaborate more what this means, e.g. that
freadsupports now reading from the clipboard viafread("clipboard")