Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve formatting from rstudio when copy/pasting to Word?

New to R, Rstudio, and SO -- my apologies in advance for any faux pas.

I want to reproduce my code in Word 2010 for a homework assignment. The scripts were written in rstudio, and I would like to preserve rstudio's formatting when pasting into Word. Principally, I like the font colors and spacing that rstudio uses. I find that when I paste from SAS to Word, the formatting is preserved, but no dice here.

I would usually look for copy special / paste special options to do this, but I can't find any. When I try to paste special into word, only unformatted text options are presented. I would rather not reformat the text line-by-line, because I think it looks pretty nice in rstudio.

I thought of trying to save the script in rstudio to some format that would preserve its formatting, but I couldn't find any way to do this. Does anyone have an idea of how I might pull this off?

Thanks in advance

like image 240
dubhousing Avatar asked Apr 12 '12 17:04

dubhousing


People also ask

How do I copy and paste from R Studio to Word?

How can I copy the output of the R console into a word document? Select what you want in the console and copy. Then in Word paste using Keep Text Only. (Right click to get the paste options.)

How do you copy and paste in R console?

If R is ready to accept commands, the R console shows a > prompt. If it receives a command (by typing, copy-pasting or sent from the script editor using Ctrl-Enter ), R will try to execute it, and when ready, show the results and come back with a new > -prompt to wait for new commands.


2 Answers

It's not totally clear whether you are pasting from RStudio's script editor (which has some 4 or 5 colors) or from the R console (script + output) within RStudio (which only has 2 colors).

If you are pasting from the console--please check "Paste special" again. There should be an option for "HTML Format" that will do what you need (though you may need to resize the font to make everything fit properly depending on your page margins).

If you are pasting from the script editor, then you're out of luck with a direct copy-and-paste solution. But there is a copy-and-paste-and-copy-and-paste solution...

One solution could be to use Notepad++. From RStudio, save your script (with a ".R" extension) then open the script in Notepad++. (Or copy and paste from RStudio to Notepad++, but make sure you set the file's language--from the "Language" menu--to R). When your script is correctly highlighted in Notepad++ go to the "Plugins > NppExport > Copy HTML to clipboard" menu to copy the open file. This can then be pasted into MS Word with HTML format.

like image 119
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 28 '22 06:09

A5C1D2H2I1M1N2O1R2T1


Just in case someone else looks for this question...

Another way to have all the source code in a word document with a good-looking format using RStudio is to use the File/Compile Notebook option, choosing MS Word as the output format.

Using this option, a .docx document will be generated with the output of your script as well as the original source code. The script will be executed, though.

If you don't want your code to be evaluated (you just want a simple copy-paste), you can add #+eval=FALSE at the beginning of your script and then the source code will be reproduced in the word document without being evaluated.

This approach relies on knitr. Here is an example if anyone wants to start playing with this.

#' --- #' title: "My homework" #' author: John Doe #' date: June 15, 2015 #' output: word_document #' ---  # The header above sets some metadata used in the knitr output  # Conventional comments are formatted as regular comments  # Comments starting with "#+" control different knitr options.  #+echo=FALSE,message=FALSE,warning=FALSE library(ggplot2)   #+echo=TRUE #' Comments with a "+" sign are used to tell knitr what should be #' done with the chunk of code: #' #'  - echo: Show the original code or not #'  - eval: Run the original code or not #'  - message: Print messages #'  - warning: Print warnings #'  - error: Print errors #'  ...  #' Comments with an apostrophe "'" will be printed as regular text. #' This is very useful to explain what you are actually doing!  # Regular comments can be used to document the code as usual # Figures are printed: ggplot(mpg, aes(x=cty, y=hwy)) + geom_point(aes(color=class))  #' Formatting **options** are possible. #' Even [links](http://stackoverflow.com/questions/10128702/how-to-preserve-formatting-from-rstudio-when-copy-pasting-to-word) #'   #' This will show all the packages and versions used to generate this document. #' It can be used to make sure that your teacher has all he needs to run your script #' if he/she wants to. sessionInfo() 

Word document example

like image 24
zeehio Avatar answered Sep 28 '22 07:09

zeehio