Skip to content

Instantly share code, notes, and snippets.

@leoluyi
Created December 5, 2014 11:49
Show Gist options
  • Save leoluyi/037309380826ca21db7b to your computer and use it in GitHub Desktop.
Save leoluyi/037309380826ca21db7b to your computer and use it in GitHub Desktop.
### Using the Windows Clipboard, or Passing Data Quickly From Excel to R and Back Again
## http://www.r-bloggers.com/using-the-windows-clipboard-or-passing-data-quickly-from-excel-to-r-and-back-again/
# Copy data out of R
copy.table <- function(obj, size = 4096) {
clip <- paste('clipboard-', size, sep = '')
f <- file(description = clip, open = 'w')
write.table(obj, f, row.names = FALSE, sep = '\t')
close(f)
}
# Paste data into R
paste.table <- function() {
f <- file(description = 'clipboard', open = 'r')
df <- read.table(f, sep = '\t', header = TRUE)
close(f)
return(df)
}
# # These all work
# copy.table(1:100)
# copy.table(letters)
# copy.table(my.df)
# copy.table(table(my.df$col1))
# copy.table(matrix(1:20, nrow = 2))
#
# # If my.df is of moderate size
# copy.table(my.df)
#
# # If my.df is huge
# copy.table(my.df, 10000)
#
# # Pasting works in a similar way. Select the range in Excel you want to copy
# # (including the header) and press Ctrl+C. Then run
#
# other.df <- paste.table()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment