[R] readClipboard() from spreadsheet to data.frame with column names?

Kenneth Hobson khobson at aaahawk.com
Thu Jan 24 17:21:47 CET 2008

This code opens a CSV file from the working directory set by wd.  I copy 
2 or more columns in the spreadsheet and then close it.  What I need 
help with is setting the first row as the column names in 
clipboardTextAsDF.  There is probably a simple or better way to do this 
but I have been away from R for awhile now.  I just be over-thinking this.

I tried read.DIF to read the clipboard but that didn't work as I had hoped.

writeClipboard(rownames(state.x77), format=1)
#A paste, Ctrl+V in any Application should now list United States State 

statex77 <- cbind(State=rownames(state.x77), state.x77)

wd <- "b:/Chapter11/"
setwd(wd)    #Note use of "/" rather than "\"
write.csv(statex77, file="StateX77.csv", row.names=FALSE)
StateX77CSV <- paste(wd,"StateX77.csv",sep="")

shell(StateX77CSV) #Might open Excel rather than QP
#data.entry(statex77)  #Show statex77 in an interactive spreadhseet manner

clipboardText <- strsplit(readClipboard(format=1), "\t")
clipboardTextAsDF <- t(data.frame(clipboardText))

More information about the R-help mailing list