2012-08-02 31 views
2

我已将strwrap函数包装到中,该函数接受剪贴板中的文本以及自动写入剪贴板。在此我假设mac的系统名称是达尔文。这个功能可以在Windows机器上运行(对不起,Linux有太多的变化,使得它可行)&大多数使用这个功能的软件包的用户不会是Linux用户。R到剪贴板将无法在Mac上工作

我在psych程序包的read.clipboard函数之后模拟了我的函数。不幸的是,我让一些人试用了talkstats.com谁有一个mac,它不起作用。我怎样才能使这个工作为Mac?根据this SO post我的代码似乎也应该适用于mac用户。

如果按预期工作,它应该既可以从剪贴板中为mac用户读取,也可以在完成后写入剪贴板。我已标记在最后的MAC专用线与#的问题

strWrap <- 
function(text = "clipboard", width = 70) { 
    if (text == "clipboard") { 
     if (Sys.info()["sysname"] == "Darwin") {  # 
      text <- paste(pipe("pbpaste"), collapse=" ")# 
     }            # 
     if (Sys.info()["sysname"] == "Windows") { 
      text <- paste(readClipboard(), collapse=" ") 
     } 
    } 
    x <- gsub("\\s+", " ", gsub("\n|\t", " ", text)) 
    x <- strwrap(x, width = width) 
    if (Sys.info()["sysname"] == "Windows") { 
     writeClipboard(x, format = 1) 
    } 
    if (Sys.info()["sysname"] == "Darwin") {   # 
     j <- pipe("pbcopy", "w")      # 
     cat(x, file = j)        # 
     close(j)          # 
    }             # 
    writeLines(x) 
} 

X <- "Two households, both alike in dignity, In fair Verona, where we lay 
our scene, From ancient grudge break to new mutiny, Where civil blood 
makes civil hands unclean. From forth the fatal loins of these two 
foes A pair of star-cross'd lovers take their life; Whose 
misadventured piteous overthrows Do with their death bury their 
parents' strife. The fearful passage of their death-mark'd love, And 
the continuance of their parents' rage, Which, but their children's 
end, nought could remove, Is now the two hours' traffic of our stage; 
The which if you with patient ears attend" 

strWrap(X, 70) 
+0

“sorry Linux”有没有人对Linux做过任何事情? – user1945827 2013-05-08 10:36:29

+0

对于Linux Mint/Ubuntu/Debian,请参阅https://stackoverflow.com/questions/10959521/how-to-write-to-clipboard-on-ubuntu-linux-in-r/44741951#44741951。 – Deleet 2017-06-26 20:55:02

+0

我会补充说,clipr软件包的目标是成为一个操作系统独立。解决方案复制到/从剪贴板https://cran.r-project.org/web/packages/clipr/index.html – 2017-06-28 16:29:43

回答

3

pipe返回一个连接对象更容易理解。您需要从连接中读取。 例如

pcon <- pipe("pbpaste") 
text <- paste(scan(pcon, what="character", quiet=TRUE), collapse=" ") 
close(pcon) 

这对我的Mac有用。

+0

请参阅[此链接](http://www.talkstats.com/showthread.php/ 27194-test-and-improve-a-function-that-s-OS-independant?p = 89986&viewfull = 1#post89986)的最终解决方案。 – 2012-08-03 02:33:42