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

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.


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