R到剪贴板不能在mac上工作

时间:2012-08-02 15:43:34

标签: macos r

我已将strwrap函数包装到strWrap中,该函数接受剪贴板中的文本以及自动写入剪贴板。在这里我假设mac的系统名称是Darwin。这个功能适用于Windows机器(抱歉Linux有太多的变化使其变得可行,而且大多数使用此功能的软件包的用户都不会是linux用户)。

我在psych套餐read.clipboard功能之后对我的功能进行了建模。不幸的是,我让一些人在talkstats.com尝试使用mac并且它无法正常工作。我怎样才能为Mac做这个工作?根据{{​​3}},我的代码似乎也适用于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)

1 个答案:

答案 0 :(得分:3)

pipe返回一个连接对象。您需要从连接中读取。 例如

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

这适用于我的Mac。