-
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
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,21 @@ 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))) { | ||
| is_windows = identical(.Platform$OS.type, "windows") | ||
| if (is_windows) { | ||
| # for errors due to permissions, clipboard locked or system errors | ||
| clip = tryCatch(utils::readClipboard(), | ||
| error = function(e) stopf("Reading clipboard failed on Windows: %s", conditionMessage(e)) | ||
| ) | ||
| if (!length(clip) || !any(nzchar(trimws(clip)))) { | ||
| stopf("Clipboard is empty.") | ||
| } | ||
| input = paste(clip, collapse="\n") | ||
| } else { | ||
| # Note: macOS (pbpaste) and Linux (xclip/xsel) support discussed in #1292 | ||
| stopf("Clipboard reading is supported on Windows only.") | ||
|
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. i wonder if we must
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 added the explicit error to avoid this potentially confusing behavior on non-Windows systems. > temp <- fread("clipboard")
> print(temp)
Empty data.table (0 rows and 1 cols): clipboardThat said, if preserving the current fallback behavior is preferred for compatibility, I’m happy to remove the stopf() and let the normal control flow continue. |
||
| } | ||
| } 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)) { | ||
|
|
||
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.
Why do we use grepl here and not simply only read from clipboard when the input is
clipboard?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.
I used regex to consider both 'clipboard' and 'clipboard-128' because original issue #1292 shows the user trying fread('clipboard-128').
However your point is valid, since readClipboard() takes no argument and only read clipboard regardless of any suffix so we can use simple string matching.
but the user who are used to base R's read.delim('clipboard-128') syntax would have to use 'clipboard'. let me know if u want the change I will do it.
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.
On Windows, R's own clipboard connections can be opened with the description
clipboard-<buffer size>in order to set the size limit for writing. It's not needed to read the clipboard. On Unix,X11_primary,X11_secondary,X11_clipboardare also supported. We don't have to support anything exceptclipboard.