2018-03-23 14:46:02 +01:00
#' Import/export from clipboard
#'
2018-04-03 16:07:32 +02:00
#' These are helper functions around \code{\link{read.table}} and \code{\link{write.table}} to import from and export to clipboard with support for Windows, Linux and macOS. The data will be read and written as tab-separated by default, which makes it possible to copy and paste from other software like Excel and SPSS without further transformation. See Details for an example.
2018-03-23 14:46:02 +01:00
#' @rdname clipboard
#' @name clipboard
#' @inheritParams utils::read.table
#' @inheritParams utils::write.table
2018-04-03 16:07:32 +02:00
#' @param startrow \emph{n}th row to start importing from. When \code{header = TRUE}, the import will start on row \code{startrow} \emph{below} the header.
2018-03-23 14:46:02 +01:00
#' @param as_vector a logical value indicating whether data consisting of only one column should be imported as vector using \code{\link[dplyr]{pull}}. This will strip off the header.
2018-03-27 17:43:42 +02:00
#' @param info print info about copying
2018-03-23 14:46:02 +01:00
#' @keywords clipboard clipboard_import clipboard_export import export
#' @importFrom dplyr %>% pull as_tibble
2018-03-23 14:59:02 +01:00
#' @importFrom utils read.delim write.table object.size
2018-04-03 16:07:32 +02:00
#' @details For \code{clipboard_export()}, the reserved clipboard size for exporting will be set to 125\% of the object size of \code{x}. This way, it is possible to export data with thousands of rows as the only limit will be your systems RAM.
#'
#' Example for copying from Excel:
#' \if{html}{
#' \out{<div style="text-align: left">}\figure{Excel_copy.png}\out{</div>}
#' }
#' \if{latex}{
#' \out{\begin{left}}\figure{Excel_copy.png}\out{\end{left}}
#' }
#' \cr
#' And pasting in R: \cr \cr
#' \code{> data <- clipboard_import()} \cr
#' \code{> data} \cr
#' \if{html}{
#' \out{<div style="text-align: left">}\figure{Excel_paste.png}\out{</div>}
#' }
#' \if{latex}{
#' \out{\begin{left}}\figure{Excel_paste.png}\out{\end{left}}
#' }
2018-03-23 14:46:02 +01:00
#' @export
#' @return data.frame
clipboard_import <- function ( sep = ' \t' ,
header = TRUE ,
dec = " ." ,
na = c ( " " , " NA" , " NULL" ) ,
startrow = 1 ,
as_vector = TRUE ) {
2018-03-29 13:10:55 +02:00
if ( is_Windows ( ) == TRUE ) {
file <- ' clipboard'
} else {
# use xclip package
check_xclip ( )
2018-03-29 14:56:40 +02:00
file <- pipe ( " xclip -o" , " r" )
2018-03-29 13:23:02 +02:00
on.exit ( close ( file ) )
2018-03-29 13:10:55 +02:00
}
2018-04-02 11:11:21 +02:00
import_tbl <- tryCatch ( read.delim ( file = file ,
sep = sep ,
header = header ,
strip.white = TRUE ,
dec = dec ,
na.strings = na ,
fileEncoding = ' UTF-8' ,
encoding = ' UTF-8' ,
stringsAsFactors = FALSE ) ,
error = function ( e ) {
FALSE
} )
if ( import_tbl == FALSE ) {
cat ( " No clipboard content found." )
if ( Sys.info ( ) [ ' sysname' ] %like% " Linux" ) {
cat ( " These functions do not work without X11 installed." )
}
cat ( " \n" )
return ( invisible ( ) )
}
2018-03-23 14:46:02 +01:00
# use tibble, so column types will be translated correctly
import_tbl <- as_tibble ( import_tbl )
2018-04-02 11:11:21 +02:00
2018-03-23 14:46:02 +01:00
if ( startrow > 1 ) {
# would else lose column headers
import_tbl <- import_tbl [startrow : nrow ( import_tbl ) , ]
}
2018-04-02 11:11:21 +02:00
2018-03-23 14:46:02 +01:00
colnames ( import_tbl ) <- gsub ( ' [.]+' , ' _' , colnames ( import_tbl ) )
2018-04-02 11:11:21 +02:00
2018-03-23 14:46:02 +01:00
if ( NCOL ( import_tbl ) == 1 & as_vector == TRUE ) {
import_tbl %>% pull ( 1 )
} else {
import_tbl
}
}
#' @rdname clipboard
#' @importFrom dplyr %>% pull as_tibble
#' @export
clipboard_export <- function ( x ,
sep = ' \t' ,
dec = " ." ,
na = " " ,
2018-03-27 17:43:42 +02:00
header = TRUE ,
info = TRUE ) {
2018-04-02 11:11:21 +02:00
2018-03-23 14:46:02 +01:00
x <- deparse ( substitute ( x ) )
size <- x %>%
2018-04-02 11:11:21 +02:00
get ( ) %>%
2018-03-23 14:46:02 +01:00
object.size ( ) %>%
formatC ( format = ' d' ) %>%
as.integer ( )
2018-04-02 11:11:21 +02:00
2018-03-23 14:46:02 +01:00
x <- get ( x )
2018-03-23 14:52:56 +01:00
2018-03-29 13:10:55 +02:00
if ( is_Windows ( ) == TRUE ) {
# set size of clipboard to 125% of the object size of x
file <- paste0 ( " clipboard-" , size * 1.25 )
} else {
# use xclip package
check_xclip ( )
2018-03-29 14:16:42 +02:00
file <- pipe ( " xclip -i" , " w" )
2018-03-29 13:23:02 +02:00
on.exit ( close ( file ) )
2018-03-29 13:10:55 +02:00
}
2018-04-02 11:11:21 +02:00
tryCatch ( write.table ( x = x ,
file = file ,
sep = sep ,
na = na ,
row.names = FALSE ,
col.names = header ,
dec = dec ,
quote = FALSE ) ,
error = function ( e ) {
FALSE
} )
2018-03-23 14:52:56 +01:00
2018-03-27 17:43:42 +02:00
if ( info == TRUE ) {
cat ( " Successfully exported to clipboard:" , NROW ( x ) , " obs. of" , NCOL ( x ) , " variables.\n" )
}
2018-04-02 11:11:21 +02:00
2018-03-23 14:46:02 +01:00
}
2018-03-29 13:10:55 +02:00
is_Windows <- function ( ) {
Sys.info ( ) [ ' sysname' ] %like% " Windows"
}
check_xclip <- function ( ) {
if ( ! isTRUE ( file.exists ( Sys.which ( " xclip" ) [1L ] ) ) ) {
2018-04-02 11:11:21 +02:00
if ( Sys.info ( ) [ ' sysname' ] %like% " Linux" ) {
2018-03-29 13:10:55 +02:00
stop ( " Please install Linux package xclip first." )
2018-04-02 11:11:21 +02:00
} else {
stop ( " Please install package xclip first (use `brew install xclip on macOS`)." )
}
2018-03-29 13:10:55 +02:00
}
}