如果您查看其他 StackOverflow 问题,例如这个问题,您会看到xclip
建议的以及两个方便的别名
alias setclip="xclip -selection c"
alias getclip="xclip -selection c -o"
这有力地暗示了这是如何工作的。为了测试,我安装了xclip
(可用于我运行的 Ubuntu),突出显示 R 启动文本中的三行并尝试了它:
edd@rob:~$ xclip -selection c -o
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
edd@rob:~$
像宣传xclip
的那样工作:恢复剪贴板内容,并将其打印到 stdout。
要在 R 中使用它,我们只需要从输出中读取,我们可以通过pipe()
连接:
> res <- readLines(pipe("xclip -selection c -o"))
> str(res)
chr [1:19] "" ...
> res[1:3]
[1] ""
[2] "R version 4.0.3 (2020-10-10) -- \"Bunny-Wunnies Freak Out\""
[3] "Copyright (C) 2020 The R Foundation for Statistical Computing"
>
但即使这样也太过分了。看着??clipboard
建议help(connections)
有一个完整的段落(!!)关于这个,其中包括
‘file’ can be used with ‘description = "clipboard"’ in mode ‘"r"’
only. This reads the X11 primary selection (see <URL:
https://specifications.freedesktop.org/clipboards-spec/clipboards-latest.txt>),
which can also be specified as ‘"X11_primary"’ and the secondary
selection as ‘"X11_secondary"’. On most systems the clipboard
selection (that used by ‘Copy’ from an ‘Edit’ menu) can be
specified as ‘"X11_clipboard"’.
确实:
> res2 <- readLines(file(description="clipboard"))
Warning message:
In readLines(file(description = "clipboard")) :
incomplete final line found on 'clipboard'
> str(res2)
chr [1:19] "" ...
> res2[1:3]
[1] ""
[2] "R version 4.0.3 (2020-10-10) -- \"Bunny-Wunnies Freak Out\""
[3] "Copyright (C) 2020 The R Foundation for Statistical Computing"
>
看起来,您仍然需要xclip
从R写入剪贴板。