2012-06-09 31 views
24

我正在運行Ubuntu 11.10,我希望能夠寫入剪貼板(或主要選擇)。下面給出了一個錯誤如何在R/Ubuntu上寫入到Ubuntu/Linux上的剪貼板?

> x <- 1:10 
> dput(x, 'clipboard') 
Error in file(file, "wt") : 'mode' for the clipboard must be 'r' on Unix 

我怎麼能寫到剪貼板/主選擇?

請注意,我看過this old R-Help post,但我仍然不清楚我應該做什麼。

Linux沒有剪貼板,但X11會話具有主要和 次要選擇。 ?文件說

剪貼板:

'file' can also be used with 'description = "clipboard"' in mode 
    '"r"' only. It reads the X11 primary selection, which can also be 
    specified as '"X11_primary"' and the secondary selection as 
    '"X11_secondary"'. 

    When the clipboard is opened for reading, the contents are 
    immediately copied to internal storage in the connection. 

    Unix users wishing to _write_ to the primary selection may be able 
    to do so via 'xclip' (<URL: 
    http://people.debian.org/~kims/xclip/>), for example by 
    'pipe("xclip -i", "w")'. 

所以RTFM應用。寫入X11選擇需要多個線程 ,我並不認爲它值得執行(與Windows不同)的非常大的努力。

請注意,窗口管理器可能有其他剪貼板,例如 RGtk2軟件包具有接口到gtk剪貼板。

回答

12

不知道這是最好方式,但這裏是我怎麼能得到它的工作:

  1. 安裝XCLIP:sudo apt-get install xclip
  2. 閱讀手冊:man xclip
  3. 寫入X11初級R:write.table(1:10, pipe("xclip -i", "w"))

更新:

注意,直到管道關閉傳遞給write.table對象將不會出現在剪貼板中。您可以通過致電gc()來強制關閉管道。例如:

write.table(1:10, pipe("xclip -i", "w")) # data may not be in clipboard 
gc()          # data written to primary clipboard 

一種更好的方式來管理連接是使用功能與on.exit(close(con)),即使write.table調用拋出一個錯誤,將關閉該管道。請注意,根據您的系統設置,您需要確保您正在寫入您打算使用的剪貼板(主要是默認設置)。

write.xclip <- function(x, selection=c("primary", "secondary", "clipboard"), ...) { 
    if (!isTRUE(file.exists(Sys.which("xclip")[1L]))) 
    stop("Cannot find xclip") 
    selection <- match.arg(selection)[1L] 
    con <- pipe(paste0("xclip -i -selection ", selection), "w") 
    on.exit(close(con)) 
    write.table(x, con, ...) 
} 
+0

+1謝謝。我很感激。我仍然很想看看還有其他什麼策略。我偶爾喜歡粘貼到剪貼板的主要原因是保存幾秒鐘,而不是寫入文件並從文件中複製。上述策略似乎假設我可以提前預測我需要剪貼板。我也無法在R Studio中使用它。我只能讓它和控制檯一起工作。 –

+0

@JeromyAnglim我也注意到這是一個RStudio問題,所以你最好向RStudio開發者報告。我不知道他們用剪貼板做了什麼。 –

+1

這對我來說並不適用於ubuntu,無論是在R Studio還是在R的終端版本中。我承認我沒有完全閱讀xclip的人(第2步),但我不認爲應該會影響結果。 – geneorama

16
clipboard <- function(x, sep="\t", row.names=FALSE, col.names=TRUE){ 
    con <- pipe("xclip -selection clipboard -i", open="w") 
    write.table(x, con, sep=sep, row.names=row.names, col.names=col.names) 
    close(con) 
} 

vec <- c(1,2,3,4) 

clipboard(vec) 
clipboard(vec, ",", col.names=FALSE) 
clipboard(vec, " ", row.names=TRUE) 

您可以創建功能,例如後粘貼回所寫的任何東西到剪貼板。默認返回製表符分隔的列,但沒有列名。指定其他分隔符,包含行名稱,或按照您的喜好排除列名稱,如圖所示。

編輯:爲了澄清,你仍然需要安裝xclip。不過,你不需要首先單獨啓動它。

+0

這些解決方案都不適用於我:-( –

0

版本:

  • 薄荷18.1,肉桂
  • XCLIP 0.12
  • [R 3.4.0(2017年4月21日)

我不能讓其他的解決方案工作,所以我man編輯。這種方法適用於我(基於他人的解決方案)。

write_clipboard = function(x, .rownames = F) { 
    #decide how to write 
    #windows is easy! 
    if (Sys.info()['sysname'] %in% c("Windows")) { 
     #just write as normal 
     write.table(x, "clipboard", sep = "\t", na = "", row.names = F) 
    } else { 
     #for non-windows, try xclip approach 
     #https://stackoverflow.com/a/10960498/3980197 
     write.xclip = function(x) { 
     #if xclip not installed 
     if (!isTRUE(file.exists(Sys.which("xclip")[1L]))) { 
      stop("Cannot find xclip") 
     } 
     con <- pipe("xclip -selection c", "w") 
     on.exit(close(con)) 
     write.table(x, con, sep = "\t", na = "", row.names = F) 
     } 

     tryCatch({ 
     write.xclip(x) 
     }, error = function(e) { 
     message("Could not write using xclip") 
     }) 
    } 
} 

這是在my personal R package中減弱功能的版本。

從剪貼板中讀取

讀書同樣困難。這是上述的伴侶功能。

read_clipboard = function(header = T, 
          sep = "\t", 
          na.strings = c("", "NA"), 
          check.names = T, 
          stringsAsFactors = F, 
          dec = ".", 
          ...) { 
    #decide how to read 
    #windows is easy! 
    if (Sys.info()['sysname'] %in% c("Windows")) { 
    #just read as normal 
    read.table(file = con, sep = sep, header = header, check.names = check.names, na.strings = na.strings, stringsAsFactors = stringsAsFactors, dec = dec, ...) 
    } else { 
    #for non-windows, try xclip approach 
    #https://stackoverflow.com/a/10960498/3980197 
    read.xclip = function(x) { 
     #if xclip not installed 
     if (!isTRUE(file.exists(Sys.which("xclip")[1L]))) { 
     stop("Cannot find xclip") 
     } 
     con <- pipe("xclip -o -selection c", "r") 
     on.exit(close(con)) 
     read.table(file = con, sep = sep, header = header, check.names = check.names, na.strings = na.strings, stringsAsFactors = stringsAsFactors, dec = dec, ...) 
    } 

    tryCatch({ 
     read.xclip(x) 
    }, error = function(e) { 
     message(sprintf("error: %s", e$message)) 
    }) 
    } 
} 
相關問題