Invert RStudio Colors For Less Eye Strain

RStudio is by far the most fully featured and mature IDE for R available. It is not, however, without its flaws. The only reason I ever use SublimeText is because of the dark programming environment it provides. Yes, you can switch the syntax highlighting in RStudio to Monokai, but if you need to look anywhere beyond the Source and Console windows, your eyes are accosted with fields of white. SublimeText doesn’t have this issue. Then again, RStudio is a web browser, so with a bit if custom JavaScript, neither does RStudio (for the most part).

rstudio_invert

HTML for the bare webpage RStudio displays is available in the RStudio/www folder. We can add some extra JavaScript to the index.html file that will cause all the colors in the RStudio window to invert, given a keyboard shortcut.

I figured this out through a lot of trial and error and stack overflow research. I don’t know anything about JavaScript, so for those readers who do, have mercy on my terrible programming style. You’ll need JQuery for this to work too. RStudio already uses JQuery I think, but you still need to explicitly load it for this to work for some reason. Download JQuery from here and save it in the rstudio/www/js folder. Call it “jquery.min.js”.

On my computer, RStudio is installed in /usr/lib/rstudio.

You can do it using R’s download.file() function like so (making sure your working directory is the rstudio folder):

Now, edit the index.html file in rstudio/www to look like the below (lines I added are highlighted)

The first part is a function that will invert colors of a webpage. The second part activates the function if you press “Ctrl+Shift+I“. It doesn’t work backwards though–if you invert colors, pressing “Ctrl+Shift+I” again does nothing. You have to right click somewhere inside the RStudio window and select “Reload” to convert everything back to normal. And sometimes, when you click “Reload”, it just hangs, so you have to right click again and hit “Stop” and then it will go back to normal.

So it’s a little hacky, but it works and it saves my eyes from exploding.

Leave a Reply