16

In R I always like to print out the script since it gives a good overview and one can adjust eventual errors. I like the syntax highlighting in R-Studio because it facilitates reading and fast comprehension of code.

Is there a way to print out the text with the highlighting I see in the editor?

0

5 Answers 5

21

Its not an R-Studio solution, but notepad++ will print R source with syntax highlighting.

0
19

RStudio will not print in colour, but it's easy to save the code as a PDF; in this case the syntax format is preserved. My favourite package is knitr.

library(knitr) 
stitch("file_name.R")

The default output is PDF/Markup in .tex. If you prefer not to typeset, running the below will export as .html

stitch(script="file_name.R", system.file("misc", "knitr-template.Rhtml", package="knitr"))
2
  • this has been modified to stitch(script="file_name.R") note script rather than file
    – dpel
    Commented Oct 20, 2016 at 9:44
  • 4
    This probably is the best solution so far, but The problem is that it runs the code. My code is very very very time consuming to run and I just want the syntax highlighting of my 3k lines of code. any solution for that?
    – Mehrad
    Commented Mar 26, 2018 at 11:51
3

Brief explanation

The reason this is an answer to this question in because of the last line of the question:

Is there a way to print out the text with the highlighting I see in the editor?

so we are not limited to only and only using Rstudio software here.

After exploring the awesome answer by @rrg and realizing that it runs the code line by line, I wrote a comment below his answer and continued googling. My problem is that the code I wrote is so large and so time consuming to run that running it for the sake of having a syntax highlighted version is not feasible.

Most of the solution out there online involves having notepad++ which is a Windows application and I'm a dedicated Linux user, so I searched for a way I can do this in Linux (and possibly Mac)

The way I solved it:

Inspired by a blog post, I used the famous and beloved Vim to convert R to syntax highlighted HTML and then because you can open HTML in your browser, you can what ever you want with it (print, screenshot, etc.)

  1. Activate synax highlighting in Vim:

    • open terminal
    • then open the vim config file by typing vim ~/.vimrc
    • press i from keyboard to go to "insert mode"
    • go to the end of the file using arrow keys on your keyboard
    • type syntax on at the end of the file
    • now you need to save and exit. For this you need to press Esc button from keyboard to come out of "insert mode" and then type :x and press Enter to save and close the file.
    • if you want to change the color scheme of the syntax highlighting, visit the bottom part of this website
  2. From terminal open your file with Vim:

    vim YOUR_FILE_PATH
    
  3. Having you R code open in vim, you can turn on the line numbers if you like by pressing Esc and then write :set number and press Enter.

  4. For converting R to HTML, press Esc to make sure you are not in "insert mode" and then type :TOhtml and press Enter. This will result is having a split window in terminal, half is your R code and the other half id your new HTML code.

  5. For saving the files, type :x along with Enter button from keyboard twice to save both files (your R file will be unchanged if you have not typed anything extra in it and your HTML file will be created with the same name near your R code)

  6. Now open it with your favorite browser (in my case Vivaldi) and do what ever you want (in my case converting the whole HTML into PNG)

2

Best way: download https://github.com/jaredpetersen/codeprinter and paste in the r code. then choose syntax highlighting Xcode

4
  • then you got exactly the colors like in rstudio
    – lb_net
    Commented Nov 1, 2018 at 12:16
  • @jzadra I checked it again and it still works for me
    – lb_net
    Commented Apr 30, 2021 at 11:24
  • 1
    Thanks, you are right. I was expecting it to change in the text box where code is pasted when I selected XCode. I see that it does not, but it does change when going to print.
    – jzadra
    Commented May 3, 2021 at 15:25
  • @jzadra Yes, this is a strange behavior.
    – lb_net
    Commented May 5, 2021 at 9:03
1

For those using a Mac (and thus without access to Notepad++) cutting and pasting into Xcode and printing from there will also work.

As with Ron Jensen's earlier comment, this isn't an R Studio solution, but in the interests of "just getting it to work", I hope this helps someone.

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