0

basic question: how can I export a table that I print into the R console and paste into a text editor (e.g this dialog box) keeping a decent format. Consider that I want to print the head of my table:

> head(cytoplasmic)

I will get the following output in the console:

   V1                V2            V3                  V4                   V5                    V6
3  LdBPK_020440.1 cytoplasmic: 0.54 nuclear: 0.45  mitochondrial: 0.0 Golgi apparatus: 0.0      peroxisomal: 0.0
5  LdBPK_201720.1 cytoplasmic: 0.89 nuclear: 0.06 mitochondrial: 0.02    peroxisomal: 0.01 Golgi apparatus: 0.01
6  LdBPK_070130.1 cytoplasmic: 0.61 nuclear: 0.32   peroxisomal: 0.05  mitochondrial: 0.02  Golgi apparatus: 0.0
7  LdBPK_040060.1 cytoplasmic: 0.52 nuclear: 0.43 mitochondrial: 0.02  extracellular: 0.01     peroxisomal: 0.01
13 LdBPK_191560.1 cytoplasmic: 0.49 nuclear: 0.46 mitochondrial: 0.03    peroxisomal: 0.01  Golgi apparatus: 0.0
15 LdBPK_364730.1 cytoplasmic: 0.93 nuclear: 0.06  mitochondrial: 0.0     peroxisomal: 0.0  Golgi apparatus: 0.0
                      V7                   V8      V9            V10
3   plasma membrane: 0.0   extracellular: 0.0 ER: 0.0 lysosomal: 0.0
5     extracellular: 0.0 plasma membrane: 0.0 ER: 0.0 lysosomal: 0.0
6   plasma membrane: 0.0   extracellular: 0.0 ER: 0.0 lysosomal: 0.0
7  Golgi apparatus: 0.01 plasma membrane: 0.0 ER: 0.0 lysosomal: 0.0
13  plasma membrane: 0.0   extracellular: 0.0 ER: 0.0 lysosomal: 0.0
15    extracellular: 0.0 plasma membrane: 0.0 ER: 0.0 lysosomal: 0.0

Which does not have a decent format in this text dialog at all.

I also tried using the kable function from the knitr package with the following code:

> kable(head(cytoplasmic, format = "markdown"))

then I get the following output:

   |V1             |V2                |V3            |V4                  |V5                   |V6                    |V7                    |V8                   |V9      |V10            |
|:--|:--------------|:-----------------|:-------------|:-------------------|:--------------------|:---------------------|:---------------------|:--------------------|:-------|:--------------|
|3  |LdBPK_020440.1 |cytoplasmic: 0.54 |nuclear: 0.45 |mitochondrial: 0.0  |Golgi apparatus: 0.0 |peroxisomal: 0.0      |plasma membrane: 0.0  |extracellular: 0.0   |ER: 0.0 |lysosomal: 0.0 |
|5  |LdBPK_201720.1 |cytoplasmic: 0.89 |nuclear: 0.06 |mitochondrial: 0.02 |peroxisomal: 0.01    |Golgi apparatus: 0.01 |extracellular: 0.0    |plasma membrane: 0.0 |ER: 0.0 |lysosomal: 0.0 |
|6  |LdBPK_070130.1 |cytoplasmic: 0.61 |nuclear: 0.32 |peroxisomal: 0.05   |mitochondrial: 0.02  |Golgi apparatus: 0.0  |plasma membrane: 0.0  |extracellular: 0.0   |ER: 0.0 |lysosomal: 0.0 |
|7  |LdBPK_040060.1 |cytoplasmic: 0.52 |nuclear: 0.43 |mitochondrial: 0.02 |extracellular: 0.01  |peroxisomal: 0.01     |Golgi apparatus: 0.01 |plasma membrane: 0.0 |ER: 0.0 |lysosomal: 0.0 |
|13 |LdBPK_191560.1 |cytoplasmic: 0.49 |nuclear: 0.46 |mitochondrial: 0.03 |peroxisomal: 0.01    |Golgi apparatus: 0.0  |plasma membrane: 0.0  |extracellular: 0.0   |ER: 0.0 |lysosomal: 0.0 |
|15 |LdBPK_364730.1 |cytoplasmic: 0.93 |nuclear: 0.06 |mitochondrial: 0.0  |peroxisomal: 0.0     |Golgi apparatus: 0.0  |extracellular: 0.0    |plasma membrane: 0.0 |ER: 0.0 |lysosomal: 0.0 |

which, on the R console looks somewhat better, it is although not really readable here.

Thanks

Kusum
  • 501
  • 2
  • 11
  • 30
BCArg
  • 2,094
  • 2
  • 19
  • 37
  • I see that the table was edited by another used. My question is exactly how can I edit that to display here with a decent format. Thanks! – BCArg Jul 01 '16 at 11:39
  • What do you expect? What is you intention? Perhaps `summary` already helps? – Christoph Jul 01 '16 at 11:40
  • I mean, if I paste the head of the table directly from the R console into the text dialog, the table will look very messy. What I want is to paste from the R console in the question dialog and have the format that is shown now which was edited by another user and approved by me. – BCArg Jul 01 '16 at 11:42
  • I imagine there should be an option on the question dialog. For example, if I want to type a code, I can select the option to type a code, then the code will appear distinguished from the text. I could not find such an option to paste a table though. – BCArg Jul 01 '16 at 11:44
  • I don't understand. Which editor do you use? Or do you want to export to another format, like `rmd` or `xlsx`? – Christoph Jul 01 '16 at 11:52
  • I want to copy from the R console and paste it into the field where I can ask questions here in Stackoverflow and display a decent format, as the one edited by Kusum. The trouble is, if I paste directly from the R console in the dialog where I write my question here, the table does not appear formated. – BCArg Jul 01 '16 at 13:34
  • The idea here is not to paste the full data in a nice way but to give a minimal example, which reproduces the error. Please read [(1)](http://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](http://stackoverflow.com/help/mcve) How to create a MCVE as well as [(3)](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. – Christoph Jul 01 '16 at 13:37
  • I finally found the answer to my question. After pasting my (unformated table) into the text field, I should select the table (not the whole table just the head of it) I want to display in a decent format and select the "Code Sample" – BCArg Jul 01 '16 at 13:54
  • Hit the space bar 4 times per line. If it is short you can use back ticks to make the code formatted `like this`. Sometimes I paste what I want to post into notepad or an empty script, then hold alt + left click and create a vertical line on the left of my text. Then hit space bar 4 times and copy/paste the whole block, including empty space. – Bryan Goggin Jul 01 '16 at 15:11

0 Answers0