customise R help files - font colouring

395 views Asked by At

I'm wondering is it possible to customise R help files so that certain text is colour coded and easier to read. rdoc already does this except that it sends the output to the console. I would instead, like it to be sent to the help panel (i'm using Rstudio). Is there any workaround for this?

If we run ?lm normally, we can see the usual help file in the help panel on the right below but when you do it again after using rdoc in Rstudio we get the help file colour coded which is great but its sent to the console output (left side). Ideally, we would like it to remain on display in the help panel as we are running code. The way it is now - it disappears the minute you run something.

?lm
#devtools::install_github("mdequeljoe/rdoc")
library(rdoc)
options(rdoc.by_section = FALSE)
rdoc(lm)

enter image description here

I want to put the code into my .rprofile similar to @csgillespie .rprofile. Note, if you follow his code you can use ?lm instead of having to call rdoc(lm) directly to produce the colour coded console output.

I have a feeling this can't be done easily (if at all?) but interested to hear any suggestions.

Thanks

2

There are 2 answers

3
Allan Cameron On BEST ANSWER

This is possible, but a little involved. You'll need your own css file defined to do it, though it would be possible to create a function that writes appropriate css.

As a proof of concept, I have used a copy of the "R.css" file defined inside every package's "/html" folder, and just changed the h2 color to red, with the file saved locally as "my.css".

Anyway, once you have the css file, this function will show the appropriate help file with the appropriate styling in your R viewer window:

help_css <- function(func, css_file)
{
  pack <- sub("^.*:(.*)>$", "\\1", capture.output(environment(func)))
  func <- deparse(substitute(func))
  
  x <- readLines(paste0(find.package(pack), "/html/", func, ".html"))
  x[3] <- paste("<style>", 
                paste(readLines(css_file), collapse = "\n"), 
                "</style>")
  writeLines(x, file.path(tempdir(), "test.html"))
  
  myViewer <- getOption("viewer")
  myViewer(file.path(tempdir(), "test.html"))
}

So, for example, if I do:

help_css(lm, "my.css")

I get:

enter image description here

2
Salim B On

As of RStudio v1.2 you can style RStudio's integrated help pane by creating a custom user theme (essentially an .rstheme file).

I've given help pane styling a try in extending the rscodeio theme (without colored syntax highlighting, though). The latest CSS code is found here.

The help pane styling is currently only available in the optional Tomorrow Night Bright (rscodeio) editor theme.

To use it right away, you can either

  • install the current rscodeio master branch using remotes:

    remotes::install_github("anthonynorth/rscodeio")
    

    And then activating the editor theme named Tomorrow Night Bright (rscodeio) under ToolsGlobal Options…AppearanceEditor theme. A first attempt of the help pane CSS code is included.

  • or – recommended – install my fork's interim-merge branch which contains all my latest work[1] overhauling the package, including a new apply_theme parameter to activate the desired editor theme right away:

    remotes::install_github("salim-b/rscodeio@interim-merge")
    rscodeio::install_themes(apply_theme = "Tomorrow Night Bright (rscodeio)")
    

[1]: This has also been proposed upstream a while ago (1, 2) but I haven't heard back from the author since.

The result looks as follows (example for ?pal::as_string):