Copying text from r studio tables

1,092 views
Skip to first unread message

Jens Stevens

unread,
May 25, 2015, 11:47:06 PM5/25/15
to davi...@googlegroups.com
I'm a big fan of RStudio and almost all their updates are for the better. But sometimes I need to copy-paste some information from a data table in the RStudio data viewer over to a spreadsheet, and with the new version of RStudio (Version 0.99.441, downloaded a few weeks ago, might still be in beta), it's now harder to do that. This is partly because of great new functionality that lets you sort and filter columns in your data table viewer while freezing column names. But I'd like to have my cake and eat it too, please. Has anyone discovered a hack? I could always write.csv()'s, but that's a little heavy handed for what I'm trying to do, even if it's technically not proper form to copy'n'paste from R.

Noam Ross

unread,
May 26, 2015, 11:01:35 AM5/26/15
to davi...@googlegroups.com

The Kmisc package has a bunch of clipboard functions, including write.cb(), which will copy data to the clipboard. FYI, I’ve found sometimes it prints an error even when it’s working correctly.



On Mon, May 25, 2015 at 8:47 PM Jens Stevens <jtst...@ucdavis.edu> wrote:
I'm a big fan of RStudio and almost all their updates are for the better. But sometimes I need to copy-paste some information from a data table in the RStudio data viewer over to a spreadsheet, and with the new version of RStudio (Version 0.99.441, downloaded a few weeks ago, might still be in beta), it's now harder to do that. This is partly because of great new functionality that lets you sort and filter columns in your data table viewer while freezing column names. But I'd like to have my cake and eat it too, please. Has anyone discovered a hack? I could always write.csv()'s, but that's a little heavy handed for what I'm trying to do, even if it's technically not proper form to copy'n'paste from R.

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Vince S. Buffalo

unread,
May 26, 2015, 12:58:57 PM5/26/15
to davi...@googlegroups.com
Hi Jens,

In case this is an X-Y problem  — why do you need to copy and paste data from RStudio to a spreadsheet? My (maybe too opinionated) view is that this probably shouldn't be happening — shuffling data to and from R with copy/paste is a recipe for different data versions to confound results (I've been bitten by something similar before).

The workflow I recommend to folks is: treat data as read only, meaning don't edit the data in a non-reproducible way. This way one set of data is tied to one set of results, and one set of code that produced those results. If you need to make changes in the data in the spreadsheet (e.g. if you made a data entry here), do that, save the results, and make a note of the changes (placing tabular data in version control is very useful here too). In my workflows, I have a raw data file (never changed), a cleaned data file (changed with Python or other scripting languages — spreadsheet changes can be made too), and then my R code reads the cleaned data file to run analyses and generate results. It may be different for your particular workflow though.

hope this helps,
Vince

On Mon, May 25, 2015 at 8:47 PM, Jens Stevens <jtst...@ucdavis.edu> wrote:
I'm a big fan of RStudio and almost all their updates are for the better. But sometimes I need to copy-paste some information from a data table in the RStudio data viewer over to a spreadsheet, and with the new version of RStudio (Version 0.99.441, downloaded a few weeks ago, might still be in beta), it's now harder to do that. This is partly because of great new functionality that lets you sort and filter columns in your data table viewer while freezing column names. But I'd like to have my cake and eat it too, please. Has anyone discovered a hack? I could always write.csv()'s, but that's a little heavy handed for what I'm trying to do, even if it's technically not proper form to copy'n'paste from R.

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--
Vince Buffalo
@vsbuffalo :: vincebuffalo.com
Coop Lab :: Population Biology Graduate Group
University of California, Davis

Michael Hannon

unread,
May 26, 2015, 7:31:46 PM5/26/15
to davi...@googlegroups.com
+1 for this. I had a somewhat similar experience with a woman in our
group. She wanted to get some data from R into Excel. I asked her
why she wanted to put data into Excel. Her response was that she'd
always used Excel, and she asked me if there were a better spreadsheet
she should be using. I suggested she might just want to forget about
the spreadsheet entirely.

I have to admit to having used Excel only for bonehead tasks, so I may
be clueless about its benefits, but given all of R's data-processing
and graphing capabilities, I don't see why anybody starting with R
would not just keep the whole process within R.

-- Mike

Michael Hannon

unread,
May 26, 2015, 10:37:45 PM5/26/15
to davi...@googlegroups.com
Hi, folks. Noam (in a private email) whacked me across the nose with
a rolled-up newspaper, suggesting, basically, that I should just shut
up unless/until I have some sense of a person's familiarity with R and
of the person's requirements (e.g., maybe the funding agency wants an
Excel file).

OK, point well taken. I just took a peek around, looking for ways to
send data from R to Excel programmatically. I found something and
made a small example. Please see the attached. I don't know if this
exercise will be of use to the OP or anybody else, but it was mildly
instructive for me.

-- Mike
write-to-excel.pdf
write-to-excel.Rmd

Jaime Ashander

unread,
May 26, 2015, 10:49:08 PM5/26/15
to davi...@googlegroups.com, davi...@googlegroups.com
Thanks Michael this is a useful example!

FTR although I'm obviously not the OP, I didn't read either post here as excel shaming or similar. Rather, they seemed considered recommendations on workflow to be taken or left depending on needs. I hope Jens had a similar reading

Jaime


--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

<write-to-excel.pdf><write-to-excel.Rmd>

<write-to-excel.pdf><write-to-excel.Rmd>

Myfanwy Johnston

unread,
May 27, 2015, 1:58:39 PM5/27/15
to davi...@googlegroups.com
In a similar spirit, while I am slowly ridding myself of Excel, I still find this function (which is basically the reverse Jens' process) pretty helpful from time to time:

read.excel <- function(header=TRUE,...) {
  read.table("clipboard",sep="\t",header=header,...)
}

dat=read.excel()

All it does is copy something from Excel onto your clipboard, and save it as an object ("dat") in R.  The warning label here is that if you leave it in an .R script and come back to it months later, YOU WILL NOT REMEMBER what the hell it was you copied to the clipboard.


--
Myfanwy Johnston
Ph.D Candidate, Animal Behavior
University of California at Davis
Biotelemetry Lab

Jaime Ashander

unread,
May 27, 2015, 2:25:06 PM5/27/15
to davi...@googlegroups.com
Wow! This is pretty cool, but unfortunately using "clipboard" this way won't work on unix/linux/mac os x :(

BUT, it looks like the following would:

read.excel <- function(header=TRUE,...) {
  read.table(pipe("pbpaste"),sep="\t",header=header,...)
}

from:
http://stackoverflow.com/questions/13438556/how-do-i-copy-and-paste-data-into-r-from-the-clipboard

Noam Ross

unread,
May 27, 2015, 2:40:10 PM5/27/15
to davi...@googlegroups.com

read.cb() and write.cb() in the Kmisc package actually do pretty much exactly this, but detect your operating system. e.g.,

read.cb <- function(sep='\t', header=TRUE, ...) {
  sn <- Sys.info()["sysname"]
  if (sn == "Darwin") {
    read.table( pipe("pbpaste"), header=header, sep=sep, ... )
  } else if (sn == "Windows") {
    read.table( "clipboard", header=header, sep=sep, ... )
  } else {
    stop("Reading from the clipboard is not implemented for your system (", 
      sn, ") in this package.")
  }
}

cat.cb() and scan.cb() do the same thing for character data.

Jens Stevens

unread,
May 29, 2015, 2:20:58 AM5/29/15
to davi...@googlegroups.com
Hey folks-
Thanks for the variety of suggestions, practical and philosophical. Haven't had a chance to try the practical ones yet but looks like there might be some useful tools in there. Regarding the philosophical, my intention with the copying and the pasting is generally less about data entry/manipulation and more about a quick way to share a small chunk of information, e.g. summary table of alternative model parameters created in R--> Excel --> email to a coauthor. "Excel" is really just a placeholder for "spreadsheet" in this case, and is often kind of a middle-man. I suppose we all use various tools to suit our needs, but it's always good to hear about alternative approaches.

-The OPP
Reply all
Reply to author
Forward
0 new messages