43

I want to reproduce my code in Word 2010. 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. How can it be done?

1
  • 29
    We should be a bit less dogmatic. I think for someone starting this business, this is close to programming. Commented Apr 12, 2012 at 18:14

9 Answers 9

40

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.

5
  • 1
    Fantastic! I'm working in the script editor and was looking to paste from there. Didn't realize Notepad++ was that clever. Thanks very much!
    – dubhousing
    Commented Apr 13, 2012 at 23:51
  • It does work as expected. I will mark as correct - didn't notice the option below the upvote/downvote buttons (which I can't use yet). Thanks again!
    – dubhousing
    Commented Apr 14, 2012 at 16:12
  • Works with PowerPoint 2016 on Windows 10 also
    – scs
    Commented Sep 21, 2016 at 18:21
  • I had an issue with Copy HTML to clipboard not preserving the carriage returns while pasting into PowerPoint. The NppExport -> Copy RTF to clipboard preserved the carriage returns for me and the syntax highlighting.
    – Matt L.
    Commented Jan 23, 2017 at 18:25
  • This did not work for me. It's word that's limiting my to paste unformatted text.
    – babipsylon
    Commented Jun 29, 2021 at 12:41
30

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

9

Assuming you have internet access

  1. Copy and paste to gist.gisthub.com
  2. Select 'R' as the language - this should provide colours
  3. Hit create (secret or public) gist
  4. Copy and paste from the gist to your word processor.

Compared with the notepad++ solution:

  • An online backup to your code, with a recording of the time when you clipped it.
  • You don't have to install any other software, useful if you're a student using a public computer.
2
  • 1
    Good answer. Only problem is that maybe you don't want to share your code publicly. Commented Jul 9, 2015 at 8:18
  • 2
    does not work any more, they seem to have removed the option to choose your language Commented Nov 13, 2017 at 2:53
3

If you just need the code as formatted:

Step1: Just add #+eval=FALSE at the beginning of your code.

Step2: Then go to File -> Knit Document. Compile the file in msword/PDF/Html.

OR

Just add #+eval=FALSE at the beginning of your code.

Press CTRL+SHIFT+K and then compile the file in msword/PDF/Html.

If you need the code with output do not enter add #+eval=FALSE at the beginning of your code and perform step 2 directly.

1

After you copy from the Rstudio Console window and paste into a Word document, you need to highlight all the the just copied text and change the font into Courier New. This will give you the same spacing and lineup as you had in the Rstudio Console window.

2
  • 1
    Almost. There will be too much space between the lines, because each one starts a new paragraph. Best is to define a style sheet code, and set "after" to 0pt. Commented Apr 12, 2012 at 18:16
  • Thanks guys, I will try coupling your two suggestions.
    – dubhousing
    Commented Apr 12, 2012 at 18:27
1

I agree with zeehio that using Knitr is probably the best option. But another way is to use the Pretty R tool and the "open document text" steps here. Basically just copy and paste your code into pretty R, and copy and paste the output (not the html) into the open document.

1

Copy paste the code from Rstudio editor to 'visual studio code' & then again copy from there into a word processor. For this to happen you must first install R extension in visual studio code. 'Visual studio code' is itself an IDE which can potentially be used for R language as well, but right now I'm emphasizing on using it to answer the above question.

1
  • Can you give an example of that that looks like?
    – JJJ
    Commented May 7, 2019 at 12:51
1

If you want to retain the formatting when coping a selection from the R Console you will need to install an older version of R Studio. Version 1.2.5042. it will not work in the newer versions

0

In R I use the Monaco editor font. To copy paste the output of the R consol in Microsoft Word, I select the output of the consol, right click and copy and paste in my Word document. Once I have pasted the output in word, I select it and put it in Word's Monaco font and reduce the size of the font if necessary.

This does the job very nicely and perfectly preserves the output style from the R consol, as well as written chunks of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.